debian-mirror-gitlab/app/assets/javascripts/design_management/utils/cache_update.js

283 lines
6.9 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
/* eslint-disable @gitlab/require-i18n-strings */
2020-10-24 23:57:45 +05:30
import { groupBy } from 'lodash';
import { deprecatedCreateFlash as createFlash } from '~/flash';
2020-05-24 23:13:21 +05:30
import { extractCurrentDiscussion, extractDesign } from './design_management_utils';
import {
ADD_IMAGE_DIFF_NOTE_ERROR,
UPDATE_IMAGE_DIFF_NOTE_ERROR,
ADD_DISCUSSION_COMMENT_ERROR,
designDeletionError,
} from './error_messages';
const deleteDesignsFromStore = (store, query, selectedDesigns) => {
const data = store.readQuery(query);
2020-10-24 23:57:45 +05:30
const changedDesigns = data.project.issue.designCollection.designs.nodes.filter(
node => !selectedDesigns.includes(node.filename),
2020-05-24 23:13:21 +05:30
);
2020-10-24 23:57:45 +05:30
data.project.issue.designCollection.designs.nodes = [...changedDesigns];
2020-05-24 23:13:21 +05:30
store.writeQuery({
...query,
data,
});
};
/**
* Adds a new version of designs to store
*
* @param {Object} store
* @param {Object} query
* @param {Object} version
*/
const addNewVersionToStore = (store, query, version) => {
if (!version) return;
const data = store.readQuery(query);
2020-10-24 23:57:45 +05:30
data.project.issue.designCollection.versions.nodes = [
version,
...data.project.issue.designCollection.versions.nodes,
2020-05-24 23:13:21 +05:30
];
store.writeQuery({
...query,
data,
});
};
const addDiscussionCommentToStore = (store, createNote, query, queryVariables, discussionId) => {
const data = store.readQuery({
query,
variables: queryVariables,
});
const design = extractDesign(data);
const currentDiscussion = extractCurrentDiscussion(design.discussions, discussionId);
currentDiscussion.notes.nodes = [...currentDiscussion.notes.nodes, createNote.note];
design.notesCount += 1;
if (
2020-10-24 23:57:45 +05:30
!design.issue.participants.nodes.some(
participant => participant.username === createNote.note.author.username,
2020-05-24 23:13:21 +05:30
)
) {
2020-10-24 23:57:45 +05:30
design.issue.participants.nodes = [
...design.issue.participants.nodes,
2020-05-24 23:13:21 +05:30
{
2020-10-24 23:57:45 +05:30
__typename: 'User',
...createNote.note.author,
2020-05-24 23:13:21 +05:30
},
];
}
store.writeQuery({
query,
variables: queryVariables,
data: {
...data,
design: {
...design,
},
},
});
};
const addImageDiffNoteToStore = (store, createImageDiffNote, query, variables) => {
const data = store.readQuery({
query,
variables,
});
const newDiscussion = {
__typename: 'Discussion',
id: createImageDiffNote.note.discussion.id,
replyId: createImageDiffNote.note.discussion.replyId,
2020-06-23 00:09:42 +05:30
resolvable: true,
resolved: false,
resolvedAt: null,
resolvedBy: null,
2020-05-24 23:13:21 +05:30
notes: {
__typename: 'NoteConnection',
nodes: [createImageDiffNote.note],
},
};
const design = extractDesign(data);
const notesCount = design.notesCount + 1;
design.discussions.nodes = [...design.discussions.nodes, newDiscussion];
if (
2020-10-24 23:57:45 +05:30
!design.issue.participants.nodes.some(
participant => participant.username === createImageDiffNote.note.author.username,
2020-05-24 23:13:21 +05:30
)
) {
2020-10-24 23:57:45 +05:30
design.issue.participants.nodes = [
...design.issue.participants.nodes,
2020-05-24 23:13:21 +05:30
{
2020-10-24 23:57:45 +05:30
__typename: 'User',
...createImageDiffNote.note.author,
2020-05-24 23:13:21 +05:30
},
];
}
store.writeQuery({
query,
variables,
data: {
...data,
design: {
...design,
notesCount,
},
},
});
};
const updateImageDiffNoteInStore = (store, updateImageDiffNote, query, variables) => {
const data = store.readQuery({
query,
variables,
});
const design = extractDesign(data);
const discussion = extractCurrentDiscussion(
design.discussions,
updateImageDiffNote.note.discussion.id,
);
discussion.notes = {
...discussion.notes,
nodes: [updateImageDiffNote.note, ...discussion.notes.nodes.slice(1)],
};
store.writeQuery({
query,
variables,
data: {
...data,
design,
},
});
};
const addNewDesignToStore = (store, designManagementUpload, query) => {
const data = store.readQuery(query);
2020-10-24 23:57:45 +05:30
const currentDesigns = data.project.issue.designCollection.designs.nodes;
const existingDesigns = groupBy(currentDesigns, 'filename');
const newDesigns = currentDesigns.concat(
designManagementUpload.designs.filter(d => !existingDesigns[d.filename]),
);
2020-05-24 23:13:21 +05:30
let newVersionNode;
const findNewVersions = designManagementUpload.designs.find(design => design.versions);
if (findNewVersions) {
2020-10-24 23:57:45 +05:30
const findNewVersionsNodes = findNewVersions.versions.nodes;
2020-05-24 23:13:21 +05:30
2020-10-24 23:57:45 +05:30
if (findNewVersionsNodes && findNewVersionsNodes.length) {
newVersionNode = [findNewVersionsNodes[0]];
2020-05-24 23:13:21 +05:30
}
}
const newVersions = [
...(newVersionNode || []),
2020-10-24 23:57:45 +05:30
...data.project.issue.designCollection.versions.nodes,
2020-05-24 23:13:21 +05:30
];
const updatedDesigns = {
__typename: 'DesignCollection',
designs: {
__typename: 'DesignConnection',
2020-10-24 23:57:45 +05:30
nodes: newDesigns,
2020-05-24 23:13:21 +05:30
},
versions: {
__typename: 'DesignVersionConnection',
2020-10-24 23:57:45 +05:30
nodes: newVersions,
2020-05-24 23:13:21 +05:30
},
};
data.project.issue.designCollection = updatedDesigns;
store.writeQuery({
...query,
data,
});
};
2020-10-24 23:57:45 +05:30
const moveDesignInStore = (store, designManagementMove, query) => {
const data = store.readQuery(query);
data.project.issue.designCollection.designs = designManagementMove.designCollection.designs;
store.writeQuery({
...query,
data,
});
};
2020-05-24 23:13:21 +05:30
const onError = (data, message) => {
createFlash(message);
throw new Error(data.errors);
};
export const hasErrors = ({ errors = [] }) => errors?.length;
/**
* Updates a store after design deletion
*
* @param {Object} store
* @param {Object} data
* @param {Object} query
* @param {Array} designs
*/
export const updateStoreAfterDesignsDelete = (store, data, query, designs) => {
if (hasErrors(data)) {
onError(data, designDeletionError({ singular: designs.length === 1 }));
} else {
deleteDesignsFromStore(store, query, designs);
addNewVersionToStore(store, query, data.version);
}
};
export const updateStoreAfterAddDiscussionComment = (
store,
data,
query,
queryVariables,
discussionId,
) => {
if (hasErrors(data)) {
onError(data, ADD_DISCUSSION_COMMENT_ERROR);
} else {
addDiscussionCommentToStore(store, data, query, queryVariables, discussionId);
}
};
export const updateStoreAfterAddImageDiffNote = (store, data, query, queryVariables) => {
if (hasErrors(data)) {
onError(data, ADD_IMAGE_DIFF_NOTE_ERROR);
} else {
addImageDiffNoteToStore(store, data, query, queryVariables);
}
};
export const updateStoreAfterUpdateImageDiffNote = (store, data, query, queryVariables) => {
if (hasErrors(data)) {
onError(data, UPDATE_IMAGE_DIFF_NOTE_ERROR);
} else {
updateImageDiffNoteInStore(store, data, query, queryVariables);
}
};
export const updateStoreAfterUploadDesign = (store, data, query) => {
if (hasErrors(data)) {
onError(data, data.errors[0]);
} else {
addNewDesignToStore(store, data, query);
}
};
2020-10-24 23:57:45 +05:30
export const updateDesignsOnStoreAfterReorder = (store, data, query) => {
if (hasErrors(data)) {
createFlash(data.errors[0]);
} else {
moveDesignInStore(store, data, query);
}
};