Commit 13d0fd61 authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Added action to set default selected stage

Ensures that a hidden stage can not be
selected for the default stage and that
only active stages are used
parent 5a43aa01
...@@ -42,20 +42,14 @@ export const receiveStageDataError = ({ commit }) => { ...@@ -42,20 +42,14 @@ export const receiveStageDataError = ({ commit }) => {
createFlash(__('There was an error fetching data for the selected stage')); createFlash(__('There was an error fetching data for the selected stage'));
}; };
export const fetchStageData = ({ state, dispatch, getters }) => { export const fetchStageData = ({ state, dispatch, getters }, slug) => {
const { cycleAnalyticsRequestParams = {} } = getters; const { cycleAnalyticsRequestParams = {} } = getters;
const { const {
selectedGroup: { fullPath }, selectedGroup: { fullPath },
stages,
} = state; } = state;
dispatch('requestStageData'); dispatch('requestStageData');
const [firstStage] = stages;
const { slug } = firstStage;
dispatch('setSelectedStage', firstStage);
return Api.cycleAnalyticsStageEvents(fullPath, slug, cycleAnalyticsRequestParams) return Api.cycleAnalyticsStageEvents(fullPath, slug, cycleAnalyticsRequestParams)
.then(({ data }) => dispatch('receiveStageDataSuccess', data)) .then(({ data }) => dispatch('receiveStageDataSuccess', data))
.catch(error => dispatch('receiveStageDataError', error)); .catch(error => dispatch('receiveStageDataError', error));
...@@ -135,18 +129,22 @@ export const receiveGroupStagesError = ({ commit }, error) => { ...@@ -135,18 +129,22 @@ export const receiveGroupStagesError = ({ commit }, error) => {
createFlash(__('There was an error fetching value stream analytics stages.')); createFlash(__('There was an error fetching value stream analytics stages.'));
}; };
export const receiveGroupStagesSuccess = ({ commit, dispatch }, stages) => { export const setDefaultSelectedStage = ({ dispatch, getters }) => {
commit(types.RECEIVE_GROUP_STAGES_SUCCESS, stages); const { activeStages = [] } = getters;
if (stages.length) { if (activeStages && activeStages.length) {
// console.log('receiveGroupStagesSuccess::stages', stages); const [firstActiveStage] = activeStages;
// const [firstStage] = stages; dispatch('setSelectedStage', firstActiveStage);
// dispatch('setSelectedStage', firstStage); dispatch('fetchStageData', firstActiveStage.slug);
dispatch('fetchStageData');
} else { } else {
createFlash(__('There was an error while fetching value stream analytics data.')); createFlash(__('There was an error while fetching value stream analytics data.'));
} }
}; };
export const receiveGroupStagesSuccess = ({ commit, dispatch }, stages) => {
commit(types.RECEIVE_GROUP_STAGES_SUCCESS, stages);
dispatch('setDefaultSelectedStage');
};
export const fetchGroupStagesAndEvents = ({ state, dispatch, getters }) => { export const fetchGroupStagesAndEvents = ({ state, dispatch, getters }) => {
const { const {
selectedGroup: { fullPath }, selectedGroup: { fullPath },
......
// eslint-disable-next-line import/prefer-default-export // eslint-disable-next-line import/prefer-default-export
export const customStageFormActive = ({ isCreating, isEditing }) => export const customStageFormActive = ({ isCreatingCustomStage, isEditingCustomStage }) =>
Boolean(isCreating || isEditing); Boolean(isCreatingCustomStage || isEditingCustomStage);
...@@ -277,7 +277,7 @@ describe('Cycle analytics actions', () => { ...@@ -277,7 +277,7 @@ describe('Cycle analytics actions', () => {
setFixtures('<div class="flash-container"></div>'); setFixtures('<div class="flash-container"></div>');
}); });
it('removes an existing flash error if present', done => { it('removes an existing flash error if present', () => {
const { mockDispatchContext } = mockFetchCycleAnalyticsAction(); const { mockDispatchContext } = mockFetchCycleAnalyticsAction();
createFlash(flashErrorMessage); createFlash(flashErrorMessage);
...@@ -285,7 +285,7 @@ describe('Cycle analytics actions', () => { ...@@ -285,7 +285,7 @@ describe('Cycle analytics actions', () => {
expect(flashAlert).toBeVisible(); expect(flashAlert).toBeVisible();
actions return actions
.fetchCycleAnalyticsData({ .fetchCycleAnalyticsData({
dispatch: mockDispatchContext, dispatch: mockDispatchContext,
state: {}, state: {},
...@@ -293,26 +293,25 @@ describe('Cycle analytics actions', () => { ...@@ -293,26 +293,25 @@ describe('Cycle analytics actions', () => {
}) })
.then(() => { .then(() => {
expect(flashAlert.style.opacity).toBe('0'); expect(flashAlert.style.opacity).toBe('0');
done(); });
})
.catch(done.fail);
}); });
}); });
it('will flash an error when there are no stages', () => { // it('will flash an error when there are no stages', () => {
[[], null].forEach(emptyStages => { // [[], null].forEach(emptyStages => {
actions.receiveGroupStagesSuccess( // actions.receiveGroupStagesSuccess(
{ // {
commit: () => {}, // dispatch: () => {},
state: emptyStages, // commit: () => {},
getters, // state: { stages: emptyStages },
}, // getters,
{}, // },
); // {},
// );
shouldFlashAMessage(flashErrorMessage);
}); // shouldFlashAMessage(flashErrorMessage);
}); // });
// });
}); });
describe('receiveCycleAnalyticsDataError', () => { describe('receiveCycleAnalyticsDataError', () => {
...@@ -372,8 +371,8 @@ describe('Cycle analytics actions', () => { ...@@ -372,8 +371,8 @@ describe('Cycle analytics actions', () => {
setFixtures('<div class="flash-container"></div>'); setFixtures('<div class="flash-container"></div>');
}); });
it(`commits the ${types.RECEIVE_GROUP_STAGES_SUCCESS} mutation`, done => { it(`commits the ${types.RECEIVE_GROUP_STAGES_SUCCESS} mutation and dispatches 'setDefaultSelectedStage'`, () => {
testAction( return testAction(
actions.receiveGroupStagesSuccess, actions.receiveGroupStagesSuccess,
{ ...customizableStagesAndEvents.stages }, { ...customizableStagesAndEvents.stages },
state, state,
...@@ -383,36 +382,33 @@ describe('Cycle analytics actions', () => { ...@@ -383,36 +382,33 @@ describe('Cycle analytics actions', () => {
payload: { ...customizableStagesAndEvents.stages }, payload: { ...customizableStagesAndEvents.stages },
}, },
], ],
[], [{ type: 'setDefaultSelectedStage' }],
done,
); );
}); });
});
it("dispatches the 'fetchStageData' action", done => { describe.only('setDefaultSelectedStage', () => {
testAction( it("dispatches the 'fetchStageData' action", () => {
actions.receiveGroupStagesSuccess, return testAction(
stages, actions.setDefaultSelectedStage,
{}, null,
[ {
{ activeStages: stages,
type: types.RECEIVE_GROUP_STAGES_SUCCESS, },
payload: stages, [],
},
],
[ [
{ type: 'setSelectedStage', payload: selectedStage }, { type: 'setSelectedStage', payload: selectedStage },
{ type: 'fetchStageData', payload: selectedStageSlug }, { type: 'fetchStageData', payload: selectedStageSlug },
], ],
done,
); );
}); });
it('will flash an error when there are no stages', () => { it('will flash an error when there are no stages', () => {
[[], null].forEach(emptyStages => { [[], null].forEach(emptyStages => {
actions.receiveGroupStagesSuccess( actions.setDefaultSelectedStage(
{ {
commit: () => {}, getters: { activeStages: emptyStages },
state: emptyStages, dispatch: () => {},
}, },
{}, {},
); );
...@@ -1018,6 +1014,7 @@ describe('Cycle analytics actions', () => { ...@@ -1018,6 +1014,7 @@ describe('Cycle analytics actions', () => {
beforeEach(() => { beforeEach(() => {
setFixtures('<div class="flash-container"></div>'); setFixtures('<div class="flash-container"></div>');
}); });
it(`commits the ${types.RECEIVE_REORDER_STAGE_ERROR} mutation and flashes an error`, () => { it(`commits the ${types.RECEIVE_REORDER_STAGE_ERROR} mutation and flashes an error`, () => {
testAction( testAction(
actions.receiveReorderStageError, actions.receiveReorderStageError,
......
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