Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cache_utils.js « graphql « work_items « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 16b892b3476523ea087e6f4b61de4bc3092e1955 (plain)
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
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,
  });
};