All files / ee/app/assets/javascripts/analytics/cycle_analytics/components/create_value_stream_form custom_stage_fields.vue

92.31% Statements 24/26
91.67% Branches 11/12
89.47% Functions 17/19
92.31% Lines 24/26

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  4x 4x 4x 4x 4x 4x 4x                                                                                       37x           37x     37x     37x     37x     37x     39x     37x     37x     37x         185x           200x     200x     72x 12x     74x           136x                                                                                                                                                              
<script>
import { GlFormGroup, GlFormInput } from '@gitlab/ui';
import { isLabelEvent, getLabelEventsIdentifiers, uniqById } from '../../utils';
import { i18n } from './constants';
import CustomStageEventField from './custom_stage_event_field.vue';
import CustomStageEventLabelField from './custom_stage_event_label_field.vue';
import StageFieldActions from './stage_field_actions.vue';
import { startEventOptions, endEventOptions } from './utils';
 
export default {
  name: 'CustomStageFormFields',
  components: {
    GlFormGroup,
    GlFormInput,
    CustomStageEventField,
    CustomStageEventLabelField,
    StageFieldActions,
  },
  props: {
    index: {
      type: Number,
      required: true,
    },
    stageLabel: {
      type: String,
      required: true,
    },
    totalStages: {
      type: Number,
      required: true,
    },
    stage: {
      type: Object,
      required: true,
    },
    errors: {
      type: Object,
      required: false,
      default: () => ({}),
    },
    stageEvents: {
      type: Array,
      required: true,
    },
    defaultGroupLabels: {
      type: Array,
      required: false,
      default: () => [],
    },
  },
  data() {
    return {
      labelEvents: getLabelEventsIdentifiers(this.stageEvents),
    };
  },
  computed: {
    startEvents() {
      return startEventOptions(this.stageEvents);
    },
    endEvents() {
      return endEventOptions(this.stageEvents, this.stage.startEventIdentifier);
    },
    hasStartEvent() {
      return this.stage.startEventIdentifier;
    },
    startEventRequiresLabel() {
      return isLabelEvent(this.labelEvents, this.stage.startEventIdentifier);
    },
    endEventRequiresLabel() {
      return isLabelEvent(this.labelEvents, this.stage.endEventIdentifier);
    },
    hasMultipleStages() {
      return this.totalStages > 1;
    },
    selectedStartEventName() {
      return this.eventName(this.stage.startEventIdentifier, 'SELECT_START_EVENT');
    },
    selectedEndEventName() {
      return this.eventName(this.stage.endEventIdentifier, 'SELECT_END_EVENT');
    },
    initialGroupLabels() {
      return uniqById(
        [
          this.stage.startEventLabelId ? this.stage.startEventLabel : null,
          this.stage.endEventLabelId ? this.stage.endEventLabel : null,
          ...this.defaultGroupLabels,
        ].filter((l) => Boolean(l)),
      );
    },
  },
  methods: {
    hasFieldErrors(key) {
      return !Object.keys(this.errors).length || this.errors[key]?.length < 1;
    },
    fieldErrorMessage(key) {
      return this.errors[key]?.join('\n');
    },
    eventNameByIdentifier(identifier) {
      const ev = this.stageEvents.find((e) => e.identifier === identifier);
      return ev?.name || null;
    },
    eventName(eventId, textKey) {
      return eventId ? this.eventNameByIdentifier(eventId) : this.$options.i18n[textKey];
    },
  },
  i18n,
};
</script>
<template>
  <div data-testid="value-stream-stage-fields">
    <div class="gl-display-flex">
      <gl-form-group
        class="gl-flex-grow-1"
        :label="stageLabel"
        :state="hasFieldErrors('name')"
        :invalid-feedback="fieldErrorMessage('name')"
        :data-testid="`custom-stage-name-${index}`"
      >
        <!-- eslint-disable vue/no-mutating-props -->
        <gl-form-input
          v-model.trim="stage.name"
          :name="`custom-stage-name-${index}`"
          :placeholder="$options.i18n.FORM_FIELD_STAGE_NAME_PLACEHOLDER"
          required
          @input="$emit('input', { field: 'name', value: $event })"
        />
        <!-- eslint-enable vue/no-mutating-props -->
      </gl-form-group>
      <stage-field-actions
        v-if="hasMultipleStages"
        class="gl-mt-6"
        :index="index"
        :stage-count="totalStages"
        :can-remove="true"
        @move="$emit('move', $event)"
        @remove="$emit('remove', $event)"
      />
    </div>
    <div class="gl-display-flex gl-justify-content-between">
      <custom-stage-event-field
        event-type="start-event"
        :index="index"
        :field-label="$options.i18n.FORM_FIELD_START_EVENT"
        :selected-event-name="selectedStartEventName"
        :events-list="startEvents"
        :identifier-error="fieldErrorMessage('startEventIdentifier')"
        :has-identifier-error="hasFieldErrors('startEventIdentifier')"
        @update-identifier="$emit('input', { field: 'startEventIdentifier', value: $event })"
      />
      <custom-stage-event-label-field
        event-type="start-event"
        :index="index"
        :field-label="$options.i18n.FORM_FIELD_START_EVENT_LABEL"
        :initial-group-labels="initialGroupLabels"
        :requires-label="startEventRequiresLabel"
        :label-error="fieldErrorMessage('startEventLabelId')"
        :has-label-error="hasFieldErrors('startEventLabelId')"
        :selected-label-ids="[stage.startEventLabelId]"
        @update-label="$emit('input', { field: 'startEventLabelId', value: $event })"
      />
    </div>
    <div class="gl-display-flex gl-justify-content-between">
      <custom-stage-event-field
        event-type="end-event"
        :index="index"
        :disabled="!hasStartEvent"
        :field-label="$options.i18n.FORM_FIELD_END_EVENT"
        :selected-event-name="selectedEndEventName"
        :events-list="endEvents"
        :identifier-error="fieldErrorMessage('endEventIdentifier')"
        :has-identifier-error="hasFieldErrors('endEventIdentifier')"
        @update-identifier="$emit('input', { field: 'endEventIdentifier', value: $event })"
      />
      <custom-stage-event-label-field
        event-type="end-event"
        :index="index"
        :field-label="$options.i18n.FORM_FIELD_END_EVENT_LABEL"
        :initial-group-labels="initialGroupLabels"
        :requires-label="endEventRequiresLabel"
        :label-error="fieldErrorMessage('endEventLabelId')"
        :has-label-error="hasFieldErrors('endEventLabelId')"
        :selected-label-ids="[stage.endEventLabelId]"
        @update-label="$emit('input', { field: 'endEventLabelId', value: $event })"
      />
    </div>
  </div>
</template>