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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
<script>
import { GlButton, GlTooltipDirective, GlIcon } from '@gitlab/ui';
import { produce } from 'immer';
import { s__ } from '~/locale';
import { updateGlobalTodoCount } from '~/sidebar/utils';
import groupWorkItemByIidQuery from '../graphql/group_work_item_by_iid.query.graphql';
import workItemByIidQuery from '../graphql/work_item_by_iid.query.graphql';
import createWorkItemTodosMutation from '../graphql/create_work_item_todos.mutation.graphql';
import markDoneWorkItemTodosMutation from '../graphql/mark_done_work_item_todos.mutation.graphql';
import {
TODO_ADD_ICON,
TODO_DONE_ICON,
TODO_PENDING_STATE,
TODO_DONE_STATE,
WIDGET_TYPE_CURRENT_USER_TODOS,
} from '../constants';
export default {
i18n: {
addATodo: s__('WorkItem|Add a to do'),
markAsDone: s__('WorkItem|Mark as done'),
},
directives: {
GlTooltip: GlTooltipDirective,
},
components: {
GlIcon,
GlButton,
},
inject: ['isGroup'],
props: {
workItemId: {
type: String,
required: true,
},
workItemIid: {
type: String,
required: true,
},
workItemFullpath: {
type: String,
required: true,
},
currentUserTodos: {
type: Array,
required: false,
default: () => [],
},
},
data() {
return {
isLoading: false,
buttonLabel:
this.currentUserTodos.length > 0
? this.$options.i18n.markAsDone
: this.$options.i18n.addATodo,
};
},
computed: {
todoId() {
return this.currentUserTodos[0]?.id || '';
},
pendingTodo() {
return this.todoId !== '';
},
buttonIcon() {
return this.pendingTodo ? TODO_DONE_ICON : TODO_ADD_ICON;
},
},
methods: {
onToggle() {
this.isLoading = true;
this.buttonLabel = '';
let mutation = createWorkItemTodosMutation;
let inputVariables = {
targetId: this.workItemId,
};
if (this.pendingTodo) {
mutation = markDoneWorkItemTodosMutation;
inputVariables = {
id: this.todoId,
};
}
this.$apollo
.mutate({
mutation,
variables: {
input: inputVariables,
},
optimisticResponse: {
todoMutation: {
todo: {
id: this.todoId,
state: this.pendingTodo ? TODO_DONE_STATE : TODO_PENDING_STATE,
},
errors: [],
},
},
update: (
cache,
{
data: {
todoMutation: { todo = {} },
},
},
) => {
const todos = [];
if (todo.state === TODO_PENDING_STATE) {
todos.push({
// eslint-disable-next-line @gitlab/require-i18n-strings
__typename: 'Todo',
id: todo.id,
});
}
this.updateWorkItemCurrentTodosWidgetCache({
cache,
todos,
});
},
})
.then(
({
data: {
todoMutation: { errors },
},
}) => {
if (errors?.length) {
throw new Error(errors[0]);
}
if (this.pendingTodo) {
updateGlobalTodoCount(1);
this.buttonLabel = this.$options.i18n.markAsDone;
} else {
updateGlobalTodoCount(-1);
this.buttonLabel = this.$options.i18n.addATodo;
}
},
)
.catch((error) => {
this.$emit('error', error.message);
})
.finally(() => {
this.isLoading = false;
});
},
updateWorkItemCurrentTodosWidgetCache({ cache, todos }) {
const query = {
query: this.isGroup ? groupWorkItemByIidQuery : workItemByIidQuery,
variables: { fullPath: this.workItemFullpath, iid: this.workItemIid },
};
const sourceData = cache.readQuery(query);
const newData = produce(sourceData, (draftState) => {
const { widgets } = draftState.workspace.workItems.nodes[0];
const widgetCurrentUserTodos = widgets.find(
(widget) => widget.type === WIDGET_TYPE_CURRENT_USER_TODOS,
);
widgetCurrentUserTodos.currentUserTodos.nodes = todos;
});
cache.writeQuery({ ...query, data: newData });
},
},
};
</script>
<template>
<gl-button
v-gl-tooltip.hover
:loading="isLoading"
:title="buttonLabel"
category="tertiary"
:aria-label="buttonLabel"
@click="onToggle"
>
<gl-icon :class="{ 'gl-fill-blue-500': pendingTodo }" :name="buttonIcon" />
</gl-button>
</template>
|