Commit 0f49eeb4 authored by Mike Greiling's avatar Mike Greiling

Merge branch 'winh-isEpic-getter' into 'master'

Extract isEpic getter in related items tree store

See merge request gitlab-org/gitlab!17665
parents a15e2bbc 3589b902
...@@ -256,18 +256,17 @@ export const removePendingReference = ({ commit }, data) => ...@@ -256,18 +256,17 @@ export const removePendingReference = ({ commit }, data) =>
export const setItemInputValue = ({ commit }, data) => commit(types.SET_ITEM_INPUT_VALUE, data); export const setItemInputValue = ({ commit }, data) => commit(types.SET_ITEM_INPUT_VALUE, data);
export const requestAddItem = ({ commit }) => commit(types.REQUEST_ADD_ITEM); export const requestAddItem = ({ commit }) => commit(types.REQUEST_ADD_ITEM);
export const receiveAddItemSuccess = ({ dispatch, commit, getters }, { actionType, rawItems }) => { export const receiveAddItemSuccess = ({ dispatch, commit, getters, state }, { rawItems }) => {
const isEpic = actionType === ActionType.Epic;
const items = rawItems.map(item => const items = rawItems.map(item =>
formatChildItem({ formatChildItem({
...convertObjectPropsToCamelCase(item, { deep: !isEpic }), ...convertObjectPropsToCamelCase(item, { deep: !getters.isEpic }),
type: isEpic ? ChildType.Epic : ChildType.Issue, type: getters.isEpic ? ChildType.Epic : ChildType.Issue,
userPermissions: isEpic ? { adminEpic: item.can_admin } : {}, userPermissions: getters.isEpic ? { adminEpic: item.can_admin } : {},
}), }),
); );
commit(types.RECEIVE_ADD_ITEM_SUCCESS, { commit(types.RECEIVE_ADD_ITEM_SUCCESS, {
insertAt: isEpic ? 0 : getters.issuesBeginAtIndex, insertAt: getters.isEpic ? 0 : getters.issuesBeginAtIndex,
items, items,
}); });
...@@ -281,7 +280,7 @@ export const receiveAddItemSuccess = ({ dispatch, commit, getters }, { actionTyp ...@@ -281,7 +280,7 @@ export const receiveAddItemSuccess = ({ dispatch, commit, getters }, { actionTyp
dispatch('setPendingReferences', []); dispatch('setPendingReferences', []);
dispatch('setItemInputValue', ''); dispatch('setItemInputValue', '');
dispatch('toggleAddItemForm', { dispatch('toggleAddItemForm', {
actionType, actionType: state.actionType,
toggleState: false, toggleState: false,
}); });
}; };
...@@ -294,16 +293,15 @@ export const receiveAddItemFailure = ({ commit, state }, data = {}) => { ...@@ -294,16 +293,15 @@ export const receiveAddItemFailure = ({ commit, state }, data = {}) => {
} }
flash(errorMessage); flash(errorMessage);
}; };
export const addItem = ({ state, dispatch }) => { export const addItem = ({ state, dispatch, getters }) => {
dispatch('requestAddItem'); dispatch('requestAddItem');
axios axios
.post(state.actionType === ActionType.Epic ? state.epicsEndpoint : state.issuesEndpoint, { .post(getters.isEpic ? state.epicsEndpoint : state.issuesEndpoint, {
issuable_references: state.pendingReferences, issuable_references: state.pendingReferences,
}) })
.then(({ data }) => { .then(({ data }) => {
dispatch('receiveAddItemSuccess', { dispatch('receiveAddItemSuccess', {
actionType: state.actionType,
// Newly added item is always first in the list // Newly added item is always first in the list
rawItems: data.issuables.slice(0, state.pendingReferences.length), rawItems: data.issuables.slice(0, state.pendingReferences.length),
}); });
...@@ -314,14 +312,10 @@ export const addItem = ({ state, dispatch }) => { ...@@ -314,14 +312,10 @@ export const addItem = ({ state, dispatch }) => {
}; };
export const requestCreateItem = ({ commit }) => commit(types.REQUEST_CREATE_ITEM); export const requestCreateItem = ({ commit }) => commit(types.REQUEST_CREATE_ITEM);
export const receiveCreateItemSuccess = ( export const receiveCreateItemSuccess = ({ state, commit, dispatch, getters }, { rawItem }) => {
{ state, commit, dispatch, getters },
{ actionType, rawItem },
) => {
const isEpic = actionType === ActionType.Epic;
const item = formatChildItem({ const item = formatChildItem({
...convertObjectPropsToCamelCase(rawItem, { deep: !isEpic }), ...convertObjectPropsToCamelCase(rawItem, { deep: !getters.isEpic }),
type: isEpic ? ChildType.Epic : ChildType.Issue, type: getters.isEpic ? ChildType.Epic : ChildType.Issue,
// This is needed since Rails API to create Epic // This is needed since Rails API to create Epic
// doesn't return global ID, we can remove this // doesn't return global ID, we can remove this
// change once create epic action is moved to // change once create epic action is moved to
...@@ -343,7 +337,7 @@ export const receiveCreateItemSuccess = ( ...@@ -343,7 +337,7 @@ export const receiveCreateItemSuccess = (
}); });
dispatch('toggleCreateEpicForm', { dispatch('toggleCreateEpicForm', {
actionType, actionType: state.actionType,
toggleState: false, toggleState: false,
}); });
}; };
...@@ -368,10 +362,7 @@ export const createItem = ({ state, dispatch }, { itemTitle }) => { ...@@ -368,10 +362,7 @@ export const createItem = ({ state, dispatch }, { itemTitle }) => {
created_at: '', created_at: '',
}); });
dispatch('receiveCreateItemSuccess', { dispatch('receiveCreateItemSuccess', { rawItem: data });
actionType: state.actionType,
rawItem: data,
});
}) })
.catch(() => { .catch(() => {
dispatch('receiveCreateItemFailure'); dispatch('receiveCreateItemFailure');
......
...@@ -27,7 +27,7 @@ export const issuesBeginAtIndex = (state, getters) => ...@@ -27,7 +27,7 @@ export const issuesBeginAtIndex = (state, getters) =>
getters.directChildren.findIndex(item => item.type === ChildType.Issue); getters.directChildren.findIndex(item => item.type === ChildType.Issue);
export const itemAutoCompleteSources = (state, getters) => { export const itemAutoCompleteSources = (state, getters) => {
if (state.actionType === ActionType.Epic) { if (getters.isEpic) {
return state.autoCompleteEpics ? getters.autoCompleteSources : {}; return state.autoCompleteEpics ? getters.autoCompleteSources : {};
} }
return state.autoCompleteIssues ? getters.autoCompleteSources : {}; return state.autoCompleteIssues ? getters.autoCompleteSources : {};
...@@ -45,8 +45,10 @@ export const issuableType = state => { ...@@ -45,8 +45,10 @@ export const issuableType = state => {
return null; return null;
}; };
export const itemPathIdSeparator = state => export const itemPathIdSeparator = (state, getters) =>
state.actionType === ActionType.Epic ? PathIdSeparator.Epic : PathIdSeparator.Issue; getters.isEpic ? PathIdSeparator.Epic : PathIdSeparator.Issue;
export const isEpic = state => state.actionType === ActionType.Epic;
// prevent babel-plugin-rewire from generating an invalid default during karma tests // prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {}; export default () => {};
...@@ -113,6 +113,7 @@ describe('RelatedItemsTree', () => { ...@@ -113,6 +113,7 @@ describe('RelatedItemsTree', () => {
it('returns autoCompleteSources value when `actionType` is set to `Epic` and `autoCompleteEpics` is true', () => { it('returns autoCompleteSources value when `actionType` is set to `Epic` and `autoCompleteEpics` is true', () => {
const mockGetter = { const mockGetter = {
autoCompleteSources: 'foo', autoCompleteSources: 'foo',
isEpic: true,
}; };
state.actionType = ActionType.Epic; state.actionType = ActionType.Epic;
state.autoCompleteEpics = true; state.autoCompleteEpics = true;
...@@ -155,16 +156,23 @@ describe('RelatedItemsTree', () => { ...@@ -155,16 +156,23 @@ describe('RelatedItemsTree', () => {
}); });
describe('itemPathIdSeparator', () => { describe('itemPathIdSeparator', () => {
it('returns string containing pathIdSeparator for `Epic` when `state.actionType` is set to `Epic`', () => { it('returns string containing pathIdSeparator for `Epic` when isEpic is truee', () => {
state.actionType = ActionType.Epic; expect(getters.itemPathIdSeparator({}, { isEpic: true })).toBe('&');
expect(getters.itemPathIdSeparator(state)).toBe('&');
}); });
it('returns string containing pathIdSeparator for `Issue` when `state.actionType` is set to `Issue`', () => { it('returns string containing pathIdSeparator for `Issue` when isEpic is false', () => {
state.actionType = ActionType.Issue; expect(getters.itemPathIdSeparator({}, { isEpic: false })).toBe('#');
});
});
expect(getters.itemPathIdSeparator(state)).toBe('#'); describe('isEpic', () => {
it.each`
actionType | expectedValue
${null} | ${false}
${ActionType.Issue} | ${false}
${ActionType.Epic} | ${true}
`('for actionType = $actionType is $expectedValue', ({ actionType, expectedValue }) => {
expect(getters.isEpic({ actionType })).toBe(expectedValue);
}); });
}); });
}); });
......
...@@ -806,6 +806,9 @@ describe('RelatedItemTree', () => { ...@@ -806,6 +806,9 @@ describe('RelatedItemTree', () => {
describe('receiveAddItemSuccess', () => { describe('receiveAddItemSuccess', () => {
it('should set `state.itemAddInProgress` to false and dispatches actions `setPendingReferences`, `setItemInputValue` and `toggleAddItemForm`', done => { it('should set `state.itemAddInProgress` to false and dispatches actions `setPendingReferences`, `setItemInputValue` and `toggleAddItemForm`', done => {
state.epicsBeginAtIndex = 0; state.epicsBeginAtIndex = 0;
state.actionType = ActionType.Epic;
state.isEpic = true;
const mockEpicsWithoutPerm = mockEpics.map(item => const mockEpicsWithoutPerm = mockEpics.map(item =>
Object.assign({}, item, { Object.assign({}, item, {
pathIdSeparator: PathIdSeparator.Epic, pathIdSeparator: PathIdSeparator.Epic,
...@@ -815,7 +818,7 @@ describe('RelatedItemTree', () => { ...@@ -815,7 +818,7 @@ describe('RelatedItemTree', () => {
testAction( testAction(
actions.receiveAddItemSuccess, actions.receiveAddItemSuccess,
{ actionType: ActionType.Epic, rawItems: mockEpicsWithoutPerm }, { rawItems: mockEpicsWithoutPerm },
state, state,
[ [
{ {
...@@ -906,6 +909,7 @@ describe('RelatedItemTree', () => { ...@@ -906,6 +909,7 @@ describe('RelatedItemTree', () => {
state.actionType = ActionType.Epic; state.actionType = ActionType.Epic;
state.epicsEndpoint = '/foo/bar'; state.epicsEndpoint = '/foo/bar';
state.pendingReferences = ['foo']; state.pendingReferences = ['foo'];
state.isEpic = true;
mock.onPost(state.epicsEndpoint).replyOnce(200, { issuables: [mockEpic1] }); mock.onPost(state.epicsEndpoint).replyOnce(200, { issuables: [mockEpic1] });
...@@ -920,7 +924,7 @@ describe('RelatedItemTree', () => { ...@@ -920,7 +924,7 @@ describe('RelatedItemTree', () => {
}, },
{ {
type: 'receiveAddItemSuccess', type: 'receiveAddItemSuccess',
payload: { actionType: ActionType.Epic, rawItems: [mockEpic1] }, payload: { rawItems: [mockEpic1] },
}, },
], ],
done, done,
...@@ -976,10 +980,12 @@ describe('RelatedItemTree', () => { ...@@ -976,10 +980,12 @@ describe('RelatedItemTree', () => {
state.parentItem = { state.parentItem = {
fullPath: createdEpic.group.fullPath, fullPath: createdEpic.group.fullPath,
}; };
state.actionType = ActionType.Epic;
state.isEpic = true;
testAction( testAction(
actions.receiveCreateItemSuccess, actions.receiveCreateItemSuccess,
{ rawItem: mockEpic1, actionType: ActionType.Epic }, { rawItem: mockEpic1 },
state, state,
[ [
{ {
...@@ -1068,7 +1074,6 @@ describe('RelatedItemTree', () => { ...@@ -1068,7 +1074,6 @@ describe('RelatedItemTree', () => {
{ {
type: 'receiveCreateItemSuccess', type: 'receiveCreateItemSuccess',
payload: { payload: {
actionType: ActionType.Epic,
rawItem: Object.assign({}, mockEpic1, { rawItem: Object.assign({}, mockEpic1, {
path: '', path: '',
state: ChildState.Open, state: ChildState.Open,
......
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