diff options
Diffstat (limited to 'app/assets/javascripts/work_items/graphql/cache_utils.js')
-rw-r--r-- | app/assets/javascripts/work_items/graphql/cache_utils.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/app/assets/javascripts/work_items/graphql/cache_utils.js b/app/assets/javascripts/work_items/graphql/cache_utils.js new file mode 100644 index 00000000000..16b892b3476 --- /dev/null +++ b/app/assets/javascripts/work_items/graphql/cache_utils.js @@ -0,0 +1,62 @@ +import { produce } from 'immer'; +import { WIDGET_TYPE_NOTES } from '~/work_items/constants'; +import { getWorkItemNotesQuery } from '~/work_items/utils'; + +/** + * Updates the cache manually when adding a main comment + * + * @param store + * @param createNoteData + * @param fetchByIid + * @param queryVariables + * @param sortOrder + */ +export const updateCommentState = (store, { data: { createNote } }, fetchByIid, queryVariables) => { + const notesQuery = getWorkItemNotesQuery(fetchByIid); + const variables = { + ...queryVariables, + pageSize: 100, + }; + const sourceData = store.readQuery({ + query: notesQuery, + variables, + }); + + const finalData = produce(sourceData, (draftData) => { + const notesWidget = fetchByIid + ? draftData.workspace.workItems.nodes[0].widgets.find( + (widget) => widget.type === WIDGET_TYPE_NOTES, + ) + : draftData.workItem.widgets.find((widget) => widget.type === WIDGET_TYPE_NOTES); + + // as notes are currently sorted/reversed on the frontend rather than in the query + // we only ever push. + // const arrayPushMethod = sortOrder === ASC ? 'push' : 'unshift'; + const arrayPushMethod = 'push'; + + // manual update of cache with a completely new discussion + if (createNote.note.discussion.notes.nodes.length === 1) { + notesWidget.discussions.nodes[arrayPushMethod]({ + id: createNote.note.discussion.id, + notes: { + nodes: createNote.note.discussion.notes.nodes, + __typename: 'NoteConnection', + }, + // eslint-disable-next-line @gitlab/require-i18n-strings + __typename: 'Discussion', + }); + } + + if (fetchByIid) { + draftData.workspace.workItems.nodes[0].widgets[6] = notesWidget; + } else { + draftData.workItem.widgets[6] = notesWidget; + } + }); + + store.writeQuery({ + query: notesQuery, + variables, + data: finalData, + }); +}; |