summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/packages_and_registries/settings/project/components/packages_cleanup_policy_form.vue
blob: 80df8ef81e6ccd6c1e5e8758035da1022abe8ea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<script>
import { GlButton } from '@gitlab/ui';
import { sprintf } from '~/locale';
import {
  UPDATE_SETTINGS_ERROR_MESSAGE,
  UPDATE_SETTINGS_SUCCESS_MESSAGE,
  KEEP_N_DUPLICATED_PACKAGE_FILES_DESCRIPTION,
  KEEP_N_DUPLICATED_PACKAGE_FILES_FIELDNAME,
  KEEP_N_DUPLICATED_PACKAGE_FILES_LABEL,
  SET_CLEANUP_POLICY_BUTTON,
  READY_FOR_CLEANUP_MESSAGE,
  TIME_TO_NEXT_CLEANUP_MESSAGE,
} from '~/packages_and_registries/settings/project/constants';
import packagesCleanupPolicyQuery from '~/packages_and_registries/settings/project/graphql/queries/get_packages_cleanup_policy.query.graphql';
import updatePackagesCleanupPolicyMutation from '~/packages_and_registries/settings/project/graphql/mutations/update_packages_cleanup_policy.mutation.graphql';
import { formOptionsGenerator } from '~/packages_and_registries/settings/project/utils';
import Tracking from '~/tracking';
import { approximateDuration, calculateRemainingMilliseconds } from '~/lib/utils/datetime_utility';
import ExpirationDropdown from './expiration_dropdown.vue';

export default {
  components: {
    GlButton,
    ExpirationDropdown,
  },
  mixins: [Tracking.mixin()],
  inject: ['projectPath'],
  props: {
    value: {
      type: Object,
      required: true,
    },
    isLoading: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  formOptions: formOptionsGenerator(),
  i18n: {
    KEEP_N_DUPLICATED_PACKAGE_FILES_LABEL,
    KEEP_N_DUPLICATED_PACKAGE_FILES_DESCRIPTION,
    SET_CLEANUP_POLICY_BUTTON,
    TIME_TO_NEXT_CLEANUP_MESSAGE,
    READY_FOR_CLEANUP_MESSAGE,
  },
  data() {
    return {
      tracking: {
        label: 'packages_cleanup_policies',
      },
      mutationLoading: false,
    };
  },
  computed: {
    prefilledForm() {
      return {
        ...this.value,
        keepNDuplicatedPackageFiles: this.findDefaultOption(
          KEEP_N_DUPLICATED_PACKAGE_FILES_FIELDNAME,
        ),
      };
    },
    showLoadingIcon() {
      return this.isLoading || this.mutationLoading;
    },
    isSubmitButtonDisabled() {
      return this.showLoadingIcon;
    },
    isFieldDisabled() {
      return this.showLoadingIcon;
    },
    mutationVariables() {
      return {
        projectPath: this.projectPath,
        keepNDuplicatedPackageFiles: this.prefilledForm.keepNDuplicatedPackageFiles,
      };
    },
    nextCleanupMessage() {
      const { nextRunAt } = this.value;
      const difference = calculateRemainingMilliseconds(nextRunAt);
      return difference
        ? sprintf(TIME_TO_NEXT_CLEANUP_MESSAGE, {
            nextRunAt: approximateDuration(difference / 1000),
          })
        : READY_FOR_CLEANUP_MESSAGE;
    },
  },
  methods: {
    findDefaultOption(option) {
      return this.value[option] || this.$options.formOptions[option].find((f) => f.default)?.key;
    },
    submit() {
      this.track('submit_packages_cleanup_form');
      this.mutationLoading = true;
      return this.$apollo
        .mutate({
          mutation: updatePackagesCleanupPolicyMutation,
          variables: {
            input: this.mutationVariables,
          },
          awaitRefetchQueries: true,
          refetchQueries: [
            {
              query: packagesCleanupPolicyQuery,
              variables: {
                projectPath: this.projectPath,
              },
            },
          ],
        })
        .then(({ data }) => {
          const [errorMessage] = data?.updatePackagesCleanupPolicy?.errors ?? [];
          if (errorMessage) {
            throw errorMessage;
          } else {
            this.$toast.show(UPDATE_SETTINGS_SUCCESS_MESSAGE);
          }
        })
        .catch(() => {
          this.$toast.show(UPDATE_SETTINGS_ERROR_MESSAGE);
        })
        .finally(() => {
          this.mutationLoading = false;
        });
    },
    onModelChange(newValue, model) {
      this.$emit('input', { ...this.value, [model]: newValue });
    },
  },
};
</script>

<template>
  <form ref="form-element" @submit.prevent="submit">
    <expiration-dropdown
      :value="prefilledForm.keepNDuplicatedPackageFiles"
      :disabled="isFieldDisabled"
      :form-options="$options.formOptions.keepNDuplicatedPackageFiles"
      :label="$options.i18n.KEEP_N_DUPLICATED_PACKAGE_FILES_LABEL"
      :description="$options.i18n.KEEP_N_DUPLICATED_PACKAGE_FILES_DESCRIPTION"
      dropdown-class="gl-md-max-w-50p"
      name="keep-n-duplicated-package-files"
      data-testid="keep-n-duplicated-package-files-dropdown"
      @input="onModelChange($event, 'keepNDuplicatedPackageFiles')"
    />
    <p v-if="value.nextRunAt" data-testid="next-run-at">
      {{ nextCleanupMessage }}
    </p>
    <div class="gl-mt-7 gl-display-flex gl-align-items-center">
      <gl-button
        data-testid="save-button"
        type="submit"
        :disabled="isSubmitButtonDisabled"
        :loading="showLoadingIcon"
        category="primary"
        variant="confirm"
        class="js-no-auto-disable gl-mr-4"
      >
        {{ $options.i18n.SET_CLEANUP_POLICY_BUTTON }}
      </gl-button>
    </div>
  </form>
</template>