summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/packages_and_registries/settings/project/components/packages_cleanup_policy_form.vue
blob: b1751d5174a07ca918f3d9e4eb014d1fd8acf822 (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
<script>
import { GlButton } from '@gitlab/ui';
import {
  UPDATE_SETTINGS_ERROR_MESSAGE,
  UPDATE_SETTINGS_SUCCESS_MESSAGE,
  SET_CLEANUP_POLICY_BUTTON,
  KEEP_N_DUPLICATED_PACKAGE_FILES_DESCRIPTION,
  KEEP_N_DUPLICATED_PACKAGE_FILES_FIELDNAME,
  KEEP_N_DUPLICATED_PACKAGE_FILES_LABEL,
} from '~/packages_and_registries/settings/project/constants';
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 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,
  },
  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,
      };
    },
  },
  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,
          },
        })
        .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">
    <div class="gl-md-max-w-50p">
      <expiration-dropdown
        v-model="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"
        name="keep-n-duplicated-package-files"
        data-testid="keep-n-duplicated-package-files-dropdown"
        @input="onModelChange($event, 'keepNDuplicatedPackageFiles')"
      />
    </div>
    <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>