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
|
import { produce } from 'immer';
import { WIDGET_TYPE_NOTES } from '~/work_items/constants';
import workItemByIidQuery from '~/work_items/graphql/work_item_by_iid.query.graphql';
import { findHierarchyWidgetChildren } from '~/work_items/utils';
const isNotesWidget = (widget) => widget.type === WIDGET_TYPE_NOTES;
const getNotesWidgetFromSourceData = (draftData) =>
draftData?.workspace?.workItems?.nodes[0]?.widgets.find(isNotesWidget);
const updateNotesWidgetDataInDraftData = (draftData, notesWidget) => {
const noteWidgetIndex = draftData.workspace.workItems.nodes[0].widgets.findIndex(isNotesWidget);
draftData.workspace.workItems.nodes[0].widgets[noteWidgetIndex] = notesWidget;
};
/**
* Work Item note create subscription update query callback
*
* @param currentNotes
* @param subscriptionData
*/
export const updateCacheAfterCreatingNote = (currentNotes, subscriptionData) => {
if (!subscriptionData.data?.workItemNoteCreated) {
return currentNotes;
}
const newNote = subscriptionData.data.workItemNoteCreated;
return produce(currentNotes, (draftData) => {
const notesWidget = getNotesWidgetFromSourceData(draftData);
if (!notesWidget.discussions) {
return;
}
const discussion = notesWidget.discussions.nodes.find((d) => d.id === newNote.discussion.id);
// handle the case where discussion already exists - we don't need to do anything, update will happen automatically
if (discussion) {
return;
}
notesWidget.discussions.nodes.push(newNote.discussion);
updateNotesWidgetDataInDraftData(draftData, notesWidget);
});
};
/**
* Work Item note delete subscription update query callback
*
* @param currentNotes
* @param subscriptionData
*/
export const updateCacheAfterDeletingNote = (currentNotes, subscriptionData) => {
if (!subscriptionData.data?.workItemNoteDeleted) {
return currentNotes;
}
const deletedNote = subscriptionData.data.workItemNoteDeleted;
const { id, discussionId, lastDiscussionNote } = deletedNote;
return produce(currentNotes, (draftData) => {
const notesWidget = getNotesWidgetFromSourceData(draftData);
if (!notesWidget.discussions) {
return;
}
const discussionIndex = notesWidget.discussions.nodes.findIndex(
(discussion) => discussion.id === discussionId,
);
if (discussionIndex === -1) {
return;
}
if (lastDiscussionNote) {
notesWidget.discussions.nodes.splice(discussionIndex, 1);
} else {
const deletedThreadDiscussion = notesWidget.discussions.nodes[discussionIndex];
const deletedThreadIndex = deletedThreadDiscussion.notes.nodes.findIndex(
(note) => note.id === id,
);
deletedThreadDiscussion.notes.nodes.splice(deletedThreadIndex, 1);
notesWidget.discussions.nodes[discussionIndex] = deletedThreadDiscussion;
}
updateNotesWidgetDataInDraftData(draftData, notesWidget);
});
};
export const addHierarchyChild = (cache, fullPath, iid, workItem) => {
const queryArgs = { query: workItemByIidQuery, variables: { fullPath, iid } };
const sourceData = cache.readQuery(queryArgs);
if (!sourceData) {
return;
}
cache.writeQuery({
...queryArgs,
data: produce(sourceData, (draftState) => {
findHierarchyWidgetChildren(draftState.workspace.workItems.nodes[0]).push(workItem);
}),
});
};
export const removeHierarchyChild = (cache, fullPath, iid, workItem) => {
const queryArgs = { query: workItemByIidQuery, variables: { fullPath, iid } };
const sourceData = cache.readQuery(queryArgs);
if (!sourceData) {
return;
}
cache.writeQuery({
...queryArgs,
data: produce(sourceData, (draftState) => {
const children = findHierarchyWidgetChildren(draftState.workspace.workItems.nodes[0]);
const index = children.findIndex((child) => child.id === workItem.id);
children.splice(index, 1);
}),
});
};
|