1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
<script>
import { GlIcon, GlDropdown, GlDropdownItem } from '@gitlab/ui';
import { produce } from 'immer';
import { s__ } from '~/locale';
import changeWorkItemParentMutation from '../../graphql/change_work_item_parent_link.mutation.graphql';
import getWorkItemLinksQuery from '../../graphql/work_item_links.query.graphql';
import { WIDGET_TYPE_HIERARCHY } from '../../constants';
export default {
components: {
GlDropdownItem,
GlDropdown,
GlIcon,
},
props: {
workItemId: {
type: String,
required: true,
},
parentWorkItemId: {
type: String,
required: true,
},
},
data() {
return {
activeToast: null,
};
},
methods: {
toggleChildFromCache(data, store) {
const sourceData = store.readQuery({
query: getWorkItemLinksQuery,
variables: { id: this.parentWorkItemId },
});
const newData = produce(sourceData, (draftState) => {
const widgetHierarchy = draftState.workItem.widgets.find(
(widget) => widget.type === WIDGET_TYPE_HIERARCHY,
);
const index = widgetHierarchy.children.nodes.findIndex(
(child) => child.id === this.workItemId,
);
if (index >= 0) {
widgetHierarchy.children.nodes.splice(index, 1);
} else {
widgetHierarchy.children.nodes.push(data.workItemUpdate.workItem);
}
});
store.writeQuery({
query: getWorkItemLinksQuery,
variables: { id: this.parentWorkItemId },
data: newData,
});
},
async addChild(data) {
const { data: resp } = await this.$apollo.mutate({
mutation: changeWorkItemParentMutation,
variables: { id: this.workItemId, parentId: this.parentWorkItemId },
update: this.toggleChildFromCache.bind(this, data),
});
if (resp.workItemUpdate.errors.length === 0) {
this.activeToast?.hide();
}
},
async removeChild() {
const { data } = await this.$apollo.mutate({
mutation: changeWorkItemParentMutation,
variables: { id: this.workItemId, parentId: null },
update: this.toggleChildFromCache.bind(this, null),
});
if (data.workItemUpdate.errors.length === 0) {
this.activeToast = this.$toast.show(s__('WorkItem|Child removed'), {
action: {
text: s__('WorkItem|Undo'),
onClick: this.addChild.bind(this, data),
},
});
}
},
},
};
</script>
<template>
<span class="gl-ml-2">
<gl-dropdown category="tertiary" toggle-class="btn-icon" :right="true">
<template #button-content>
<gl-icon name="ellipsis_v" :size="14" />
</template>
<gl-dropdown-item @click="removeChild">
{{ s__('WorkItem|Remove') }}
</gl-dropdown-item>
</gl-dropdown>
</span>
</template>
|