Skip to content

Commit

Permalink
UI: Handle empty file uploads in FileUploader
Browse files Browse the repository at this point in the history
* Display a warning for empty files, indicating they won't be included and listing file names.
* Feature controlled by `records-resources-allow-empty-files` config value.
* Continue uploading other files while showing the warning message.
  • Loading branch information
Samk13 committed Sep 14, 2024
1 parent b717fb7 commit 80bde7a
Showing 1 changed file with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright (C) 2020-2022 Northwestern University.
// Copyright (C) 2022 Graz University of Technology.
// Copyright (C) 2022 TU Wien.
// Copyright (C) 2024 KTH Royal Institute of Technology.
//
// Invenio-RDM-Records is free software; you can redistribute it and/or modify it
// under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -40,6 +41,7 @@ export const FileUploaderComponent = ({
isFileImportInProgress,
decimalSizeDisplay,
filesLocked,
allowEmptyFiles,
...uiProps
}) => {
// We extract the working copy of the draft stored as `values` in formik
Expand Down Expand Up @@ -80,9 +82,24 @@ export const FileUploaderComponent = ({
const maxFileStorageReached = filesSize + acceptedFilesSize > quota.maxStorage;

const filesNames = _map(filesList, "name");
const duplicateFiles = acceptedFiles.filter((acceptedFile) =>
filesNames.includes(acceptedFile.name)
);
let hasNonEmptyFiles = false;
let hasEmptyFiles = false;
let duplicateFiles = [];
let nonEmptyFiles = [];
let emptyFiles = [];

for (const file of acceptedFiles) {
if (file.size === 0) {
hasEmptyFiles = true;
emptyFiles.push(file);
} else {
hasNonEmptyFiles = true;
nonEmptyFiles.push(file);
}
if (filesNames.includes(file.name)) {
duplicateFiles.push(file);
}
}

if (maxFileNumberReached) {
setWarningMsg(
Expand Down Expand Up @@ -130,7 +147,24 @@ export const FileUploaderComponent = ({
</div>
);
} else {
uploadFiles(formikDraft, acceptedFiles);
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>
);
}

// Proceed with uploading the non-empty files or all files if empty files are allowed
if (allowEmptyFiles || hasNonEmptyFiles) {
uploadFiles(formikDraft, allowEmptyFiles ? acceptedFiles : nonEmptyFiles);
}
}
},
multiple: true,
Expand Down Expand Up @@ -348,6 +382,7 @@ FileUploaderComponent.propTypes = {
decimalSizeDisplay: PropTypes.bool,
filesLocked: PropTypes.bool,
permissions: PropTypes.object,
allowEmptyFiles: PropTypes.bool,
};

FileUploaderComponent.defaultProps = {
Expand All @@ -369,4 +404,5 @@ FileUploaderComponent.defaultProps = {
importButtonText: i18next.t("Import files"),
decimalSizeDisplay: true,
filesLocked: false,
allowEmptyFiles: true,
};

0 comments on commit 80bde7a

Please sign in to comment.