Commit 98fb79db authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Use separate arrays for hidden stages

Cleans up the hidden and active stages
in the form
parent 8219c379
...@@ -71,13 +71,7 @@ export const defaultFields = { ...@@ -71,13 +71,7 @@ export const defaultFields = {
endEventLabelId: null, endEventLabelId: null,
}; };
const defaultStageCommonFields = { custom: false, hidden: false }; export const defaultCustomStageFields = { ...defaultFields, custom: true };
export const defaultCustomStageFields = {
...defaultFields,
...defaultStageCommonFields,
custom: true,
};
/** /**
* These stage configs are copied from the https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/cycle_analytics * These stage configs are copied from the https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/cycle_analytics
...@@ -120,7 +114,7 @@ const BASE_DEFAULT_STAGE_CONFIG = [ ...@@ -120,7 +114,7 @@ const BASE_DEFAULT_STAGE_CONFIG = [
export const DEFAULT_STAGE_CONFIG = BASE_DEFAULT_STAGE_CONFIG.map(({ id, ...rest }) => ({ export const DEFAULT_STAGE_CONFIG = BASE_DEFAULT_STAGE_CONFIG.map(({ id, ...rest }) => ({
...rest, ...rest,
...defaultStageCommonFields, custom: false,
name: capitalizeFirstCharacter(id), name: capitalizeFirstCharacter(id),
})); }));
......
...@@ -17,9 +17,6 @@ import { validateValueStreamName, validateStage } from './create_value_stream_fo ...@@ -17,9 +17,6 @@ import { validateValueStreamName, validateStage } from './create_value_stream_fo
import DefaultStageFields from './create_value_stream_form/default_stage_fields.vue'; import DefaultStageFields from './create_value_stream_form/default_stage_fields.vue';
import CustomStageFields from './create_value_stream_form/custom_stage_fields.vue'; import CustomStageFields from './create_value_stream_form/custom_stage_fields.vue';
const findStageIndexByName = (stages, target = '') =>
stages.findIndex(({ name }) => name === target);
const initializeStageErrors = (selectedPreset = PRESET_OPTIONS_DEFAULT) => const initializeStageErrors = (selectedPreset = PRESET_OPTIONS_DEFAULT) =>
selectedPreset === PRESET_OPTIONS_DEFAULT ? DEFAULT_STAGE_CONFIG.map(() => ({})) : [{}]; selectedPreset === PRESET_OPTIONS_DEFAULT ? DEFAULT_STAGE_CONFIG.map(() => ({})) : [{}];
...@@ -30,12 +27,11 @@ const initializeStages = (selectedPreset = PRESET_OPTIONS_DEFAULT) => ...@@ -30,12 +27,11 @@ const initializeStages = (selectedPreset = PRESET_OPTIONS_DEFAULT) =>
const formatStageDataForSubmission = (stages) => { const formatStageDataForSubmission = (stages) => {
return stages.map(({ custom = false, name, ...rest }) => { return stages.map(({ custom = false, name, ...rest }) => {
const additionalProps = custom ? convertObjectPropsToSnakeCase({ ...rest }) : {}; return custom
return { ? convertObjectPropsToSnakeCase({ ...rest, custom, name })
...additionalProps, : {
custom, name,
name, };
};
}); });
}; };
...@@ -85,6 +81,7 @@ export default { ...@@ -85,6 +81,7 @@ export default {
: { stages: [], nameError }; : { stages: [], nameError };
return { return {
hiddenStages: [],
selectedPreset: initialPreset, selectedPreset: initialPreset,
presetOptions: PRESET_OPTIONS, presetOptions: PRESET_OPTIONS,
name: '', name: '',
...@@ -127,12 +124,6 @@ export default { ...@@ -127,12 +124,6 @@ export default {
], ],
}; };
}, },
hiddenStages() {
return this.stages.filter((stage) => stage.hidden);
},
activeStages() {
return this.stages.filter((stage) => !stage.hidden);
},
hasFormErrors() { hasFormErrors() {
return Boolean( return Boolean(
this.nameError.length || this.stageErrors.some((obj) => Object.keys(obj).length), this.nameError.length || this.stageErrors.some((obj) => Object.keys(obj).length),
...@@ -177,7 +168,7 @@ export default { ...@@ -177,7 +168,7 @@ export default {
return sprintf(this.$options.I18N.HIDDEN_DEFAULT_STAGE, { name }); return sprintf(this.$options.I18N.HIDDEN_DEFAULT_STAGE, { name });
}, },
validateStages() { validateStages() {
return this.activeStages.map(validateStage); return this.stages.map(validateStage);
}, },
validate() { validate() {
const { name } = this; const { name } = this;
...@@ -196,14 +187,15 @@ export default { ...@@ -196,14 +187,15 @@ export default {
Vue.set(this, 'stages', newStages); Vue.set(this, 'stages', newStages);
}, },
validateStageFields(index) { validateStageFields(index) {
Vue.set(this.stageErrors, index, validateStage(this.activeStages[index])); Vue.set(this.stageErrors, index, validateStage(this.stages[index]));
}, },
fieldErrors(index) { fieldErrors(index) {
return this.stageErrors && this.stageErrors[index] ? this.stageErrors[index] : {}; return this.stageErrors && this.stageErrors[index] ? this.stageErrors[index] : {};
}, },
onHide(index) { onHide(index) {
const stage = this.stages[index]; const target = this.stages[index];
Vue.set(this.stages, index, { ...stage, hidden: true }); Vue.set(this, 'stages', [...this.stages.filter((_, i) => i !== index)]);
Vue.set(this, 'hiddenStages', [...this.hiddenStages, target]);
}, },
onRemove(index) { onRemove(index) {
const newErrors = this.stageErrors.filter((_, idx) => idx !== index); const newErrors = this.stageErrors.filter((_, idx) => idx !== index);
...@@ -212,9 +204,11 @@ export default { ...@@ -212,9 +204,11 @@ export default {
Vue.set(this, 'stageErrors', [...newErrors]); Vue.set(this, 'stageErrors', [...newErrors]);
}, },
onRestore(hiddenStageIndex) { onRestore(hiddenStageIndex) {
const stage = this.hiddenStages[hiddenStageIndex]; const target = this.hiddenStages[hiddenStageIndex];
const stageIndex = findStageIndexByName(this.stages, stage.name); Vue.set(this, 'hiddenStages', [
Vue.set(this.stages, stageIndex, { ...stage, hidden: false }); ...this.hiddenStages.filter((_, i) => i !== hiddenStageIndex),
]);
Vue.set(this, 'stages', [...this.stages, target]);
}, },
onAddStage() { onAddStage() {
// validate previous stages only and add a new stage // validate previous stages only and add a new stage
...@@ -297,7 +291,7 @@ export default { ...@@ -297,7 +291,7 @@ export default {
@input="onSelectPreset" @input="onSelectPreset"
/> />
<div v-if="hasExtendedFormFields" data-testid="extended-form-fields"> <div v-if="hasExtendedFormFields" data-testid="extended-form-fields">
<div v-for="(stage, activeStageIndex) in activeStages" :key="stageKey(activeStageIndex)"> <div v-for="(stage, activeStageIndex) in stages" :key="stageKey(activeStageIndex)">
<hr class="gl-my-3" /> <hr class="gl-my-3" />
<span <span
class="gl-display-flex gl-m-0 gl-vertical-align-middle gl-mr-2 gl-font-weight-bold gl-display-flex gl-pb-3" class="gl-display-flex gl-m-0 gl-vertical-align-middle gl-mr-2 gl-font-weight-bold gl-display-flex gl-pb-3"
...@@ -308,7 +302,7 @@ export default { ...@@ -308,7 +302,7 @@ export default {
:stage="stage" :stage="stage"
:stage-events="formEvents" :stage-events="formEvents"
:index="activeStageIndex" :index="activeStageIndex"
:total-stages="activeStages.length" :total-stages="stages.length"
:errors="fieldErrors(activeStageIndex)" :errors="fieldErrors(activeStageIndex)"
@move="handleMove" @move="handleMove"
@remove="onRemove" @remove="onRemove"
...@@ -319,7 +313,7 @@ export default { ...@@ -319,7 +313,7 @@ export default {
:stage="stage" :stage="stage"
:stage-events="formEvents" :stage-events="formEvents"
:index="activeStageIndex" :index="activeStageIndex"
:total-stages="activeStages.length" :total-stages="stages.length"
:errors="fieldErrors(activeStageIndex)" :errors="fieldErrors(activeStageIndex)"
@move="handleMove" @move="handleMove"
@hide="onHide" @hide="onHide"
......
...@@ -66,7 +66,6 @@ export default { ...@@ -66,7 +66,6 @@ export default {
}, },
[types.RECEIVE_CREATE_STAGE_ERROR](state) { [types.RECEIVE_CREATE_STAGE_ERROR](state) {
state.isSavingCustomStage = false; state.isSavingCustomStage = false;
// ??
state.isCreatingCustomStage = false; state.isCreatingCustomStage = false;
state.isEditingCustomStage = false; state.isEditingCustomStage = false;
}, },
......
...@@ -122,10 +122,9 @@ export default { ...@@ -122,10 +122,9 @@ export default {
state.createValueStreamErrors = {}; state.createValueStreamErrors = {};
}, },
[types.RECEIVE_CREATE_VALUE_STREAM_ERROR](state, { data: { stages = [] }, errors = {} }) { [types.RECEIVE_CREATE_VALUE_STREAM_ERROR](state, { data: { stages = [] }, errors = {} }) {
state.isCreatingValueStream = false;
const { stages: stageErrors = {}, ...rest } = errors; const { stages: stageErrors = {}, ...rest } = errors;
state.createValueStreamErrors = { ...rest, stages: prepareStageErrors(stages, stageErrors) }; state.createValueStreamErrors = { ...rest, stages: prepareStageErrors(stages, stageErrors) };
state.isCreatingValueStream = false;
}, },
[types.RECEIVE_CREATE_VALUE_STREAM_SUCCESS](state, valueStream) { [types.RECEIVE_CREATE_VALUE_STREAM_SUCCESS](state, valueStream) {
state.isCreatingValueStream = false; state.isCreatingValueStream = false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment