import React from "react"; import axios from "axios"; import { toast } from "react-toastify"; import { BsFilePdf, BsFileExcel} from "react-icons/bs"; type DownloadingPdf = { [key: string]: boolean; }; type PdfEndpoint = "stats" | "assignments"; type FileType = "pdf" | "excel"; export const usePDFDownload = (endpoint: PdfEndpoint, file: FileType = 'pdf') => { const [downloadingPdf, setDownloadingPdf] = React.useState( {} ); const triggerDownload = async (id: string) => { try { setDownloadingPdf((prev) => ({ ...prev, [id]: true })); const res = await axios.post(`/api/${endpoint}/${id}/export/${file}`); toast.success("Report ready!"); const link = document.createElement("a"); link.href = res.data; // download should have worked but there are some CORS issues // https://firebase.google.com/docs/storage/web/download-files#cors_configuration // link.download="report.pdf"; link.target = "_blank"; link.rel = "noreferrer"; link.click(); setDownloadingPdf((prev) => ({ ...prev, [id]: false })); } catch (err) { toast.error("Failed to display the report!"); console.error(err); setDownloadingPdf((prev) => ({ ...prev, [id]: false })); } }; const renderIcon = ( id: string, downloadClasses: string, loadingClasses: string ) => { if (downloadingPdf[id]) { return ( ); } const Icon = file === "excel" ? BsFileExcel : BsFilePdf; return ( { e.stopPropagation(); triggerDownload(id); }} /> ); }; return renderIcon; };