Commit 5fc7fb78 authored by Florie Guibert's avatar Florie Guibert

Consolidate labels widget architecture

Review feedback
parent 3c079000
...@@ -102,19 +102,19 @@ export default { ...@@ -102,19 +102,19 @@ export default {
handleClose() { handleClose() {
this.toggleBoardItem({ boardItem: this.activeBoardItem, sidebarType: this.sidebarType }); this.toggleBoardItem({ boardItem: this.activeBoardItem, sidebarType: this.sidebarType });
}, },
handleUpdateSelectedLabels(input) { handleUpdateSelectedLabels(labels) {
this.setActiveBoardItemLabels({ this.setActiveBoardItemLabels({
iid: this.activeBoardItem.iid, iid: this.activeBoardItem.iid,
projectPath: this.projectPathForActiveIssue, projectPath: this.projectPathForActiveIssue,
labelsIds: input.map((label) => getIdFromGraphQLId(label.id)), labelIds: labels.map((label) => getIdFromGraphQLId(label.id)),
labels: input, labels,
}); });
}, },
handleLabelRemove(input) { handleLabelRemove(removeLabelId) {
this.setActiveBoardItemLabels({ this.setActiveBoardItemLabels({
iid: this.activeBoardItem.iid, iid: this.activeBoardItem.iid,
projectPath: this.projectPathForActiveIssue, projectPath: this.projectPathForActiveIssue,
removeLabelIds: [input], removeLabelIds: [removeLabelId],
}); });
}, },
}, },
......
...@@ -664,7 +664,8 @@ export default { ...@@ -664,7 +664,8 @@ export default {
variables: { variables: {
input: { input: {
iid: input.iid || String(activeBoardItem.iid), iid: input.iid || String(activeBoardItem.iid),
labelsIds: input.labelsIds ?? [], labelIds: input.labelsId ?? undefined,
addLabelIds: input.addLabelIds ?? [],
removeLabelIds: input.removeLabelIds ?? [], removeLabelIds: input.removeLabelIds ?? [],
projectPath: input.projectPath, projectPath: input.projectPath,
}, },
...@@ -680,19 +681,21 @@ export default { ...@@ -680,19 +681,21 @@ export default {
prop: 'labels', prop: 'labels',
value: data.updateIssue?.issue?.labels.nodes, value: data.updateIssue?.issue?.labels.nodes,
}); });
} else {
let labels = input?.labels || []; return;
if (input.removeLabelIds) { }
labels = activeBoardItem.labels.filter(
(label) => input.removeLabelIds[0] !== getIdFromGraphQLId(label.id), let labels = input?.labels || [];
); if (input.removeLabelIds) {
} labels = activeBoardItem.labels.filter(
commit(types.UPDATE_BOARD_ITEM_BY_ID, { (label) => input.removeLabelIds[0] !== getIdFromGraphQLId(label.id),
itemId: activeBoardItem.id, );
prop: 'labels',
value: labels,
});
} }
commit(types.UPDATE_BOARD_ITEM_BY_ID, {
itemId: activeBoardItem.id,
prop: 'labels',
value: labels,
});
}, },
setActiveItemSubscribed: async ({ commit, getters, state }, input) => { setActiveItemSubscribed: async ({ commit, getters, state }, input) => {
......
...@@ -132,9 +132,8 @@ export default { ...@@ -132,9 +132,8 @@ export default {
}; };
}, },
update(data) { update(data) {
const labels = data.workspace?.issuable?.labels.nodes || []; this.selected = data.workspace?.issuable?.labels.nodes || [];
this.selected = labels; return this.selected;
return labels;
}, },
error() { error() {
createFlash({ message: __('Error fetching labels.') }); createFlash({ message: __('Error fetching labels.') });
...@@ -145,9 +144,10 @@ export default { ...@@ -145,9 +144,10 @@ export default {
handleDropdownClose(labels) { handleDropdownClose(labels) {
if (this.iid !== '') { if (this.iid !== '') {
this.updateSelectedLabels(this.getUpdateVariables(labels)); this.updateSelectedLabels(this.getUpdateVariables(labels));
} else {
this.$emit('updateSelectedLabels', labels);
} }
this.$emit('updateSelectedLabels', labels);
this.collapseEditableItem(); this.collapseEditableItem();
}, },
collapseEditableItem() { collapseEditableItem() {
...@@ -193,6 +193,8 @@ export default { ...@@ -193,6 +193,8 @@ export default {
if (data[mutationName]?.errors?.length) { if (data[mutationName]?.errors?.length) {
throw new Error(); throw new Error();
} }
this.$emit('updateSelectedLabels', data[mutationName]?.[this.issuableType].labels?.nodes);
}) })
.catch(() => createFlash({ message: __('An error occurred while updating labels.') })) .catch(() => createFlash({ message: __('An error occurred while updating labels.') }))
.finally(() => { .finally(() => {
......
...@@ -27,6 +27,7 @@ import issueCreateMutation from '~/boards/graphql/issue_create.mutation.graphql' ...@@ -27,6 +27,7 @@ import issueCreateMutation from '~/boards/graphql/issue_create.mutation.graphql'
import actions from '~/boards/stores/actions'; import actions from '~/boards/stores/actions';
import * as types from '~/boards/stores/mutation_types'; import * as types from '~/boards/stores/mutation_types';
import mutations from '~/boards/stores/mutations'; import mutations from '~/boards/stores/mutations';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import { import {
mockLists, mockLists,
...@@ -1572,7 +1573,7 @@ describe('setActiveIssueLabels', () => { ...@@ -1572,7 +1573,7 @@ describe('setActiveIssueLabels', () => {
const getters = { activeBoardItem: mockIssue }; const getters = { activeBoardItem: mockIssue };
const testLabelIds = labels.map((label) => label.id); const testLabelIds = labels.map((label) => label.id);
const input = { const input = {
labelsIds: testLabelIds, labelIds: testLabelIds,
removeLabelIds: [], removeLabelIds: [],
projectPath: 'h/b', projectPath: 'h/b',
labels, labels,
...@@ -1611,6 +1612,64 @@ describe('setActiveIssueLabels', () => { ...@@ -1611,6 +1612,64 @@ describe('setActiveIssueLabels', () => {
await expect(actions.setActiveIssueLabels({ getters }, input)).rejects.toThrow(Error); await expect(actions.setActiveIssueLabels({ getters }, input)).rejects.toThrow(Error);
}); });
describe('labels_widget FF on', () => {
beforeEach(() => {
window.gon = {
features: { labelsWidget: true },
};
getters.activeBoardItem = { ...mockIssue, labels };
});
afterEach(() => {
window.gon = {
features: {},
};
});
it('should assign labels', () => {
const payload = {
itemId: getters.activeBoardItem.id,
prop: 'labels',
value: labels,
};
testAction(
actions.setActiveIssueLabels,
input,
{ ...state, ...getters },
[
{
type: types.UPDATE_BOARD_ITEM_BY_ID,
payload,
},
],
[],
);
});
it('should remove label', () => {
const payload = {
itemId: getters.activeBoardItem.id,
prop: 'labels',
value: [labels[1]],
};
testAction(
actions.setActiveIssueLabels,
{ ...input, removeLabelIds: [getIdFromGraphQLId(labels[0].id)] },
{ ...state, ...getters },
[
{
type: types.UPDATE_BOARD_ITEM_BY_ID,
payload,
},
],
[],
);
});
});
}); });
describe('setActiveItemSubscribed', () => { describe('setActiveItemSubscribed', () => {
......
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