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

299 lines
7.8 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
/* eslint-disable @gitlab/require-i18n-strings */
2020-11-24 15:15:51 +05:30
import produce from 'immer';
2021-03-11 19:13:27 +05:30
import { differenceBy } from 'lodash';
2021-01-29 00:20:46 +05:30
import createFlash from '~/flash';
2020-11-24 15:15:51 +05:30
import { extractCurrentDiscussion, extractDesign, extractDesigns } from './design_management_utils';
2020-05-24 23:13:21 +05:30
import {
ADD_IMAGE_DIFF_NOTE_ERROR,
UPDATE_IMAGE_DIFF_NOTE_ERROR,
2020-11-24 15:15:51 +05:30
DELETE_DESIGN_TODO_ERROR,
2020-05-24 23:13:21 +05:30
designDeletionError,
} from './error_messages';
2021-03-08 18:12:59 +05:30
const designsOf = (data) => data.project.issue.designCollection.designs;
2020-11-24 15:15:51 +05:30
2020-05-24 23:13:21 +05:30
const deleteDesignsFromStore = (store, query, selectedDesigns) => {
2020-11-24 15:15:51 +05:30
const sourceData = store.readQuery(query);
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
const data = produce(sourceData, (draftData) => {
2020-11-24 15:15:51 +05:30
const changedDesigns = designsOf(sourceData).nodes.filter(
2021-03-08 18:12:59 +05:30
(design) => !selectedDesigns.includes(design.filename),
2020-11-24 15:15:51 +05:30
);
designsOf(draftData).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;
2020-11-24 15:15:51 +05:30
const sourceData = store.readQuery(query);
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
const data = produce(sourceData, (draftData) => {
2020-11-24 15:15:51 +05:30
draftData.project.issue.designCollection.versions.nodes = [
version,
...draftData.project.issue.designCollection.versions.nodes,
];
});
2020-05-24 23:13:21 +05:30
store.writeQuery({
...query,
data,
});
};
const addImageDiffNoteToStore = (store, createImageDiffNote, query, variables) => {
2020-11-24 15:15:51 +05:30
const sourceData = store.readQuery({
2020-05-24 23:13:21 +05:30
query,
variables,
});
2020-11-24 15:15:51 +05:30
2020-05-24 23:13:21 +05:30
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],
},
};
2020-11-24 15:15:51 +05:30
2021-03-08 18:12:59 +05:30
const data = produce(sourceData, (draftData) => {
2020-11-24 15:15:51 +05:30
const design = extractDesign(draftData);
design.notesCount += 1;
design.discussions.nodes = [...design.discussions.nodes, newDiscussion];
if (
!design.issue.participants.nodes.some(
2021-03-08 18:12:59 +05:30
(participant) => participant.username === createImageDiffNote.note.author.username,
2020-11-24 15:15:51 +05:30
)
) {
design.issue.participants.nodes = [
...design.issue.participants.nodes,
{
__typename: 'User',
...createImageDiffNote.note.author,
},
];
}
});
2020-05-24 23:13:21 +05:30
store.writeQuery({
query,
variables,
2020-11-24 15:15:51 +05:30
data,
2020-05-24 23:13:21 +05:30
});
};
2021-01-29 00:20:46 +05:30
const updateImageDiffNoteInStore = (store, repositionImageDiffNote, query, variables) => {
2020-11-24 15:15:51 +05:30
const sourceData = store.readQuery({
2020-05-24 23:13:21 +05:30
query,
variables,
});
2021-03-08 18:12:59 +05:30
const data = produce(sourceData, (draftData) => {
2020-11-24 15:15:51 +05:30
const design = extractDesign(draftData);
const discussion = extractCurrentDiscussion(
design.discussions,
2021-01-29 00:20:46 +05:30
repositionImageDiffNote.note.discussion.id,
2020-11-24 15:15:51 +05:30
);
discussion.notes = {
...discussion.notes,
2021-01-29 00:20:46 +05:30
nodes: [repositionImageDiffNote.note, ...discussion.notes.nodes.slice(1)],
2020-11-24 15:15:51 +05:30
};
});
2020-05-24 23:13:21 +05:30
store.writeQuery({
query,
variables,
2020-11-24 15:15:51 +05:30
data,
2020-05-24 23:13:21 +05:30
});
};
const addNewDesignToStore = (store, designManagementUpload, query) => {
2020-11-24 15:15:51 +05:30
const sourceData = store.readQuery(query);
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
const data = produce(sourceData, (draftData) => {
2020-11-24 15:15:51 +05:30
const currentDesigns = extractDesigns(draftData);
2021-01-03 14:25:43 +05:30
const difference = differenceBy(designManagementUpload.designs, currentDesigns, 'filename');
const newDesigns = currentDesigns
2021-03-08 18:12:59 +05:30
.map((design) => {
return designManagementUpload.designs.find((d) => d.filename === design.filename) || design;
2021-01-03 14:25:43 +05:30
})
.concat(difference);
2020-05-24 23:13:21 +05:30
2020-11-24 15:15:51 +05:30
let newVersionNode;
2021-03-08 18:12:59 +05:30
const findNewVersions = designManagementUpload.designs.find((design) => design.versions);
2020-05-24 23:13:21 +05:30
2020-11-24 15:15:51 +05:30
if (findNewVersions) {
const findNewVersionsNodes = findNewVersions.versions.nodes;
2020-05-24 23:13:21 +05:30
2020-11-24 15:15:51 +05:30
if (findNewVersionsNodes && findNewVersionsNodes.length) {
newVersionNode = [findNewVersionsNodes[0]];
}
2020-05-24 23:13:21 +05:30
}
2020-11-24 15:15:51 +05:30
const newVersions = [
...(newVersionNode || []),
...draftData.project.issue.designCollection.versions.nodes,
];
2020-05-24 23:13:21 +05:30
2020-11-24 15:15:51 +05:30
const updatedDesigns = {
__typename: 'DesignCollection',
2021-01-03 14:25:43 +05:30
copyState: 'READY',
2020-11-24 15:15:51 +05:30
designs: {
__typename: 'DesignConnection',
nodes: newDesigns,
},
versions: {
__typename: 'DesignVersionConnection',
nodes: newVersions,
},
};
draftData.project.issue.designCollection = updatedDesigns;
});
2020-05-24 23:13:21 +05:30
store.writeQuery({
...query,
data,
});
};
2020-10-24 23:57:45 +05:30
const moveDesignInStore = (store, designManagementMove, query) => {
2020-11-24 15:15:51 +05:30
const sourceData = store.readQuery(query);
2021-03-08 18:12:59 +05:30
const data = produce(sourceData, (draftData) => {
2020-11-24 15:15:51 +05:30
draftData.project.issue.designCollection.designs =
designManagementMove.designCollection.designs;
});
2020-10-24 23:57:45 +05:30
store.writeQuery({
...query,
data,
});
};
2020-11-24 15:15:51 +05:30
export const addPendingTodoToStore = (store, pendingTodo, query, queryVariables) => {
const sourceData = store.readQuery({
query,
variables: queryVariables,
});
2021-03-08 18:12:59 +05:30
const data = produce(sourceData, (draftData) => {
2020-11-24 15:15:51 +05:30
const design = extractDesign(draftData);
const existingTodos = design.currentUserTodos?.nodes || [];
const newTodoNodes = [...existingTodos, { ...pendingTodo, __typename: 'Todo' }];
if (!design.currentUserTodos) {
design.currentUserTodos = {
__typename: 'TodoConnection',
nodes: newTodoNodes,
};
} else {
design.currentUserTodos.nodes = newTodoNodes;
}
});
store.writeQuery({ query, variables: queryVariables, data });
};
export const deletePendingTodoFromStore = (store, todoMarkDone, query, queryVariables) => {
const sourceData = store.readQuery({
query,
variables: queryVariables,
});
const {
todo: { id: todoId },
} = todoMarkDone;
2021-03-08 18:12:59 +05:30
const data = produce(sourceData, (draftData) => {
2020-11-24 15:15:51 +05:30
const design = extractDesign(draftData);
const existingTodos = design.currentUserTodos?.nodes || [];
design.currentUserTodos.nodes = existingTodos.filter(({ id }) => id !== todoId);
});
store.writeQuery({ query, variables: queryVariables, data });
};
2020-05-24 23:13:21 +05:30
const onError = (data, message) => {
2021-01-29 00:20:46 +05:30
createFlash({ message });
2020-05-24 23:13:21 +05:30
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 updateStoreAfterAddImageDiffNote = (store, data, query, queryVariables) => {
if (hasErrors(data)) {
onError(data, ADD_IMAGE_DIFF_NOTE_ERROR);
} else {
addImageDiffNoteToStore(store, data, query, queryVariables);
}
};
2021-01-29 00:20:46 +05:30
export const updateStoreAfterRepositionImageDiffNote = (store, data, query, queryVariables) => {
2020-05-24 23:13:21 +05:30
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)) {
2021-01-29 00:20:46 +05:30
createFlash({ message: data.errors[0] });
2020-10-24 23:57:45 +05:30
} else {
moveDesignInStore(store, data, query);
}
};
2020-11-24 15:15:51 +05:30
export const updateStoreAfterDeleteDesignTodo = (store, data, query, queryVariables) => {
if (hasErrors(data)) {
onError(data, DELETE_DESIGN_TODO_ERROR);
} else {
deletePendingTodoFromStore(store, data, query, queryVariables);
}
};