const OperationFileUpload = ({
onChange,
backendPersistentFiles,
removePersistentFile,
multiple,
placeholder,
createDownloadLink,
files,
}: FileUploadProps) => {
const intl = useIntl();
const onDrop = useCallback(
(acceptedFiles: File[]) => {
acceptedFiles.forEach(file => onChange(file, FileOperation.ADD));
},
[onChange],
);
const removeFile = (file: File) => {
onChange(file, FileOperation.REMOVE);
};
const { getRootProps, getInputProps } = useDropzone({
onDrop,
disabled: multiple ? false : !!files.length || !!backendPersistentFiles.length,
multiple,
noDragEventsBubbling: true,
});
return (
<S.DropzoneWrapper>
<S.DropzoneContainer {...getRootProps()}>
<S.InputForDropzone {...getInputProps()} />
{backendPersistentFiles.length || files.length ? (
<React.Fragment>
{
backendPersistentFiles.map(f => (
<S.AcceptedFileContainer>
<S.LoadByLinkFile
href={createDownloadLink(
f.id)}
target="_blank"
onClick={(event: React.MouseEvent<HTMLElement>) => {
event.stopPropagation();
}}
>
<CommonTooltip name={
f.name} id={
f.name} />
</S.LoadByLinkFile>
{f.contentSize !== undefined && f.contentSize !== 0 && (
<AnyText color={LIGHT_LINK_COLOR} fontSize={14}>
{formatBytes(f.contentSize)}
</AnyText>
)}
<S.StyledRemoveButton
type="button"
onClick={(event: React.MouseEvent<HTMLElement>) => {
event.stopPropagation();
removePersistentFile(
f.id);
}}
>
<CloseIcon htmlColor={CLOSE_ICON_COLOR} style={{ width: 18, height: 18 }} />
</S.StyledRemoveButton>
</S.AcceptedFileContainer>
))}
{
files.map(
(f: FileItem) =>
f.status !== FileUploadStatus.SUCCESS && (
<S.AcceptedFileContainer>
<CommonTooltip name={
f.file.name} id={
f.file.name} />
{f.file.size !== undefined && f.file.size !== 0 && (
<AnyText color={LIGHT_LINK_COLOR} fontSize={14}>
{formatBytes(f.file.size)}
</AnyText>
)}
<S.StyledRemoveButton
type="button"
onClick={(event: React.MouseEvent<HTMLElement>) => {
event.stopPropagation();
removeFile(f.file);
}}
>
<CloseIcon htmlColor={CLOSE_ICON_COLOR} style={{ width: 18, height: 18 }} />
</S.StyledRemoveButton>
</S.AcceptedFileContainer>
),
)}
</React.Fragment>
) : (
<AnyText color={LIGHT_LINK_COLOR} fontSize={14}>
{placeholder ||
(multiple
? intl.formatMessage(MESSAGES.CHOOSE_FILES_PLACEHOLDER_FIELD)
: intl.formatMessage(MESSAGES.CHOOSE_FILE_PLACEHOLDER_FIELD))}
</AnyText>
)}
<S.AttachSvgIcon src={attachIcon} svgStyle={{ fill: LIGHT_LINK_COLOR }} />
</S.DropzoneContainer>
</S.DropzoneWrapper>
);
};