Fix: handleDownload file download more than one file

This commit is contained in:
igorrCarvalho 2024-06-05 21:25:32 -03:00
commit ac932e9904

View file

@ -3,6 +3,8 @@ import {
BASE_URL_API, BASE_URL_API,
} from "../../../../../../constants/constants"; } from "../../../../../../constants/constants";
let isDownloading = false;
export default async function handleDownload({ export default async function handleDownload({
fileName, fileName,
content, content,
@ -10,10 +12,18 @@ export default async function handleDownload({
fileName: string; fileName: string;
content: string; content: string;
}): Promise<void> { }): Promise<void> {
if (isDownloading) return;
try { try {
isDownloading = true;
const response = await fetch( const response = await fetch(
`${BACKEND_URL.slice(0, BACKEND_URL.length - 1)}${BASE_URL_API}files/download/${content}`, `${BACKEND_URL.slice(0, BACKEND_URL.length - 1)}${BASE_URL_API}files/download/${content}`,
); );
if (!response.ok) {
throw new Error("Network response was not ok");
}
const blob = await response.blob(); const blob = await response.blob();
const url = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);
@ -27,5 +37,7 @@ export default async function handleDownload({
URL.revokeObjectURL(url); // Clean up the URL object URL.revokeObjectURL(url); // Clean up the URL object
} catch (error) { } catch (error) {
console.error("Failed to download file:", error); console.error("Failed to download file:", error);
} finally {
isDownloading = false;
} }
} }