refactor: Extract bulk download logic to custom hook (#8910)
* ✨ (custom-handle-bulk-files-download.tsx): introduce custom hook useCustomHandleBulkFilesDownload to handle bulk file downloads in a more customized way 🔧 (index.tsx): replace direct usage of useGetDownloadFilesV2 with the new custom hook useCustomHandleBulkFilesDownload for handling bulk file downloads in a more efficient and maintainable manner * Update src/frontend/src/customization/hooks/custom-handle-bulk-files-download.tsx Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
parent
343bdd0632
commit
ca0f5bc907
2 changed files with 45 additions and 21 deletions
|
|
@ -0,0 +1,36 @@
|
||||||
|
import { useGetDownloadFilesV2 } from "@/controllers/API/queries/file-management/use-get-download-files";
|
||||||
|
|
||||||
|
export const useCustomHandleBulkFilesDownload = () => {
|
||||||
|
const { mutate: downloadFiles } = useGetDownloadFilesV2();
|
||||||
|
|
||||||
|
const handleBulkDownload = (
|
||||||
|
selectedFiles: any,
|
||||||
|
setSuccessData: (data: { title: string }) => void,
|
||||||
|
setErrorData: (data: { title: string; list: string[] }) => void,
|
||||||
|
setIsDownloading: (isDownloading: boolean) => void,
|
||||||
|
) => {
|
||||||
|
setIsDownloading(true);
|
||||||
|
downloadFiles(
|
||||||
|
{
|
||||||
|
ids: selectedFiles.map((file) => file.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
onSuccess: (data) => {
|
||||||
|
setSuccessData({ title: data.message });
|
||||||
|
setIsDownloading(false);
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
setErrorData({
|
||||||
|
title: "Error downloading files",
|
||||||
|
list: [
|
||||||
|
error.message || "An error occurred while downloading the files",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
setIsDownloading(false);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
return { handleBulkDownload };
|
||||||
|
};
|
||||||
|
|
@ -8,8 +8,8 @@ import Loading from "@/components/ui/loading";
|
||||||
import { SidebarTrigger } from "@/components/ui/sidebar";
|
import { SidebarTrigger } from "@/components/ui/sidebar";
|
||||||
import { useGetFilesV2 } from "@/controllers/API/queries/file-management";
|
import { useGetFilesV2 } from "@/controllers/API/queries/file-management";
|
||||||
import { useDeleteFilesV2 } from "@/controllers/API/queries/file-management/use-delete-files";
|
import { useDeleteFilesV2 } from "@/controllers/API/queries/file-management/use-delete-files";
|
||||||
import { useGetDownloadFilesV2 } from "@/controllers/API/queries/file-management/use-get-download-files";
|
|
||||||
import { usePostRenameFileV2 } from "@/controllers/API/queries/file-management/use-put-rename-file";
|
import { usePostRenameFileV2 } from "@/controllers/API/queries/file-management/use-put-rename-file";
|
||||||
|
import { useCustomHandleBulkFilesDownload } from "@/customization/hooks/custom-handle-bulk-files-download";
|
||||||
import { customPostUploadFileV2 } from "@/customization/hooks/use-custom-post-upload-file";
|
import { customPostUploadFileV2 } from "@/customization/hooks/use-custom-post-upload-file";
|
||||||
import useUploadFile from "@/hooks/files/use-upload-file";
|
import useUploadFile from "@/hooks/files/use-upload-file";
|
||||||
import DeleteConfirmationModal from "@/modals/deleteConfirmationModal";
|
import DeleteConfirmationModal from "@/modals/deleteConfirmationModal";
|
||||||
|
|
@ -37,6 +37,7 @@ export const FilesPage = () => {
|
||||||
const [selectedFiles, setSelectedFiles] = useState<any[]>([]);
|
const [selectedFiles, setSelectedFiles] = useState<any[]>([]);
|
||||||
const [quantitySelected, setQuantitySelected] = useState(0);
|
const [quantitySelected, setQuantitySelected] = useState(0);
|
||||||
const [isShiftPressed, setIsShiftPressed] = useState(false);
|
const [isShiftPressed, setIsShiftPressed] = useState(false);
|
||||||
|
const [isDownloading, setIsDownloading] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleKeyDown = (e: KeyboardEvent) => {
|
const handleKeyDown = (e: KeyboardEvent) => {
|
||||||
|
|
@ -75,8 +76,7 @@ export const FilesPage = () => {
|
||||||
const { mutate: rename } = usePostRenameFileV2();
|
const { mutate: rename } = usePostRenameFileV2();
|
||||||
|
|
||||||
const { mutate: deleteFiles, isPending: isDeleting } = useDeleteFilesV2();
|
const { mutate: deleteFiles, isPending: isDeleting } = useDeleteFilesV2();
|
||||||
const { mutate: downloadFiles, isPending: isDownloading } =
|
const { handleBulkDownload } = useCustomHandleBulkFilesDownload();
|
||||||
useGetDownloadFilesV2();
|
|
||||||
|
|
||||||
const handleRename = (params: NewValueParams<any, any>) => {
|
const handleRename = (params: NewValueParams<any, any>) => {
|
||||||
rename({
|
rename({
|
||||||
|
|
@ -251,26 +251,14 @@ export const FilesPage = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDownload = () => {
|
const handleDownload = () => {
|
||||||
console.log(selectedFiles);
|
handleBulkDownload(
|
||||||
downloadFiles(
|
selectedFiles,
|
||||||
{
|
setSuccessData,
|
||||||
ids: selectedFiles.map((file) => file.id),
|
setErrorData,
|
||||||
},
|
setIsDownloading,
|
||||||
{
|
|
||||||
onSuccess: (data) => {
|
|
||||||
setSuccessData({ title: data.message });
|
|
||||||
},
|
|
||||||
onError: (error) => {
|
|
||||||
setErrorData({
|
|
||||||
title: "Error downloading files",
|
|
||||||
list: [
|
|
||||||
error.message || "An error occurred while downloading the files",
|
|
||||||
],
|
|
||||||
});
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = () => {
|
const handleDelete = () => {
|
||||||
deleteFiles(
|
deleteFiles(
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue