From 13e7a30bb96faeb25936ff08a107834fd7af5766 Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Thu, 21 Sep 2023 15:58:32 +0200 Subject: [PATCH] feat: show upload modal on file drop --- frontend/src/components/upload/Dropzone.tsx | 21 +++----- .../upload/modals/showCreateUploadModal.tsx | 44 +++++++++------- frontend/src/pages/upload/index.tsx | 51 ++++++++++--------- 3 files changed, 60 insertions(+), 56 deletions(-) diff --git a/frontend/src/components/upload/Dropzone.tsx b/frontend/src/components/upload/Dropzone.tsx index cd979664..f2e4bce1 100644 --- a/frontend/src/components/upload/Dropzone.tsx +++ b/frontend/src/components/upload/Dropzone.tsx @@ -1,6 +1,6 @@ import { Button, Center, createStyles, Group, Text } from "@mantine/core"; import { Dropzone as MantineDropzone } from "@mantine/dropzone"; -import { Dispatch, ForwardedRef, SetStateAction, useRef } from "react"; +import { ForwardedRef, useRef } from "react"; import { TbCloudUpload, TbUpload } from "react-icons/tb"; import { FormattedMessage } from "react-intl"; import useTranslate from "../../hooks/useTranslate.hook"; @@ -35,13 +35,11 @@ const useStyles = createStyles((theme) => ({ const Dropzone = ({ isUploading, maxShareSize, - files, - setFiles, + showCreateUploadModalCallback, }: { isUploading: boolean; maxShareSize: number; - files: FileUpload[]; - setFiles: Dispatch>; + showCreateUploadModalCallback: (files: FileUpload[]) => void; }) => { const t = useTranslate(); @@ -55,24 +53,21 @@ const Dropzone = ({ }} disabled={isUploading} openRef={openRef as ForwardedRef<() => void>} - onDrop={(newFiles: FileUpload[]) => { - const fileSizeSum = [...newFiles, ...files].reduce( - (n, { size }) => n + size, - 0, - ); + onDrop={(files: FileUpload[]) => { + const fileSizeSum = files.reduce((n, { size }) => n + size, 0); if (fileSizeSum > maxShareSize) { toast.error( t("upload.dropzone.notify.file-too-big", { maxSize: byteToHumanSizeString(maxShareSize), - }), + }) ); } else { - newFiles = newFiles.map((newFile) => { + files = files.map((newFile) => { newFile.uploadingProgress = 0; return newFile; }); - setFiles([...newFiles, ...files]); + showCreateUploadModalCallback(files); } }} className={classes.dropzone} diff --git a/frontend/src/components/upload/modals/showCreateUploadModal.tsx b/frontend/src/components/upload/modals/showCreateUploadModal.tsx index 9d255dcc..65cda508 100644 --- a/frontend/src/components/upload/modals/showCreateUploadModal.tsx +++ b/frontend/src/components/upload/modals/showCreateUploadModal.tsx @@ -26,6 +26,7 @@ import useTranslate, { translateOutsideContext, } from "../../../hooks/useTranslate.hook"; import shareService from "../../../services/share.service"; +import { FileUpload } from "../../../types/File.type"; import { CreateShare } from "../../../types/share.type"; import { getExpirationPreview } from "../../../utils/date.util"; @@ -38,7 +39,8 @@ const showCreateUploadModal = ( allowUnauthenticatedShares: boolean; enableEmailRecepients: boolean; }, - uploadCallback: (createShare: CreateShare) => void, + files: FileUpload[], + uploadCallback: (createShare: CreateShare, files: FileUpload[]) => void ) => { const t = translateOutsideContext(); @@ -47,6 +49,7 @@ const showCreateUploadModal = ( children: ( ), @@ -55,9 +58,11 @@ const showCreateUploadModal = ( const CreateUploadModalBody = ({ uploadCallback, + files, options, }: { - uploadCallback: (createShare: CreateShare) => void; + files: FileUpload[]; + uploadCallback: (createShare: CreateShare, files: FileUpload[]) => void; options: { isUserSignedIn: boolean; isReverseShare: boolean; @@ -121,16 +126,19 @@ const CreateUploadModalBody = ({ const expiration = form.values.never_expires ? "never" : form.values.expiration_num + form.values.expiration_unit; - uploadCallback({ - id: values.link, - expiration: expiration, - recipients: values.recipients, - description: values.description, - security: { - password: values.password, - maxViews: values.maxViews, + uploadCallback( + { + id: values.link, + expiration: expiration, + recipients: values.recipients, + description: values.description, + security: { + password: values.password, + maxViews: values.maxViews, + }, }, - }); + files + ); modals.closeAll(); } })} @@ -152,7 +160,7 @@ const CreateUploadModalBody = ({ "link", Buffer.from(Math.random().toString(), "utf8") .toString("base64") - .substr(10, 7), + .substr(10, 7) ) } > @@ -251,7 +259,7 @@ const CreateUploadModalBody = ({ neverExpires: t("upload.modal.completed.never-expires"), expiresOn: t("upload.modal.completed.expires-on"), }, - form, + form )} @@ -266,7 +274,7 @@ const CreateUploadModalBody = ({