Skip to content

Commit

Permalink
fix: [AXIMST-510] there is no message if a user upload a large file (#…
Browse files Browse the repository at this point in the history
…183)

* fix: [AXIMST-510] there is no message if a user upload a large file

* fix: after review

* fix: add translations

---------

Co-authored-by: monteri <lansevermore>
  • Loading branch information
monteri committed Apr 29, 2024
1 parent 7cc8899 commit d1f7641
Show file tree
Hide file tree
Showing 22 changed files with 58 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ export const CLIPBOARD_STATUS = {

export const STRUCTURAL_XBLOCK_TYPES = ['vertical', 'sequential', 'chapter', 'course'];

export const UPLOAD_FILE_MAX_SIZE = 100 * 1024 * 1024; // 100mb
export const UPLOAD_FILE_MAX_SIZE = 20 * 1000 * 1000; // 20mb
4 changes: 2 additions & 2 deletions src/files-and-videos/files-page/FilesPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { getFileSizeToClosestByte } from '../../utils';
import FileThumbnail from './FileThumbnail';
import FileInfoModalSidebar from './FileInfoModalSidebar';
import FileValidationModal from './FileValidationModal';
import { UPLOAD_FILE_MAX_SIZE } from '../../constants';

const FilesPage = ({
courseId,
Expand Down Expand Up @@ -90,7 +91,6 @@ const FilesPage = ({
usageErrorMessages: errorMessages.usageMetrics,
fileType: 'file',
};
const maxFileSize = 20 * 1048576;

const activeColumn = {
id: 'activeStatus',
Expand Down Expand Up @@ -205,7 +205,7 @@ const FilesPage = ({
handleErrorReset,
handleFileOrder,
tableColumns,
maxFileSize,
maxFileSize: UPLOAD_FILE_MAX_SIZE,
thumbnailPreview,
infoModalSidebar,
files: assets,
Expand Down
11 changes: 9 additions & 2 deletions src/generic/modal-dropzone/ModalDropzone.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { FileUpload as FileUploadIcon } from '@openedx/paragon/icons';

import useModalDropzone from './useModalDropzone';
import messages from './messages';
import { UPLOAD_FILE_MAX_SIZE } from '../../constants';

const ModalDropzone = ({
fileTypes,
Expand All @@ -27,7 +28,7 @@ const ModalDropzone = ({
onCancel,
onChange,
onSavingStatus,
maxSize,
maxSize = UPLOAD_FILE_MAX_SIZE,
}) => {
const {
intl,
Expand All @@ -43,6 +44,11 @@ const ModalDropzone = ({
onChange, onCancel, onClose, fileTypes, onSavingStatus,
});

const invalidSizeMore = intl.formatMessage(
messages.uploadImageDropzoneInvalidSizeMore,
{ maxSize: maxSize / (1000 * 1000) },
);

const inputComponent = previewUrl ? (
<div>
{previewComponent || (
Expand Down Expand Up @@ -94,6 +100,7 @@ const ModalDropzone = ({
onProcessUpload={handleSelectFile}
inputComponent={inputComponent}
accept={accept}
errorMessages={{ invalidSizeMore }}
validator={imageValidator}
maxSize={maxSize}
/>
Expand All @@ -120,7 +127,7 @@ ModalDropzone.defaultProps = {
imageHelpText: '',
previewComponent: null,
imageDropzoneText: '',
maxSize: Infinity,
maxSize: UPLOAD_FILE_MAX_SIZE,
};

ModalDropzone.propTypes = {
Expand Down
23 changes: 23 additions & 0 deletions src/generic/modal-dropzone/ModalDropzone.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,27 @@ describe('<ModalDropzone />', () => {

await expect(uploadAssets(courseId, fileData, () => {})).rejects.toThrow('Network Error');
});

it('displays an error message when the file size exceeds the limit', async () => {
const maxSizeInBytes = 20 * 1000 * 1000;

const { getByText, getByRole } = render(<RootWrapper {...props} maxSize={maxSizeInBytes} />);
const dropzoneInput = getByRole('presentation', { hidden: true });

const file = new File(
[new ArrayBuffer(maxSizeInBytes + 1)],
'test-file.png',
{ type: 'image/png' },
);

userEvent.upload(dropzoneInput.firstChild, file);

await waitFor(() => {
// Assert that the error message is displayed
const maxSizeInMB = maxSizeInBytes / (1000 * 1000);
const expectedErrorMessage = messages.uploadImageDropzoneInvalidSizeMore
.defaultMessage.replace('{maxSize}', maxSizeInMB);
expect(getByText(expectedErrorMessage)).toBeInTheDocument();
});
});
});
4 changes: 4 additions & 0 deletions src/generic/modal-dropzone/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const messages = defineMessages({
id: 'course-authoring.certificates.modal-dropzone.upload.modal',
defaultMessage: 'Upload',
},
uploadImageDropzoneInvalidSizeMore: {
id: 'course-authoring.certificates.modal-dropzone.validation.invalid-size-more',
defaultMessage: 'Image size must be less than {maxSize}MB.',
},
});

export default messages;
7 changes: 4 additions & 3 deletions src/generic/modal-dropzone/useModalDropzone.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ const useModalDropzone = ({
onChange(url);
onSavingStatus({ status: RequestStatus.SUCCESSFUL });
onClose();
setDisabledUploadBtn(true);
setUploadProgress(0);
setPreviewUrl(null);
}
} catch (error) {
onSavingStatus({ status: RequestStatus.FAILED });
} finally {
setDisabledUploadBtn(true);
setUploadProgress(0);
setPreviewUrl(null);
}
};

Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/de_DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/es_419.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/fa_IR.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/fr_CA.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/hi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/it_IT.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/pt_PT.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/uk.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/messages/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@
"course-authoring.certificates.modal-dropzone.image.button": "Upload signature image",
"course-authoring.certificates.modal-dropzone.cancel.modal": "Cancel",
"course-authoring.certificates.modal-dropzone.upload.modal": "Upload",
"course-authoring.certificates.modal-dropzone.validation.invalid-size-more": "Image size must be less than {maxSize}MB",
"course-authoring.certificates.details.confirm-modal": "Delete this certificate?",
"course-authoring.certificates.details.confirm-modal.message": "Deleting this certificate is permanent and cannot be undone.",
"course-authoring.certificates.details.confirm.edit": "Edit this certificate?",
Expand Down
2 changes: 0 additions & 2 deletions src/textbooks/textbook-form/TextbookForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import FormikControl from '../../generic/FormikControl';
import PromptIfDirty from '../../generic/PromptIfDirty';
import ModalDropzone from '../../generic/modal-dropzone/ModalDropzone';
import { useModel } from '../../generic/model-store';
import { UPLOAD_FILE_MAX_SIZE } from '../../constants';
import textbookFormValidationSchema from './validations';
import messages from './messages';

Expand Down Expand Up @@ -166,7 +165,6 @@ const TextbookForm = ({
previewComponent={(
<Icon src={PdfIcon} className="modal-preview-icon" />
)}
maxSize={UPLOAD_FILE_MAX_SIZE}
/>
<PromptIfDirty dirty={dirty} />
</>
Expand Down

0 comments on commit d1f7641

Please sign in to comment.