// @ts-nocheck import { useState } from 'react'; import { isEmpty } from 'lodash'; import { Button, Intent, Text, Spinner } from '@blueprintjs/core'; import { Box, Group, Icon, Stack } from '@/components'; import { ImportDropzoneField, ImportDropzoneFieldProps, } from '@/containers/Import/ImportDropzoneFile'; import { useUncontrolled } from '@/hooks/useUncontrolled'; import { useGetPresignedUrlAttachment, useUploadAttachments, } from '@/hooks/query/attachments'; import { formatBytes } from '../Sales/Invoices/InvoiceForm/utils'; import styles from './UploadAttachmentPopoverContent.module.scss'; import { MIME_TYPES } from '@/components/Dropzone/mine-types'; interface AttachmentFileCommon { originName: string; key: string; size: number; mimeType: string; } interface AttachmentFileLoaded extends AttachmentFileCommon {} interface AttachmentFileLoading extends AttachmentFileCommon { loading: boolean; } type AttachmentFile = AttachmentFileLoaded | AttachmentFileLoading; interface UploadAttachmentsPopoverContentProps { initialValue?: AttachmentFile[]; value?: AttachmentFile[]; onChange?: (value: AttachmentFile[]) => void; onUploadedChange?: (value: AttachmentFile[]) => void; dropzoneFieldProps?: ImportDropzoneFieldProps; } /** * Uploads and list the attachments with ability to delete particular attachment. * @param {UploadAttachmentsPopoverContentProps} */ export function UploadAttachmentsPopoverContent({ initialValue, value, onChange, onUploadedChange, dropzoneFieldProps, }: UploadAttachmentsPopoverContentProps) { // Controlled/uncontrolled value state. const [localFiles, handleFilesChange] = useUncontrolled({ finalValue: [], initialValue, value, onChange: onChange, }); // Stops loading of the given attachment key and updates it to new key, // that came from the server-side after uploading is done. const stopLoadingAttachment = ( localFiles: AttachmentFile[], internalKey: string, newKey: string, ) => { return localFiles.map((localFile) => { if (localFile.key === internalKey) { return { ...localFile, key: newKey, loading: false, }; } return localFile; }); }; // Uploads the attachments. const { mutateAsync: uploadAttachments } = useUploadAttachments({ onSuccess: (data) => { const newLocalFiles = stopLoadingAttachment( localFiles, data.config.data.get('internalKey'), data.data.data.key, ); handleFilesChange(newLocalFiles); onUploadedChange && onUploadedChange(newLocalFiles); }, }); // Deletes the attachment of the given file key. const handleClick = (key: string) => () => { const updatedFiles = localFiles.filter((file, i) => file.key !== key); handleFilesChange(updatedFiles); onUploadedChange && onUploadedChange(updatedFiles); }; // Handle change dropzone. const handleChangeDropzone = (file: File) => { const formData = new FormData(); const key = Date.now().toString(); formData.append('file', file); formData.append('internalKey', key); handleFilesChange([ { originName: file.name, size: file.size, key, loading: true, }, ...localFiles, ]); uploadAttachments(formData); }; return (
Attach documents Maximum: 25MB {!isEmpty(localFiles) && ( {localFiles.map((localFile: AttachmentFile, index: number) => (
{localFile.loading ? ( ) : ( )}
{localFile.originName} {localFile.loading ? ( Loading... ) : ( {formatBytes(localFile.size)} )}
{!localFile.loading && ( )}
))}
)}
); } const ViewButton = ({ fileKey }: { fileKey: string }) => { const [isLoading, setLoading] = useState(false); const { mutateAsync: getAttachmentPresignedUrl } = useGetPresignedUrlAttachment(); const handleViewBtnClick = (key: string) => () => { setLoading(true); getAttachmentPresignedUrl(key).then((data) => { window.open(data.presigned_url); setLoading(false); }); }; return ( ); };