Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed dropzone invalid file error message #3559

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/i18n/resources/en/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ Do you want to proceed?`,
dropzone: {
acceptedFilesMessage: '(Only {{acceptedExtensions}} files with a max size of {{maxSize}} will be accepted)',
error: {
fileNotValid: 'Selected file is not valid',
fileTooBig: 'Selected file is too big',
invalidFileExtension: 'Invalid file extension: {{extension}}',
},
Expand Down
42 changes: 31 additions & 11 deletions webapp/components/Dropzone/Dropzone.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ const Dropzone = (props) => {

const validateFiles = useCallback(
(files) => {
if (!files.length) {
// Dropzone component filters out the files exceeding the specified max size
return i18n.t('dropzone.error.fileTooBig')
}
if (acceptProp && typeof acceptProp === 'object') {
// 'accept' is specified and files have been accepted successfully
return null
}
const invalidExtension = files
.map(FileUtils.getExtension)
.find((extension) => !acceptedExtensions.includes(extension))
Expand All @@ -63,24 +55,52 @@ const Dropzone = (props) => {
}
return null
},
[acceptProp, acceptedExtensions]
[acceptedExtensions, i18n]
)

const onDrop = useCallback(
const onDropAccepted = useCallback(
(files) => {
const error = validateFiles(files)
if (error) {
setErrorMessage(error)
} else {
setErrorMessage(null)
onDropProp(files)
}
},
[onDropProp, validateFiles]
)

const onDropRejected = useCallback(
(fileRejections) => {
const errorMessages = fileRejections.map((fileRejection) => {
const { errors, file } = fileRejection
const errorCodes = errors.map((error) => error.code)

if (errorCodes.includes('file-invalid-type')) {
const extension = FileUtils.getExtension(file.path)
return i18n.t('dropzone.error.invalidFileExtension', { extension })
} else if (errorCodes.includes('file-too-large')) {
return i18n.t('dropzone.error.fileTooBig')
} else {
return i18n.t('dropzone.error.fileNotValid')
}
})
setErrorMessage(errorMessages.join(', '))
},
[i18n]
)

return (
<>
<ReactDropzone accept={accept} disabled={disabled} maxSize={maxSize} multiple={multiple} onDrop={onDrop}>
<ReactDropzone
accept={accept}
disabled={disabled}
maxSize={maxSize}
multiple={multiple}
onDropAccepted={onDropAccepted}
onDropRejected={onDropRejected}
>
{({ getRootProps, getInputProps }) => (
<div className={classNames('dropzone', { disabled })} {...getRootProps()}>
<input {...getInputProps()} />
Expand Down
Loading