You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
5.6 KiB
201 lines
5.6 KiB
3 years ago
|
import {
|
||
|
ChangeEventHandler,
|
||
|
FormEventHandler,
|
||
|
useEffect,
|
||
|
useRef,
|
||
|
useState,
|
||
|
} from 'react';
|
||
3 years ago
|
import { Box, Button, Input, InputLabel } from '@mui/material';
|
||
|
import EventEmitter from 'events';
|
||
|
|
||
|
import { UPLOAD_FILE_TYPES } from '../../lib/consts/UPLOAD_FILE_TYPES';
|
||
|
|
||
|
import FileInfo from './FileInfo';
|
||
3 years ago
|
import { ProgressBar } from '../Bars';
|
||
3 years ago
|
import { BodyText } from '../Text';
|
||
3 years ago
|
import mainAxiosInstance from '../../lib/singletons/mainAxiosInstance';
|
||
3 years ago
|
|
||
3 years ago
|
type FileUploadFormProps = {
|
||
|
onFileUploadComplete?: () => void;
|
||
3 years ago
|
openFilePickerEventEmitter?: EventEmitter;
|
||
|
};
|
||
|
|
||
3 years ago
|
type SelectedFile = Pick<
|
||
|
FileDetailMetadata,
|
||
|
'fileName' | 'fileLocations' | 'fileType'
|
||
|
> & {
|
||
3 years ago
|
file: File;
|
||
|
};
|
||
|
|
||
3 years ago
|
type InUploadFile = Pick<FileDetailMetadata, 'fileName'> & {
|
||
3 years ago
|
progressValue: number;
|
||
|
};
|
||
|
|
||
3 years ago
|
const FILE_UPLOAD_FORM_DEFAULT_PROPS: Partial<FileUploadFormProps> = {
|
||
|
onFileUploadComplete: undefined,
|
||
3 years ago
|
openFilePickerEventEmitter: undefined,
|
||
|
};
|
||
|
|
||
3 years ago
|
const FileUploadForm = (
|
||
3 years ago
|
{
|
||
3 years ago
|
onFileUploadComplete,
|
||
3 years ago
|
openFilePickerEventEmitter,
|
||
3 years ago
|
}: FileUploadFormProps = FILE_UPLOAD_FORM_DEFAULT_PROPS as FileUploadFormProps,
|
||
3 years ago
|
): JSX.Element => {
|
||
3 years ago
|
const selectFileRef = useRef<HTMLInputElement>();
|
||
|
|
||
3 years ago
|
const [selectedFiles, setSelectedFiles] = useState<SelectedFile[]>([]);
|
||
|
const [inUploadFiles, setInUploadFiles] = useState<InUploadFile[]>([]);
|
||
3 years ago
|
|
||
3 years ago
|
const convertMIMETypeToFileTypeKey = (fileMIMEType: string): FileType => {
|
||
3 years ago
|
const fileTypesIterator = UPLOAD_FILE_TYPES.entries();
|
||
|
|
||
3 years ago
|
let fileType: FileType | undefined;
|
||
3 years ago
|
|
||
|
do {
|
||
|
const fileTypesResult = fileTypesIterator.next();
|
||
|
|
||
|
if (fileTypesResult.value) {
|
||
|
const [fileTypeKey, [mimeTypeToUse]] = fileTypesResult.value;
|
||
|
|
||
|
if (fileMIMEType === mimeTypeToUse) {
|
||
|
fileType = fileTypeKey;
|
||
|
}
|
||
|
} else {
|
||
|
fileType = 'other';
|
||
|
}
|
||
|
} while (!fileType);
|
||
|
|
||
|
return fileType;
|
||
|
};
|
||
|
|
||
|
const autocompleteAfterSelectFile: ChangeEventHandler<HTMLInputElement> = ({
|
||
|
target: { files },
|
||
|
}) => {
|
||
|
if (files) {
|
||
3 years ago
|
setSelectedFiles(
|
||
|
Array.from(files).map(
|
||
|
(file): SelectedFile => ({
|
||
|
file,
|
||
3 years ago
|
fileName: file.name,
|
||
|
fileLocations: [],
|
||
|
fileType: convertMIMETypeToFileTypeKey(file.type),
|
||
3 years ago
|
}),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
const generateFileInfoOnChangeHandler = (
|
||
|
fileIndex: number,
|
||
3 years ago
|
): FileInfoChangeHandler => (inputValues) => {
|
||
|
selectedFiles[fileIndex] = {
|
||
|
...selectedFiles[fileIndex],
|
||
3 years ago
|
...inputValues,
|
||
|
};
|
||
|
};
|
||
|
|
||
|
const uploadFiles: FormEventHandler<HTMLFormElement> = (event) => {
|
||
|
event.preventDefault();
|
||
|
|
||
|
while (selectedFiles.length > 0) {
|
||
|
const selectedFile = selectedFiles.shift();
|
||
|
|
||
|
if (selectedFile) {
|
||
3 years ago
|
const { file, fileName, fileType } = selectedFile;
|
||
3 years ago
|
|
||
|
const fileFormData = new FormData();
|
||
|
|
||
|
fileFormData.append('file', new File([file], fileName, { ...file }));
|
||
|
fileFormData.append('file-type', fileType);
|
||
|
|
||
|
const inUploadFile: InUploadFile = { fileName, progressValue: 0 };
|
||
|
inUploadFiles.push(inUploadFile);
|
||
|
|
||
3 years ago
|
mainAxiosInstance
|
||
|
.post('/files', fileFormData, {
|
||
|
headers: {
|
||
|
'Content-Type': 'multipart/form-data',
|
||
|
},
|
||
|
onUploadProgress: ({ loaded, total }) => {
|
||
|
inUploadFile.progressValue = Math.round((loaded / total) * 100);
|
||
|
setInUploadFiles([...inUploadFiles]);
|
||
|
},
|
||
|
})
|
||
|
.then(() => {
|
||
|
onFileUploadComplete?.call(null);
|
||
3 years ago
|
|
||
3 years ago
|
inUploadFiles.splice(inUploadFiles.indexOf(inUploadFile), 1);
|
||
|
setInUploadFiles([...inUploadFiles]);
|
||
|
});
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
// Clears "staging area" (selected files) and populates "in-progress area" (in-upload files).
|
||
3 years ago
|
setSelectedFiles([]);
|
||
|
setInUploadFiles([...inUploadFiles]);
|
||
3 years ago
|
};
|
||
|
|
||
|
useEffect(() => {
|
||
|
openFilePickerEventEmitter?.addListener('open', () => {
|
||
|
selectFileRef.current?.click();
|
||
|
});
|
||
|
}, [openFilePickerEventEmitter]);
|
||
|
|
||
|
return (
|
||
3 years ago
|
<form onSubmit={uploadFiles}>
|
||
3 years ago
|
<Box sx={{ display: 'flex', flexDirection: 'column' }}>
|
||
|
<InputLabel htmlFor="select-file">
|
||
|
<Input
|
||
|
id="select-file"
|
||
|
inputProps={{ multiple: true }}
|
||
|
onChange={autocompleteAfterSelectFile}
|
||
|
ref={selectFileRef}
|
||
|
sx={{ display: 'none' }}
|
||
|
type="file"
|
||
|
/>
|
||
|
</InputLabel>
|
||
3 years ago
|
{inUploadFiles.map(({ fileName, progressValue }) => (
|
||
|
<Box
|
||
|
key={`in-upload-${fileName}`}
|
||
|
sx={{ display: 'flex', flexDirection: 'row' }}
|
||
|
>
|
||
|
<BodyText text={fileName} />
|
||
|
<Box sx={{ flexGrow: 1 }}>
|
||
|
<ProgressBar progressPercentage={progressValue} />
|
||
|
</Box>
|
||
|
</Box>
|
||
3 years ago
|
))}
|
||
3 years ago
|
{selectedFiles.map(
|
||
|
(
|
||
|
{
|
||
|
file: { name: originalFileName },
|
||
3 years ago
|
fileName,
|
||
|
fileType,
|
||
|
fileLocations,
|
||
3 years ago
|
},
|
||
|
fileIndex,
|
||
|
) => (
|
||
|
<FileInfo
|
||
3 years ago
|
{...{ fileName, fileType, fileLocations }}
|
||
3 years ago
|
// Use a non-changing key to prevent recreating the component.
|
||
|
// fileName holds the string from the file-name input, thus it changes when users makes a change.
|
||
|
key={`selected-${originalFileName}`}
|
||
|
onChange={generateFileInfoOnChangeHandler(fileIndex)}
|
||
|
/>
|
||
|
),
|
||
|
)}
|
||
|
{selectedFiles.length > 0 && (
|
||
|
<Button sx={{ textTransform: 'none' }} type="submit">
|
||
3 years ago
|
<BodyText text="Upload" />
|
||
|
</Button>
|
||
|
)}
|
||
|
</Box>
|
||
|
</form>
|
||
|
);
|
||
|
};
|
||
|
|
||
3 years ago
|
FileUploadForm.defaultProps = FILE_UPLOAD_FORM_DEFAULT_PROPS;
|
||
3 years ago
|
|
||
3 years ago
|
export default FileUploadForm;
|