summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/design_management/utils/error_messages.js
blob: e7b2c814bb3a88361926a61659405196f7562264 (plain)
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
import { __, s__, n__, sprintf } from '~/locale';

export const ADD_DISCUSSION_COMMENT_ERROR = s__(
  'DesignManagement|Could not add a new comment. Please try again.',
);

export const ADD_IMAGE_DIFF_NOTE_ERROR = s__(
  'DesignManagement|Could not create new discussion. Please try again.',
);

export const UPDATE_IMAGE_DIFF_NOTE_ERROR = s__(
  'DesignManagement|Could not update discussion. Please try again.',
);

export const UPDATE_NOTE_ERROR = s__('DesignManagement|Could not update note. Please try again.');

export const UPLOAD_DESIGN_ERROR = s__(
  'DesignManagement|Error uploading a new design. Please try again.',
);

export const UPLOAD_DESIGN_INVALID_FILETYPE_ERROR = __(
  'Could not upload your designs as one or more files uploaded are not supported.',
);

export const DESIGN_NOT_FOUND_ERROR = __('Could not find design.');

export const DESIGN_VERSION_NOT_EXIST_ERROR = __('Requested design version does not exist.');

const DESIGN_UPLOAD_SKIPPED_MESSAGE = s__('DesignManagement|Upload skipped.');

const ALL_DESIGNS_SKIPPED_MESSAGE = `${DESIGN_UPLOAD_SKIPPED_MESSAGE} ${s__(
  'The designs you tried uploading did not change.',
)}`;

export const EXISTING_DESIGN_DROP_MANY_FILES_MESSAGE = __(
  'You can only upload one design when dropping onto an existing design.',
);

export const EXISTING_DESIGN_DROP_INVALID_FILENAME_MESSAGE = __(
  'You must upload a file with the same file name when dropping onto an existing design.',
);

export const MOVE_DESIGN_ERROR = __(
  'Something went wrong when reordering designs. Please try again',
);

export const CREATE_DESIGN_TODO_ERROR = __('Failed to create a to-do item for the design.');

export const CREATE_DESIGN_TODO_EXISTS_ERROR = __('There is already a to-do item for this design.');

export const DELETE_DESIGN_TODO_ERROR = __('Failed to remove a to-do item for the design.');

export const TOGGLE_TODO_ERROR = __('Failed to toggle the to-do status for the design.');

const MAX_SKIPPED_FILES_LISTINGS = 5;

const oneDesignSkippedMessage = (filename) =>
  `${DESIGN_UPLOAD_SKIPPED_MESSAGE} ${sprintf(s__('DesignManagement|%{filename} did not change.'), {
    filename,
  })}`;

/**
 * Return warning message indicating that some (but not all) uploaded
 * files were skipped.
 * @param {Array<{ filename }>} skippedFiles
 */
const someDesignsSkippedMessage = (skippedFiles) => {
  const designsSkippedMessage = `${DESIGN_UPLOAD_SKIPPED_MESSAGE} ${s__(
    'Some of the designs you tried uploading did not change:',
  )}`;

  const moreText = sprintf(s__(`DesignManagement|and %{moreCount} more.`), {
    moreCount: skippedFiles.length - MAX_SKIPPED_FILES_LISTINGS,
  });

  return `${designsSkippedMessage} ${skippedFiles
    .slice(0, MAX_SKIPPED_FILES_LISTINGS)
    .map(({ filename }) => filename)
    .join(', ')}${skippedFiles.length > MAX_SKIPPED_FILES_LISTINGS ? `, ${moreText}` : '.'}`;
};

export const designDeletionError = ({ singular = true } = {}) => {
  const design = singular ? __('a design') : __('designs');
  return sprintf(s__('Could not archive %{design}. Please try again.'), {
    design,
  });
};

/**
 * Return warning message, if applicable, that one, some or all uploaded
 * files were skipped.
 * @param {Array<{ filename }>} uploadedDesigns
 * @param {Array<{ filename }>} skippedFiles
 */
export const designUploadSkippedWarning = (uploadedDesigns, skippedFiles) => {
  if (skippedFiles.length === 0) {
    return null;
  }

  if (skippedFiles.length === uploadedDesigns.length) {
    const { filename } = skippedFiles[0];

    return n__(oneDesignSkippedMessage(filename), ALL_DESIGNS_SKIPPED_MESSAGE, skippedFiles.length);
  }

  return someDesignsSkippedMessage(skippedFiles);
};