2020-05-24 23:13:21 +05:30
|
|
|
/* eslint-disable @gitlab/require-i18n-strings */
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
import { differenceBy } from 'lodash';
|
2020-11-24 15:15:51 +05:30
|
|
|
import produce from 'immer';
|
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';
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
const designsOf = data => data.project.issue.designCollection.designs;
|
|
|
|
|
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
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
const data = produce(sourceData, draftData => {
|
|
|
|
const changedDesigns = designsOf(sourceData).nodes.filter(
|
|
|
|
design => !selectedDesigns.includes(design.filename),
|
|
|
|
);
|
|
|
|
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
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
const data = produce(sourceData, draftData => {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
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
|
|
|
|
|
|
|
const data = produce(sourceData, draftData => {
|
|
|
|
const design = extractDesign(draftData);
|
|
|
|
design.notesCount += 1;
|
|
|
|
design.discussions.nodes = [...design.discussions.nodes, newDiscussion];
|
|
|
|
|
|
|
|
if (
|
|
|
|
!design.issue.participants.nodes.some(
|
|
|
|
participant => participant.username === createImageDiffNote.note.author.username,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
const data = produce(sourceData, draftData => {
|
|
|
|
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
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
const data = produce(sourceData, draftData => {
|
|
|
|
const currentDesigns = extractDesigns(draftData);
|
2021-01-03 14:25:43 +05:30
|
|
|
const difference = differenceBy(designManagementUpload.designs, currentDesigns, 'filename');
|
|
|
|
|
|
|
|
const newDesigns = currentDesigns
|
|
|
|
.map(design => {
|
|
|
|
return designManagementUpload.designs.find(d => d.filename === design.filename) || design;
|
|
|
|
})
|
|
|
|
.concat(difference);
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
let newVersionNode;
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
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);
|
|
|
|
|
|
|
|
const data = produce(sourceData, draftData => {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = produce(sourceData, draftData => {
|
|
|
|
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;
|
|
|
|
const data = produce(sourceData, draftData => {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|