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 Aug 28, 2024
1 parent b717fb7 commit f50bb40
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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 @@ -79,6 +81,9 @@ export const FileUploaderComponent = ({
);
const maxFileStorageReached = filesSize + acceptedFilesSize > quota.maxStorage;

const emptyFiles = acceptedFiles.filter((file) => file.size === 0);
const nonEmptyFiles = acceptedFiles.filter((file) => file.size > 0);

const filesNames = _map(filesList, "name");
const duplicateFiles = acceptedFiles.filter((acceptedFile) =>
filesNames.includes(acceptedFile.name)
Expand Down Expand Up @@ -130,7 +135,24 @@ export const FileUploaderComponent = ({
</div>
);
} else {
uploadFiles(formikDraft, acceptedFiles);
if (!_isEmpty(emptyFiles) && !allowEmptyFiles) {
setWarningMsg(
<div className="content">
<Message
warning
icon="warning circle"
header="Could not upload files."
content={i18next.t("Empty files were skipped as they aren’t allowed.")}
list={_map(emptyFiles, "name")}
/>
</div>
);
}

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

FileUploaderComponent.defaultProps = {
Expand All @@ -369,4 +392,5 @@ FileUploaderComponent.defaultProps = {
importButtonText: i18next.t("Import files"),
decimalSizeDisplay: true,
filesLocked: false,
allowEmptyFiles: true,
};
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// This file is part of Invenio-RDM-Records
// Copyright (C) 2020-2023 CERN.
// Copyright (C) 2020-2022 Northwestern University.
// 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.

import { connect } from "react-redux";
import { deleteFile, importParentFiles, uploadFiles } from "../../state/actions";
import { FileUploaderComponent } from "./FileUploader";
import { getInputFromDOM } from "../../dom";

const mapStateToProps = (state) => {
const { links, entries } = state.files;
const allowEmptyFiles = getInputFromDOM("records-resources-allow-empty-files");
return {
files: entries,
links,
Expand All @@ -21,6 +24,7 @@ const mapStateToProps = (state) => {
hasParentRecord: Boolean(
state.deposit.record?.versions?.index && state.deposit.record?.versions?.index > 1
),
allowEmptyFiles,
};
};

Expand Down

0 comments on commit f50bb40

Please sign in to comment.