All files / app/assets/javascripts/design_management/utils design_management_utils.js

86.95% Statements 40/46
72.22% Branches 13/18
75% Functions 18/24
90% Lines 27/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185      14x 15x               14x 76x                       14x 7x   41x   91x   14x   14x   37x   37x   14x           14x 26x 26x             14x 18x                                                                                       9x                                   14x                                         14x                                 14x           14x   15x             14x     14x       14x            
import { uniqueId } from 'lodash';
import { VALID_DESIGN_FILE_MIMETYPE } from '../constants';
 
export const isValidDesignFile = ({ type }) =>
  (type.match(VALID_DESIGN_FILE_MIMETYPE.regex) || []).length > 0;
 
/**
 * Returns formatted array of discussions
 *
 * @param {Array} discussions
 */
 
export const extractDiscussions = (discussions) =>
  discussions.nodes.map((discussion, index) => ({
    ...discussion,
    index: index + 1,
    notes: discussion.notes.nodes,
  }));
 
/**
 * Returns a discussion with the given id from discussions array
 *
 * @param {Array} discussions
 */
 
export const extractCurrentDiscussion = (discussions, id) =>
  discussions.nodes.find((discussion) => discussion.id === id);
 
export const findVersionId = (id) => (id.match('::Version/(.+$)') || [])[1];
 
export const findNoteId = (id) => (id.match('DiffNote/(.+$)') || [])[1];
 
export const findIssueId = (id) => (id.match('Issue/(.+$)') || [])[1];
 
export const findDesignId = (id) => (id.match('Design/(.+$)') || [])[1];
 
export const extractDesigns = (data) => data.project.issue.designCollection.designs.nodes;
 
export const extractDesign = (data) => (extractDesigns(data) || [])[0];
 
export const toDiffNoteGid = (noteId) => `gid://gitlab/DiffNote/${noteId}`;
 
/**
 * Return the note ID from a URL hash parameter
 * @param {String} urlHash URL hash, including `#` prefix
 */
export const extractDesignNoteId = (urlHash) => {
  const [, noteId] = urlHash.match('#note_([0-9]+$)') || [];
  return noteId || null;
};
 
/**
 * Generates optimistic response for a design upload mutation
 * @param {Array<File>} files
 */
export const designUploadOptimisticResponse = (files) => {
  const designs = files.map((file) => ({
    // 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: '',
    description: '',
    descriptionHtml: '',
    filename: file.name,
    fullPath: '',
    notesCount: 0,
    event: 'NONE',
    currentUserTodos: {
      __typename: 'TodoConnection',
      nodes: [],
    },
    diffRefs: {
      __typename: 'DiffRefs',
      baseSha: '',
      startSha: '',
      headSha: '',
    },
    discussions: {
      __typename: 'DesignDiscussion',
      nodes: [],
    },
    versions: {
      __typename: 'DesignVersionConnection',
      nodes: {
        __typename: 'DesignVersion',
        id: -uniqueId(),
        sha: -uniqueId(),
        createdAt: '',
        author: {
          __typename: 'UserCore',
          id: -uniqueId(),
          name: '',
          avatarUrl: '',
        },
      },
    },
  }));
 
  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
 *  @param {Object} note
 *  @param {Object} position
 */
export const repositionImageDiffNoteOptimisticResponse = (note, { position }) => ({
  // 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',
  repositionImageDiffNote: {
    __typename: 'RepositionImageDiffNotePayload',
    note: {
      ...note,
      position: {
        ...note.position,
        ...position,
      },
    },
    errors: [],
  },
});
 
/**
 * Generates optimistic response for a design upload mutation
 * @param {Array} designs
 */
export const moveDesignOptimisticResponse = (designs) => ({
  // 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: [],
  },
});
 
const normalizeAuthor = (author) => ({
  ...author,
  web_url: author.webUrl,
  avatar_url: author.avatarUrl,
});
 
export const extractParticipants = (users) => users.map((node) => normalizeAuthor(node));
 
export const getPageLayoutElement = () => document.querySelector('.layout-page');
 
/**
 * 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
 */
export const extractTodoIdFromDeletePath = (todoDeletePath) =>
  (todoDeletePath.match('todos/([0-9]+$)') || [])[1];
 
const createTodoGid = (todoId) => {
  return `gid://gitlab/Todo/${todoId}`;
};
 
export const createPendingTodo = (todoId) => {
  return {
    __typename: 'Todo', // eslint-disable-line @gitlab/require-i18n-strings
    id: createTodoGid(todoId),
  };
};