summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/feature_flags/components/edit_feature_flag.vue
blob: 7c9744da0e86d9fbba738db578e955ee4db4da0d (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<script>
import { GlAlert, GlLoadingIcon, GlToggle } from '@gitlab/ui';
import { createNamespacedHelpers } from 'vuex';
import axios from '~/lib/utils/axios_utils';
import { sprintf, s__ } from '~/locale';
import glFeatureFlagMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { LEGACY_FLAG, NEW_FLAG_ALERT } from '../constants';
import store from '../store/index';
import FeatureFlagForm from './form.vue';

const { mapState, mapActions } = createNamespacedHelpers('edit');

export default {
  store,
  components: {
    GlAlert,
    GlLoadingIcon,
    GlToggle,
    FeatureFlagForm,
  },
  mixins: [glFeatureFlagMixin()],
  props: {
    endpoint: {
      type: String,
      required: true,
    },
    path: {
      type: String,
      required: true,
    },
    environmentsEndpoint: {
      type: String,
      required: true,
    },
    projectId: {
      type: String,
      required: true,
    },
    featureFlagIssuesEndpoint: {
      type: String,
      required: true,
    },
    showUserCallout: {
      type: Boolean,
      required: true,
    },
    userCalloutId: {
      default: '',
      type: String,
      required: false,
    },
    userCalloutsPath: {
      default: '',
      type: String,
      required: false,
    },
  },
  data() {
    return {
      userShouldSeeNewFlagAlert: this.showUserCallout,
    };
  },
  translations: {
    legacyFlagAlert: s__(
      'FeatureFlags|GitLab is moving to a new way of managing feature flags, and in 13.4, this feature flag will become read-only. Please create a new feature flag.',
    ),
    legacyReadOnlyFlagAlert: s__(
      'FeatureFlags|GitLab is moving to a new way of managing feature flags. This feature flag is read-only, and it will be removed in 14.0. Please create a new feature flag.',
    ),
    newFlagAlert: NEW_FLAG_ALERT,
  },
  computed: {
    ...mapState([
      'error',
      'name',
      'description',
      'scopes',
      'strategies',
      'isLoading',
      'hasError',
      'iid',
      'active',
      'version',
    ]),
    title() {
      return this.iid
        ? `^${this.iid} ${this.name}`
        : sprintf(s__('Edit %{name}'), { name: this.name });
    },
    deprecated() {
      return this.hasNewVersionFlags && this.version === LEGACY_FLAG;
    },
    deprecatedAndEditable() {
      return this.deprecated && !this.hasLegacyReadOnlyFlags;
    },
    deprecatedAndReadOnly() {
      return this.deprecated && this.hasLegacyReadOnlyFlags;
    },
    hasNewVersionFlags() {
      return this.glFeatures.featureFlagsNewVersion;
    },
    hasLegacyReadOnlyFlags() {
      return (
        this.glFeatures.featureFlagsLegacyReadOnly &&
        !this.glFeatures.featureFlagsLegacyReadOnlyOverride
      );
    },
    shouldShowNewFlagAlert() {
      return !this.hasNewVersionFlags && this.userShouldSeeNewFlagAlert;
    },
  },
  created() {
    this.setPath(this.path);
    return this.setEndpoint(this.endpoint).then(() => this.fetchFeatureFlag());
  },
  methods: {
    ...mapActions([
      'updateFeatureFlag',
      'setEndpoint',
      'setPath',
      'fetchFeatureFlag',
      'toggleActive',
    ]),
    dismissNewVersionFlagAlert() {
      this.userShouldSeeNewFlagAlert = false;
      axios.post(this.userCalloutsPath, {
        feature_name: this.userCalloutId,
      });
    },
  },
};
</script>
<template>
  <div>
    <gl-alert
      v-if="shouldShowNewFlagAlert"
      variant="warning"
      class="gl-my-5"
      @dismiss="dismissNewVersionFlagAlert"
    >
      {{ $options.translations.newFlagAlert }}
    </gl-alert>
    <gl-loading-icon v-if="isLoading" />

    <template v-else-if="!isLoading && !hasError">
      <gl-alert v-if="deprecatedAndEditable" variant="warning" :dismissible="false" class="gl-my-5">
        {{ $options.translations.legacyFlagAlert }}
      </gl-alert>
      <gl-alert v-if="deprecatedAndReadOnly" variant="warning" :dismissible="false" class="gl-my-5">
        {{ $options.translations.legacyReadOnlyFlagAlert }}
      </gl-alert>
      <div class="gl-display-flex gl-align-items-center gl-mb-4 gl-mt-4">
        <gl-toggle
          :value="active"
          data-testid="feature-flag-status-toggle"
          data-track-event="click_button"
          data-track-label="feature_flag_toggle"
          class="gl-mr-4"
          @change="toggleActive"
        />
        <h3 class="page-title gl-m-0">{{ title }}</h3>
      </div>

      <div v-if="error.length" class="alert alert-danger">
        <p v-for="(message, index) in error" :key="index" class="gl-mb-0">{{ message }}</p>
      </div>

      <feature-flag-form
        :name="name"
        :description="description"
        :project-id="projectId"
        :scopes="scopes"
        :strategies="strategies"
        :cancel-path="path"
        :submit-text="__('Save changes')"
        :environments-endpoint="environmentsEndpoint"
        :feature-flag-issues-endpoint="featureFlagIssuesEndpoint"
        :active="active"
        :version="version"
        @handleSubmit="data => updateFeatureFlag(data)"
      />
    </template>
  </div>
</template>