summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/security_configuration/components/manage_via_mr.vue
blob: fe408354f66d95baa0f8a2f4f0d4ee23913931aa (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
<script>
import { GlButton } from '@gitlab/ui';
import { featureToMutationMap } from 'ee_else_ce/security_configuration/components/constants';
import { parseErrorMessage } from '~/lib/utils/error_message';
import { redirectTo } from '~/lib/utils/url_utility'; // eslint-disable-line import/no-deprecated
import { sprintf, s__ } from '~/locale';
import apolloProvider from '../provider';

function mutationSettingsForFeatureType(type) {
  return featureToMutationMap[type];
}

export const i18n = {
  buttonLabel: s__('SecurityConfiguration|Configure with a merge request'),
  noSuccessPathError: s__(
    'SecurityConfiguration|%{featureName} merge request creation mutation failed',
  ),
  genericErrorText: s__(
    `SecurityConfiguration|Something went wrong. Please refresh the page, or try again later.`,
  ),
};

export default {
  apolloProvider,
  components: {
    GlButton,
  },
  inject: ['projectFullPath'],
  props: {
    feature: {
      type: Object,
      required: true,
    },
    variant: {
      type: String,
      required: false,
      default: 'confirm',
    },
    category: {
      type: String,
      required: false,
      default: 'secondary',
    },
  },
  data() {
    return {
      isLoading: false,
    };
  },
  computed: {
    mutationSettings() {
      return mutationSettingsForFeatureType(this.feature.type);
    },
  },
  methods: {
    async mutate() {
      this.isLoading = true;
      try {
        const { mutationSettings } = this;
        const { data } = await this.$apollo.mutate(
          mutationSettings.getMutationPayload(this.projectFullPath),
        );
        const { errors, successPath } = data[mutationSettings.mutationId];

        if (errors.length > 0) {
          throw new Error(errors[0]);
        }

        // Sending window.gon.uf_error_prefix prefixed messages should happen only in
        // the backend. Hence the code below is an anti-pattern.
        // The issue to refactor: https://gitlab.com/gitlab-org/gitlab/-/issues/397714
        if (!successPath) {
          throw new Error(
            `${window.gon.uf_error_prefix} ${sprintf(this.$options.i18n.noSuccessPathError, {
              featureName: this.feature.name,
            })}`,
          );
        }

        redirectTo(successPath); // eslint-disable-line import/no-deprecated
      } catch (e) {
        this.$emit('error', parseErrorMessage(e, this.$options.i18n.genericErrorText));
        this.isLoading = false;
      }
    },
  },
  /**
   * Returns a boolean representing whether this component can be rendered for
   * the given feature. Useful for parent components to determine whether or
   * not to render this component.
   * @param {Object} feature The feature to check.
   * @returns {boolean}
   */
  canRender(feature) {
    const { available, configured, canEnableByMergeRequest, type } = feature;
    return (
      canEnableByMergeRequest &&
      available &&
      !configured &&
      Boolean(mutationSettingsForFeatureType(type))
    );
  },
  i18n,
};
</script>

<template>
  <gl-button
    v-if="!feature.configured"
    data-testid="configure-via-mr-button"
    :loading="isLoading"
    :variant="variant"
    :category="category"
    :data-qa-selector="`${feature.type}_mr_button`"
    @click="mutate"
    >{{ $options.i18n.buttonLabel }}</gl-button
  >
</template>