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

183 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
import { uniqueId } from 'lodash';
import { VALID_DESIGN_FILE_MIMETYPE } from '../constants';
export const isValidDesignFile = ({ type }) =>
(type.match(VALID_DESIGN_FILE_MIMETYPE.regex) || []).length > 0;
/**
2020-10-24 23:57:45 +05:30
* Returns formatted array of discussions
2020-05-24 23:13:21 +05:30
*
* @param {Array} discussions
*/
2021-03-08 18:12:59 +05:30
export const extractDiscussions = (discussions) =>
2020-06-23 00:09:42 +05:30
discussions.nodes.map((discussion, index) => ({
2020-05-24 23:13:21 +05:30
...discussion,
2020-06-23 00:09:42 +05:30
index: index + 1,
2020-05-24 23:13:21 +05:30
notes: discussion.notes.nodes,
}));
/**
* Returns a discussion with the given id from discussions array
*
* @param {Array} discussions
*/
export const extractCurrentDiscussion = (discussions, id) =>
2021-03-08 18:12:59 +05:30
discussions.nodes.find((discussion) => discussion.id === id);
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
export const findVersionId = (id) => (id.match('::Version/(.+$)') || [])[1];
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
export const findNoteId = (id) => (id.match('DiffNote/(.+$)') || [])[1];
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
export const findIssueId = (id) => (id.match('Issue/(.+$)') || [])[1];
2020-11-24 15:15:51 +05:30
2021-03-08 18:12:59 +05:30
export const findDesignId = (id) => (id.match('Design/(.+$)') || [])[1];
2020-11-24 15:15:51 +05:30
2021-03-08 18:12:59 +05:30
export const extractDesigns = (data) => data.project.issue.designCollection.designs.nodes;
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
export const extractDesign = (data) => (extractDesigns(data) || [])[0];
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
export const toDiffNoteGid = (noteId) => `gid://gitlab/DiffNote/${noteId}`;
2020-11-24 15:15:51 +05:30
/**
* Return the note ID from a URL hash parameter
* @param {String} urlHash URL hash, including `#` prefix
*/
2021-03-08 18:12:59 +05:30
export const extractDesignNoteId = (urlHash) => {
2020-11-24 15:15:51 +05:30
const [, noteId] = urlHash.match('#note_([0-9]+$)') || [];
return noteId || null;
};
2020-05-24 23:13:21 +05:30
/**
* Generates optimistic response for a design upload mutation
* @param {Array<File>} files
*/
2021-03-08 18:12:59 +05:30
export const designUploadOptimisticResponse = (files) => {
const designs = files.map((file) => ({
2020-05-24 23:13:21 +05:30
// False positive i18n lint: https://gitlab.com/gitlab-org/frontend/eslint-plugin-i18n/issues/26
// eslint-disable-next-line @gitlab/require-i18n-strings
__typename: 'Design',
id: -uniqueId(),
image: '',
imageV432x230: '',
filename: file.name,
fullPath: '',
notesCount: 0,
event: 'NONE',
2021-01-03 14:25:43 +05:30
currentUserTodos: {
__typename: 'TodoConnection',
nodes: [],
},
2020-05-24 23:13:21 +05:30
diffRefs: {
__typename: 'DiffRefs',
baseSha: '',
startSha: '',
headSha: '',
},
discussions: {
__typename: 'DesignDiscussion',
nodes: [],
},
versions: {
__typename: 'DesignVersionConnection',
2020-10-24 23:57:45 +05:30
nodes: {
__typename: 'DesignVersion',
id: -uniqueId(),
sha: -uniqueId(),
2021-11-11 11:23:49 +05:30
createdAt: '',
author: {
__typename: 'UserCore',
id: -uniqueId(),
name: '',
avatarUrl: '',
},
2020-05-24 23:13:21 +05:30
},
},
}));
return {
// False positive i18n lint: https://gitlab.com/gitlab-org/frontend/eslint-plugin-i18n/issues/26
// eslint-disable-next-line @gitlab/require-i18n-strings
__typename: 'Mutation',
designManagementUpload: {
__typename: 'DesignManagementUploadPayload',
designs,
skippedDesigns: [],
errors: [],
},
};
};
/**
* Generates optimistic response for a design upload mutation
2020-10-24 23:57:45 +05:30
* @param {Object} note
* @param {Object} position
2020-05-24 23:13:21 +05:30
*/
2021-01-29 00:20:46 +05:30
export const repositionImageDiffNoteOptimisticResponse = (note, { position }) => ({
2020-05-24 23:13:21 +05:30
// False positive i18n lint: https://gitlab.com/gitlab-org/frontend/eslint-plugin-i18n/issues/26
// eslint-disable-next-line @gitlab/require-i18n-strings
__typename: 'Mutation',
2021-01-29 00:20:46 +05:30
repositionImageDiffNote: {
__typename: 'RepositionImageDiffNotePayload',
2020-05-24 23:13:21 +05:30
note: {
...note,
position: {
...note.position,
...position,
},
},
errors: [],
},
});
2020-10-24 23:57:45 +05:30
/**
* Generates optimistic response for a design upload mutation
* @param {Array} designs
*/
2021-03-08 18:12:59 +05:30
export const moveDesignOptimisticResponse = (designs) => ({
2020-10-24 23:57:45 +05:30
// False positive i18n lint: https://gitlab.com/gitlab-org/frontend/eslint-plugin-i18n/issues/26
// eslint-disable-next-line @gitlab/require-i18n-strings
__typename: 'Mutation',
designManagementMove: {
__typename: 'DesignManagementMovePayload',
designCollection: {
__typename: 'DesignCollection',
designs: {
__typename: 'DesignConnection',
nodes: designs,
},
},
errors: [],
},
});
2021-03-08 18:12:59 +05:30
const normalizeAuthor = (author) => ({
2020-05-24 23:13:21 +05:30
...author,
web_url: author.webUrl,
avatar_url: author.avatarUrl,
});
2021-03-08 18:12:59 +05:30
export const extractParticipants = (users) => users.map((node) => normalizeAuthor(node));
2020-06-23 00:09:42 +05:30
export const getPageLayoutElement = () => document.querySelector('.layout-page');
2020-11-24 15:15:51 +05:30
/**
* Extract the ID of the To-Do for a given 'delete' path
* Example of todoDeletePath: /delete/1234
* @param {String} todoDeletePath delete_path from REST API response
*/
2021-03-08 18:12:59 +05:30
export const extractTodoIdFromDeletePath = (todoDeletePath) =>
2020-11-24 15:15:51 +05:30
(todoDeletePath.match('todos/([0-9]+$)') || [])[1];
2021-03-08 18:12:59 +05:30
const createTodoGid = (todoId) => {
2020-11-24 15:15:51 +05:30
return `gid://gitlab/Todo/${todoId}`;
};
2021-03-08 18:12:59 +05:30
export const createPendingTodo = (todoId) => {
2020-11-24 15:15:51 +05:30
return {
__typename: 'Todo', // eslint-disable-line @gitlab/require-i18n-strings
id: createTodoGid(todoId),
};
};