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