Commit f6da547b authored by Natalia Tepluhina's avatar Natalia Tepluhina Committed by Phil Hughes

Refactor Apollo cache updates to be immutable in Design Management

parent c8ace7cd
...@@ -20,6 +20,7 @@ const defaultClient = createDefaultClient( ...@@ -20,6 +20,7 @@ const defaultClient = createDefaultClient(
return defaultDataIdFromObject(object); return defaultDataIdFromObject(object);
}, },
}, },
assumeImmutableResults: true,
}, },
); );
......
/* eslint no-param-reassign: ["error", { "props": false }] */
import produce from 'immer';
import createFlash from '~/flash'; import createFlash from '~/flash';
import { extractCurrentDiscussion, extractDesign } from './design_management_utils'; import { extractCurrentDiscussion, extractDesign } from './design_management_utils';
import { import {
...@@ -7,13 +10,20 @@ import { ...@@ -7,13 +10,20 @@ import {
designDeletionError, designDeletionError,
} from './error_messages'; } from './error_messages';
const designsOf = data => data.project.issue.designCollection.designs;
const isParticipating = (design, username) =>
design.issue.participants.edges.some(participant => participant.node.username === username);
const deleteDesignsFromStore = (store, query, selectedDesigns) => { const deleteDesignsFromStore = (store, query, selectedDesigns) => {
const data = store.readQuery(query); const sourceData = store.readQuery(query);
const changedDesigns = data.project.issue.designCollection.designs.edges.filter( const data = produce(sourceData, draftData => {
const changedDesigns = designsOf(sourceData).edges.filter(
({ node }) => !selectedDesigns.includes(node.filename), ({ node }) => !selectedDesigns.includes(node.filename),
); );
data.project.issue.designCollection.designs.edges = [...changedDesigns]; designsOf(draftData).edges = [...changedDesigns];
});
store.writeQuery({ store.writeQuery({
...query, ...query,
...@@ -31,13 +41,13 @@ const deleteDesignsFromStore = (store, query, selectedDesigns) => { ...@@ -31,13 +41,13 @@ const deleteDesignsFromStore = (store, query, selectedDesigns) => {
const addNewVersionToStore = (store, query, version) => { const addNewVersionToStore = (store, query, version) => {
if (!version) return; if (!version) return;
const data = store.readQuery(query); const sourceData = store.readQuery(query);
const newEdge = { node: version, __typename: 'DesignVersionEdge' };
data.project.issue.designCollection.versions.edges = [ const newVersion = { node: version, __typename: 'DesignVersionEdge' };
newEdge,
...data.project.issue.designCollection.versions.edges, const data = produce(sourceData, draftData => {
]; draftData.project.issue.designCollection.versions.edges.unshift(newVersion);
});
store.writeQuery({ store.writeQuery({
...query, ...query,
...@@ -46,56 +56,48 @@ const addNewVersionToStore = (store, query, version) => { ...@@ -46,56 +56,48 @@ const addNewVersionToStore = (store, query, version) => {
}; };
const addDiscussionCommentToStore = (store, createNote, query, queryVariables, discussionId) => { const addDiscussionCommentToStore = (store, createNote, query, queryVariables, discussionId) => {
const data = store.readQuery({ const sourceData = store.readQuery({
query, query,
variables: queryVariables, variables: queryVariables,
}); });
const design = extractDesign(data); const newParticipant = {
const currentDiscussion = extractCurrentDiscussion(design.discussions, discussionId);
currentDiscussion.node.notes.edges = [
...currentDiscussion.node.notes.edges,
{
__typename: 'NoteEdge',
node: createNote.note,
},
];
design.notesCount += 1;
if (
!design.issue.participants.edges.some(
participant => participant.node.username === createNote.note.author.username,
)
) {
design.issue.participants.edges = [
...design.issue.participants.edges,
{
__typename: 'UserEdge', __typename: 'UserEdge',
node: { node: {
// eslint-disable-next-line @gitlab/i18n/no-non-i18n-strings // eslint-disable-next-line @gitlab/i18n/no-non-i18n-strings
__typename: 'User', __typename: 'User',
...createNote.note.author, ...createNote.note.author,
}, },
}, };
];
const data = produce(sourceData, draftData => {
const design = extractDesign(draftData);
const currentDiscussion = extractCurrentDiscussion(design.discussions, discussionId);
currentDiscussion.node.notes.edges.push({
__typename: 'NoteEdge',
node: createNote.note,
});
if (!isParticipating(design, createNote.note.author.username)) {
design.issue.participants.edges.push(newParticipant);
} }
design.notesCount += 1;
});
store.writeQuery({ store.writeQuery({
query, query,
variables: queryVariables, variables: queryVariables,
data: { data,
...data,
design: {
...design,
},
},
}); });
}; };
const addImageDiffNoteToStore = (store, createImageDiffNote, query, variables) => { const addImageDiffNoteToStore = (store, createImageDiffNote, query, variables) => {
const data = store.readQuery({ const sourceData = store.readQuery({
query, query,
variables, variables,
}); });
const newDiscussion = { const newDiscussion = {
__typename: 'DiscussionEdge', __typename: 'DiscussionEdge',
node: { node: {
...@@ -115,43 +117,38 @@ const addImageDiffNoteToStore = (store, createImageDiffNote, query, variables) = ...@@ -115,43 +117,38 @@ const addImageDiffNoteToStore = (store, createImageDiffNote, query, variables) =
}, },
}, },
}; };
const design = extractDesign(data);
const notesCount = design.notesCount + 1; const newParticipant = {
design.discussions.edges = [...design.discussions.edges, newDiscussion];
if (
!design.issue.participants.edges.some(
participant => participant.node.username === createImageDiffNote.note.author.username,
)
) {
design.issue.participants.edges = [
...design.issue.participants.edges,
{
__typename: 'UserEdge', __typename: 'UserEdge',
node: { node: {
// eslint-disable-next-line @gitlab/i18n/no-non-i18n-strings // eslint-disable-next-line @gitlab/i18n/no-non-i18n-strings
__typename: 'User', __typename: 'User',
...createImageDiffNote.note.author, ...createImageDiffNote.note.author,
}, },
}, };
];
const data = produce(sourceData, draftData => {
const design = extractDesign(draftData);
design.discussions.edges.push(newDiscussion);
if (!isParticipating(design, createImageDiffNote.note.author.username)) {
design.issue.participants.edges.push(newParticipant);
} }
design.notesCount += 1;
});
store.writeQuery({ store.writeQuery({
query, query,
variables, variables,
data: { data,
...data,
design: {
...design,
notesCount,
},
},
}); });
}; };
const addNewDesignToStore = (store, designManagementUpload, query) => { const addNewDesignToStore = (store, designManagementUpload, query) => {
const data = store.readQuery(query); const sourceData = store.readQuery(query);
const newDesigns = data.project.issue.designCollection.designs.edges.reduce((acc, design) => { const newDesigns = designsOf(sourceData).edges.reduce((acc, design) => {
if (!acc.find(d => d.filename === design.node.filename)) { if (!acc.find(d => d.filename === design.node.filename)) {
acc.push(design.node); acc.push(design.node);
} }
...@@ -172,7 +169,7 @@ const addNewDesignToStore = (store, designManagementUpload, query) => { ...@@ -172,7 +169,7 @@ const addNewDesignToStore = (store, designManagementUpload, query) => {
const newVersions = [ const newVersions = [
...(newVersionNode || []), ...(newVersionNode || []),
...data.project.issue.designCollection.versions.edges, ...sourceData.project.issue.designCollection.versions.edges,
]; ];
const updatedDesigns = { const updatedDesigns = {
...@@ -190,7 +187,9 @@ const addNewDesignToStore = (store, designManagementUpload, query) => { ...@@ -190,7 +187,9 @@ const addNewDesignToStore = (store, designManagementUpload, query) => {
}, },
}; };
data.project.issue.designCollection = updatedDesigns; const data = produce(sourceData, draftData => {
draftData.project.issue.designCollection = updatedDesigns;
});
store.writeQuery({ store.writeQuery({
...query, ...query,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment