diff --git a/ipi-survey-platform/src/pages/Reports/Reports.jsx b/ipi-survey-platform/src/pages/Reports/Reports.jsx index c17d444..f59c6d6 100644 --- a/ipi-survey-platform/src/pages/Reports/Reports.jsx +++ b/ipi-survey-platform/src/pages/Reports/Reports.jsx @@ -1,5 +1,6 @@ import React from 'react'; import AdminHeader from '@/components/admin/AdminHeader'; +import {getReportEstablishment,getReportProduct,getReportValue} from '@/services/reports'; const reportCards = [ { @@ -79,6 +80,67 @@ const ExportIcon = () => ( ); const Reports = () => { + const [loading, setLoading] = React.useState(false); + + const getReport = async (reportType) => { + if (loading) { + return; // Prevent multiple clicks while loading + } + setLoading(true); + try { + let response; + let fileNames = ''; + if (reportType === 'Establishment Data (Quarterly Format)') { + response = await getReportEstablishment(); + fileNames = 'Establishment Data (Quarterly Format).xlsx'; + } else if (reportType === 'Product Data (Quarterly Format)') { + response = await getReportProduct(); + fileNames = 'Product Data (Quarterly Format).xlsx'; + } else if (reportType === 'Value Data (Quarterly Format)') { + response = await getReportValue(); + fileNames = 'Value Data (Quarterly Format).xlsx'; + } else { + console.warn(`Report type "${reportType}" is not implemented yet.`); + setLoading(false); + return; + } + const blob = new Blob([response.data], { + type: response.headers["content-type"], + }); + + const url = window.URL.createObjectURL(blob); + + const link = document.createElement("a"); + link.href = url; + + + + // Dynamic filename from backend + const contentDisposition = + response.headers["content-disposition"]; + + if (contentDisposition) { + const match = contentDisposition.match(/filename="?(.+)"?/); + + if (match?.[1]) { + fileNames = match[1]; + } + } + + link.setAttribute("download", fileNames); + + document.body.appendChild(link); + link.click(); + + link.remove(); + window.URL.revokeObjectURL(url); + setLoading(false); + + } catch (error) { + console.error(`Error fetching report:`, error); + setLoading(false); + } + }; return (