All files / app/assets/javascripts/feature_flags/components form.vue

45% Statements 9/20
7.14% Branches 1/14
69.23% Functions 9/13
45% Lines 9/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 2324x                                                                                                                                                                       10x                     12x     10x         12x       12x       1x       1x     1x                                                                                                                                                                                                                                  
<!-- eslint-disable vue/multi-word-component-names -->
<script>
import { GlButton } from '@gitlab/ui';
import { memoize, cloneDeep, isNumber, uniqueId } from 'lodash';
import Vue from 'vue';
import { s__ } from '~/locale';
import RelatedIssuesRoot from '~/related_issues/components/related_issues_root.vue';
import featureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import {
  ROLLOUT_STRATEGY_ALL_USERS,
  ROLLOUT_STRATEGY_PERCENT_ROLLOUT,
  ROLLOUT_STRATEGY_USER_ID,
  ALL_ENVIRONMENTS_NAME,
  NEW_VERSION_FLAG,
} from '../constants';
import Strategy from './strategy.vue';
 
export default {
  i18n: {
    removeLabel: s__('FeatureFlags|Remove'),
    statusLabel: s__('FeatureFlags|Status'),
  },
  components: {
    GlButton,
    Strategy,
    RelatedIssuesRoot,
  },
  mixins: [featureFlagsMixin()],
  inject: {
    featureFlagIssuesEndpoint: {
      default: '',
    },
  },
  props: {
    active: {
      type: Boolean,
      required: false,
      default: true,
    },
    name: {
      type: String,
      required: false,
      default: '',
    },
    description: {
      type: String,
      required: false,
      default: '',
    },
    cancelPath: {
      type: String,
      required: true,
    },
    submitText: {
      type: String,
      required: true,
    },
    strategies: {
      type: Array,
      required: false,
      default: () => [],
    },
  },
  translations: {
    allEnvironmentsText: s__('FeatureFlags|* (All Environments)'),
 
    helpText: s__(
      'FeatureFlags|Feature Flag behavior is built up by creating a set of rules to define the status of target environments. A default wildcard rule %{codeStart}*%{codeEnd} for %{boldStart}All Environments%{boldEnd} is set, and you are able to add as many rules as you need by choosing environment specs below. You can toggle the behavior for each of your rules to set them %{boldStart}Active%{boldEnd} or %{boldStart}Inactive%{boldEnd}.',
    ),
 
    newHelpText: s__(
      'FeatureFlags|Enable features for specific users and environments by configuring feature flag strategies.',
    ),
    noStrategiesText: s__('FeatureFlags|Feature Flag has no strategies'),
  },
 
  ROLLOUT_STRATEGY_ALL_USERS,
  ROLLOUT_STRATEGY_PERCENT_ROLLOUT,
  ROLLOUT_STRATEGY_USER_ID,
 
  // Matches numbers 0 through 100
  rolloutPercentageRegex: /^[0-9]$|^[1-9][0-9]$|^100$/,
 
  data() {
    return {
      formName: this.name,
      formDescription: this.description,
 
      formStrategies: cloneDeep(this.strategies),
 
      newScope: '',
    };
  },
  computed: {
    filteredStrategies() {
      return this.formStrategies.filter((s) => !s.shouldBeDestroyed);
    },
    showRelatedIssues() {
      return Boolean(this.featureFlagIssuesEndpoint);
    },
  },
  methods: {
    keyFor(strategy) {
      Iif (strategy.id) {
        return strategy.id;
      }
 
      return uniqueId('strategy_');
    },
 
    addStrategy() {
      this.formStrategies.push({ name: ROLLOUT_STRATEGY_ALL_USERS, parameters: {}, scopes: [] });
    },
 
    deleteStrategy(s) {
      Iif (isNumber(s.id)) {
        Vue.set(s, 'shouldBeDestroyed', true);
      } else {
        this.formStrategies = this.formStrategies.filter((strategy) => strategy !== s);
      }
    },
 
    isAllEnvironment(name) {
      return name === ALL_ENVIRONMENTS_NAME;
    },
    /**
     * When the user clicks the submit button
     * it triggers an event with the form data
     */
    handleSubmit() {
      const flag = {
        name: this.formName,
        description: this.formDescription,
        active: this.active,
        version: NEW_VERSION_FLAG,
        strategies: this.formStrategies,
      };
 
      this.$emit('handleSubmit', flag);
    },
 
    isRolloutPercentageInvalid: memoize(function isRolloutPercentageInvalid(percentage) {
      return !this.$options.rolloutPercentageRegex.test(percentage);
    }),
    onFormStrategyChange(strategy, index) {
      const currentUserListId = this.filteredStrategies[index]?.userList?.id;
      const newUserListId = strategy?.userList?.id;
 
      Object.assign(this.filteredStrategies[index], strategy);
 
      Iif (currentUserListId !== newUserListId) {
        this.formStrategies = [...this.formStrategies];
      }
    },
  },
};
</script>
<template>
  <form class="feature-flags-form">
    <fieldset>
      <div class="gl-display-flex gl-flex-wrap gl-mx-n5">
        <div class="gl-mb-5 gl-px-5 gl-w-full col-md-4">
          <label for="feature-flag-name" class="gl-font-weight-bold"
            >{{ s__('FeatureFlags|Name') }} *</label
          >
          <input id="feature-flag-name" v-model="formName" class="form-control" />
        </div>
      </div>
 
      <div class="gl-display-flex gl-flex-wrap gl-mx-n5">
        <div class="gl-mb-5 gl-px-5 gl-w-full col-md-4">
          <label for="feature-flag-description" class="gl-font-weight-bold">
            {{ s__('FeatureFlags|Description') }}
          </label>
          <textarea
            id="feature-flag-description"
            v-model="formDescription"
            class="form-control"
            rows="4"
          ></textarea>
        </div>
      </div>
 
      <related-issues-root
        v-if="showRelatedIssues"
        :endpoint="featureFlagIssuesEndpoint"
        :can-admin="true"
        :show-categorized-issues="false"
      />
 
      <div class="gl-display-flex gl-flex-wrap gl-mx-n5">
        <div class="gl-mb-5 gl-px-5 gl-w-full">
          <h4>{{ s__('FeatureFlags|Strategies') }}</h4>
          <div class="gl-display-flex gl-align-items-baseline gl-justify-content-space-between">
            <p class="gl-mr-5">{{ $options.translations.newHelpText }}</p>
            <gl-button variant="confirm" category="secondary" @click="addStrategy">
              {{ s__('FeatureFlags|Add strategy') }}
            </gl-button>
          </div>
        </div>
      </div>
      <div v-if="filteredStrategies.length > 0" data-testid="feature-flag-strategies">
        <strategy
          v-for="(strategy, index) in filteredStrategies"
          :key="keyFor(strategy)"
          :strategy="strategy"
          :index="index"
          @change="onFormStrategyChange($event, index)"
          @delete="deleteStrategy(strategy)"
        />
      </div>
      <div v-else class="gl-display-flex gl-justify-content-center gl-border-t gl-py-6 gl-w-full">
        <span>{{ $options.translations.noStrategiesText }}</span>
      </div>
    </fieldset>
 
    <div class="gl-mr-6">
      <gl-button
        ref="submitButton"
        type="button"
        variant="confirm"
        class="js-ff-submit gl-mr-2"
        @click="handleSubmit"
        >{{ submitText }}</gl-button
      >
      <gl-button :href="cancelPath" class="js-ff-cancel">
        {{ __('Cancel') }}
      </gl-button>
    </div>
  </form>
</template>