Added initial Excel changes

This commit is contained in:
Joao Ramos
2024-08-15 14:56:14 +01:00
parent e84cc8ddd8
commit 1950d5f15d
8 changed files with 590 additions and 17 deletions

View File

@@ -1,15 +1,16 @@
import React from "react";
import axios from "axios";
import { toast } from "react-toastify";
import { BsFilePdf } from "react-icons/bs";
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) => {
export const usePDFDownload = (endpoint: PdfEndpoint, file: FileType = 'pdf') => {
const [downloadingPdf, setDownloadingPdf] = React.useState<DownloadingPdf>(
{}
);
@@ -17,7 +18,7 @@ export const usePDFDownload = (endpoint: PdfEndpoint) => {
const triggerDownload = async (id: string) => {
try {
setDownloadingPdf((prev) => ({ ...prev, [id]: true }));
const res = await axios.post(`/api/${endpoint}/${id}/export`);
const res = await axios.post(`/api/${endpoint}/${id}/export/${file}`);
toast.success("Report ready!");
const link = document.createElement("a");
link.href = res.data;
@@ -45,8 +46,11 @@ export const usePDFDownload = (endpoint: PdfEndpoint) => {
<span className={`${loadingClasses} loading loading-infinity w-6`} />
);
}
const Icon = file === "excel" ? BsFileExcel : BsFilePdf;
return (
<BsFilePdf
<Icon
className={`${downloadClasses} text-2xl cursor-pointer`}
onClick={(e) => {
e.stopPropagation();