From f352c53e70bbc3e210c96f9550004143c288001b Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Tue, 30 Jul 2024 17:55:23 -0300 Subject: [PATCH] enhancement: Update useFileDrop hook to handle multiple file drops (#3083) * refactor: Update useFileDrop hook to handle multiple file drops The useFileDrop hook in use-on-file-drop.tsx has been updated to handle multiple file drops. It now accepts an array of files instead of a single file, and processes each file individually. This allows for uploading multiple files at once and improves the user experience. * [autofix.ci] apply automated fixes * Remove unused console.log * Removed console.log --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Lucas Oliveira <62335616+lucaseduoli@users.noreply.github.com> --- .../pages/MainPage/hooks/use-on-file-drop.tsx | 92 +++++++++++-------- 1 file changed, 56 insertions(+), 36 deletions(-) diff --git a/src/frontend/src/pages/MainPage/hooks/use-on-file-drop.tsx b/src/frontend/src/pages/MainPage/hooks/use-on-file-drop.tsx index c0b898dfa..e47e13013 100644 --- a/src/frontend/src/pages/MainPage/hooks/use-on-file-drop.tsx +++ b/src/frontend/src/pages/MainPage/hooks/use-on-file-drop.tsx @@ -1,4 +1,5 @@ import { useLocation } from "react-router-dom"; +import { XYPosition } from "reactflow"; import { CONSOLE_ERROR_MSG, UPLOAD_ALERT_LIST, @@ -7,7 +8,20 @@ import { import useAlertStore from "../../../stores/alertStore"; import { useFolderStore } from "../../../stores/foldersStore"; -const useFileDrop = (uploadFlow, type) => { +const useFileDrop = ( + uploadFlow: ({ + newProject, + file, + isComponent, + position, + }: { + newProject: boolean; + file?: File; + isComponent: boolean | null; + position?: XYPosition; + }) => Promise, + type, +) => { const location = useLocation(); const setSuccessData = useAlertStore((state) => state.setSuccessData); const setErrorData = useAlertStore((state) => state.setErrorData); @@ -18,46 +32,52 @@ const useFileDrop = (uploadFlow, type) => { const handleFileDrop = (e) => { e.preventDefault(); if (e.dataTransfer.types.some((type) => type === "Files")) { - const file = e.dataTransfer.files.item(0); + const files: FileList = e.dataTransfer.files; - if (file.type === "application/json") { - const reader = new FileReader(); + const uploadPromises: Promise[] = []; - reader.onload = (event) => { - const fileContent = event.target!.result; - const fileContentJson = JSON.parse(fileContent as string); - const is_component = fileContentJson.is_component; - uploadFlow({ - newProject: true, - file: file, - isComponent: type === "all" ? null : type === "component", - }) - .then(() => { - setSuccessData({ - title: `${ - is_component ? "Component" : "Flow" - } uploaded successfully`, - }); - getFolderById(folderId ? folderId : myCollectionId); - }) - .catch((error) => { - setErrorData({ - title: CONSOLE_ERROR_MSG, - list: [error], - }); - }); - }; - - reader.readAsText(file); - } else { - setErrorData({ - title: WRONG_FILE_ERROR_ALERT, - list: [UPLOAD_ALERT_LIST], - }); + for (let i = 0; i < files.length; i++) { + const file = files[i]; + if (file.type === "application/json") { + const reader = new FileReader(); + const FileReaderPromise: Promise = new Promise( + (resolve, reject) => { + reader.onload = (event) => { + const fileContent = event.target!.result; + const fileContentJson = JSON.parse(fileContent as string); + const is_component = fileContentJson.is_component; + uploadFlow({ + newProject: true, + file: file, + isComponent: type === "all" ? null : type === "component", + }) + .then((_) => resolve()) + .catch((error) => { + reject(error); + }); + }; + reader.readAsText(file); + }, + ); + uploadPromises.push(FileReaderPromise); + } } + + Promise.all(uploadPromises) + .then(() => { + setSuccessData({ + title: `All files uploaded successfully`, + }); + getFolderById(folderId ? folderId : myCollectionId); + }) + .catch((error) => { + setErrorData({ + title: CONSOLE_ERROR_MSG, + list: [error], + }); + }); } }; - return [handleFileDrop]; };