fix excel exports on publishers

This commit is contained in:
Dobromir Popov
2024-12-28 18:27:23 +02:00
parent c290b1a37b
commit 9e5cce3644
4 changed files with 19736 additions and 18 deletions

View File

@ -12,7 +12,7 @@ import fs from 'fs';
import path from 'path';
import { all } from "axios";
import { logger } from "src/helpers/common";
import { ExportPublishersToExcel } from "src/helpers/excel";
import { generatePublishersExcel } from "src/helpers/excel";
/**
*
@ -435,9 +435,16 @@ export default async function handler(req, res) {
res.status(200).json(await dataHelper.getAllPublishersWithStatisticsMonth(day, noEndDate));
case "exportPublishersExcel":
try {
await ExportPublishersToExcel(req, res);
const today = new Date();
const dateStr = today.toISOString().split('T')[0]; // Gets YYYY-MM-DD format
const excelBuffer = await generatePublishersExcel();
res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
res.setHeader("Content-Disposition", "attachment; filename=" + encodeURI(`Publishers_${dateStr}.xlsx`));
res.send(excelBuffer);
} catch (error) {
console.error(JSON.stringify(error));
res.status(500).json({ error: "Failed to generate Excel file" });
}
break;
default:

View File

@ -197,20 +197,34 @@ function PublishersPage({ publishers = [] }: IProps) {
const exportPublishers = async () => {
try {
const response = await axiosInstance.get('/api/?action=exportPublishersExcel');
const blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const response = await axiosInstance.get('/api/?action=exportPublishersExcel', { responseType: 'arraybuffer' });
// Get filename from Content-Disposition header
const contentDisposition = response.headers['content-disposition'];
let filename = 'publishers.xlsx'; // default fallback
if (contentDisposition) {
const filenameMatch = contentDisposition.match(/filename=(.*?)(;|$)/);
if (filenameMatch) {
filename = decodeURI(filenameMatch[1]);
}
}
const blob = new Blob([response.data], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'publishers.xlsx';
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url); // Clean up
} catch (error) {
console.error(JSON.stringify(error)); // Log the error
console.error(JSON.stringify(error));
toast.error("Грешка при експорт на данни");
}
}
return (
<Layout>
<ProtectedRoute allowedRoles={[UserRole.ADMIN, UserRole.POWERUSER]}>
@ -245,7 +259,7 @@ function PublishersPage({ publishers = [] }: IProps) {
</div>
{/* export by calling excel helper .ExportPublishersToExcel() */}
<div className="flex justify-center m-4">
<button className="btn" onClick={exportPublishers}>Export to Excel</button>
<button className="button m-2 btn btn-primary" onClick={exportPublishers}>Export to Excel</button>
</div>
</div>