diff --git a/src/views/Reports/OpeningInventoryReports.js b/src/views/Reports/OpeningInventoryReports.js index 7f9e7a1..45d5ad5 100644 --- a/src/views/Reports/OpeningInventoryReports.js +++ b/src/views/Reports/OpeningInventoryReports.js @@ -54,29 +54,36 @@ const OpeningInventoryReports = () => { setLoading(false); } }; - + const handleDownloadReport = async () => { try { - const response = await axios.get(`/api/report/opening-inventory/download`, { - headers: { - Authorization: `Bearer ${token}`, - }, - responseType: 'arraybuffer', // Ensure that we handle the response as binary data - }); - + const response = await axios.get( + `/api/report/opening-inventory/download`, + { + headers: { + Authorization: `Bearer ${token}`, + }, + responseType: "arraybuffer", // Ensure that we handle the response as binary data + } + ); + // Step 1: Convert the response data into a Blob - const url = window.URL.createObjectURL(new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })); - + const url = window.URL.createObjectURL( + new Blob([response.data], { + type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + }) + ); + // Step 2: Create a download link and trigger the download const link = document.createElement("a"); link.setAttribute("href", url); link.setAttribute("download", "OpeningInventoryReport.xlsx"); document.body.appendChild(link); link.click(); - + // Step 3: Clean up document.body.removeChild(link); - window.URL.revokeObjectURL(url); // Clean up the Blob URL + window.URL.revokeObjectURL(url); // Clean up the Blob URL } catch (err) { const msg = err?.response?.data?.msg || "Something went wrong!"; swal({ @@ -232,7 +239,7 @@ const OpeningInventoryReports = () => { className="mt-4" variant="contained" color="primary" - size="small" + size="small" onClick={handleDownloadReport} disabled={loading} > @@ -264,7 +271,7 @@ const OpeningInventoryReports = () => { Brand Name