Skip to content

Commit

Permalink
Refactor: Enhance Warnings Handling
Browse files Browse the repository at this point in the history
* Add the missing translation tags.
* Refactor the logic to display multiple warning messages, while allowing other files to upload, except for case where exceeding the file size limit.
  • Loading branch information
Samk13 authored and ntarocco committed Sep 19, 2024
1 parent fa829a1 commit 6d226ce
Showing 1 changed file with 37 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ export const FileUploaderComponent = ({
(accumulators, file) => {
if (filesNamesSet.has(file.name)) {
accumulators.duplicateFiles.push(file);
}

if (file.size === 0) {
} else if (file.size === 0) {
accumulators.emptyFiles.push(file);
} else {
accumulators.nonEmptyFiles.push(file);
Expand All @@ -102,18 +100,20 @@ export const FileUploaderComponent = ({
);

const hasEmptyFiles = !_isEmpty(emptyFiles);
const hasNonEmptyFiles = !_isEmpty(nonEmptyFiles);
const hasDuplicateFiles = !_isEmpty(duplicateFiles);

if (maxFileNumberReached) {
setWarningMsg(
<div className="content">
<Message
warning
icon="warning circle"
header="Could not upload files."
content={`Uploading the selected files would result in ${
filesList.length + acceptedFiles.length
} files (max.${quota.maxFiles})`}
header={i18next.t("Could not upload files.")}
content={i18next.t(
`Uploading the selected files would result in ${
filesList.length + acceptedFiles.length
} files (max.${quota.maxFiles})`
)}
/>
</div>
);
Expand All @@ -123,7 +123,7 @@ export const FileUploaderComponent = ({
<Message
warning
icon="warning circle"
header="Could not upload files."
header={i18next.t("Could not upload files.")}
content={
<>
{i18next.t("Uploading the selected files would result in")}{" "}
Expand All @@ -138,35 +138,43 @@ export const FileUploaderComponent = ({
/>
</div>
);
} else if (!_isEmpty(duplicateFiles)) {
setWarningMsg(
<div className="content">
} else {
let warnings = [];

if (hasDuplicateFiles) {
warnings.push(
<Message
warning
icon="warning circle"
header={i18next.t(`The following files already exist`)}
header={i18next.t("The following files already exist")}
list={_map(duplicateFiles, "name")}
/>
</div>
);
} else {
);
}

if (!allowEmptyFiles && hasEmptyFiles) {
setWarningMsg(
<div className="content">
<Message
warning
icon="warning circle"
header={i18next.t("Could not upload all files.")}
content={i18next.t("Empty files were skipped.")}
list={_map(emptyFiles, "name")}
/>
</div>
warnings.push(
<Message
warning
icon="warning circle"
header={i18next.t("Could not upload all files.")}
content={i18next.t("Empty files were skipped.")}
list={_map(emptyFiles, "name")}
/>
);
}

// Proceed with uploading the non-empty files or all files if empty files are allowed
if (allowEmptyFiles || hasNonEmptyFiles) {
uploadFiles(formikDraft, allowEmptyFiles ? acceptedFiles : nonEmptyFiles);
if (!_isEmpty(warnings)) {
setWarningMsg(<div className="content">{warnings}</div>);
}

const filesToUpload = allowEmptyFiles
? [...nonEmptyFiles, ...emptyFiles]
: nonEmptyFiles;

// Proceed with uploading files if there are any to upload
if (!_isEmpty(filesToUpload)) {
uploadFiles(formikDraft, filesToUpload);
}
}
},
Expand Down

0 comments on commit 6d226ce

Please sign in to comment.