Skip to content

Commit

Permalink
Fixed fileupload name bug
Browse files Browse the repository at this point in the history
  • Loading branch information
shivankacker committed Sep 26, 2024
1 parent f5152fc commit 4ba63ed
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Components/Files/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export const FileUpload = (props: FileUploadProps) => {
label={t("enter_file_name")}
id="upload-file-name"
required
value={fileUpload.fileNames[0]}
value={fileUpload.fileNames[0] || ""}
disabled={!!fileUpload.progress}
onChange={(e) => fileUpload.setFileName(e.value)}
error={fileUpload.error || undefined}
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Patient/PatientConsentRecords.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export default function PatientConsentRecords(props: {
<TextFormField
name="filename"
label="File Name"
value={fileUpload.fileNames[0]}
value={fileUpload.fileNames[0] || ""}
onChange={(e) => fileUpload.setFileName(e.value)}
/>
<div className="flex gap-2">
Expand Down
1 change: 1 addition & 0 deletions src/Locale/en/FileUpload.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"file_list_headings__supporting_info": "Supporting Info",
"file_error__choose_file": "Please choose a file to upload",
"file_error__file_name": "Please give a name for all files!",
"file_error__single_file_name": "Please give a name for the file",
"change_file": "Change File",
"file_error__file_size": "Maximum size of files is 100 MB",
"file_error__file_type": "Invalid file type \".{{extension}}\" Allowed types: {{allowedExtensions}}",
Expand Down
15 changes: 11 additions & 4 deletions src/Utils/useFileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ChangeEvent,
DetailedHTMLProps,
InputHTMLAttributes,
useEffect,
useState,
} from "react";
import {
Expand Down Expand Up @@ -104,6 +105,11 @@ export default function useFileUpload(
});
};

useEffect(() => {
const blanks = Array(files.length).fill("");
setUploadFileNames((names) => [...names, ...blanks].slice(0, files.length));
}, [files]);

const validateFileUpload = () => {
if (files.length === 0) {
setError(t("file_error__choose_file"));
Expand Down Expand Up @@ -198,10 +204,11 @@ export default function useFileUpload(
setProgress(0);

for (const [index, file] of files.entries()) {
const filename =
uploadFileNames[index] === "" && file
? file.name
: uploadFileNames[index];
const filename = uploadFileNames[index];
if (!filename) {
setError(t("file_error__single_file_name"));
return;
}

const { data } = await request(routes.createUpload, {
body: {
Expand Down

0 comments on commit 4ba63ed

Please sign in to comment.