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

@ -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>