modified creadores
This commit is contained in:
@@ -1,43 +1,74 @@
|
||||
"use client";
|
||||
|
||||
import styles from "./style.module.css";
|
||||
import Cookies from "js-cookie";
|
||||
import { useState } from "react";
|
||||
import toast from "react-hot-toast";
|
||||
import { saveAs } from "file-saver";
|
||||
import ExcelJS from "exceljs";
|
||||
|
||||
export default function DownloadReporteXLSX() {
|
||||
import styles from "./style.module.css";
|
||||
|
||||
interface Recibo {
|
||||
id_recibo: number;
|
||||
folio_recibo: string;
|
||||
fecha_recibo: string;
|
||||
fecha_registro: string;
|
||||
monto: string;
|
||||
user: {
|
||||
usuario: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface Columns{
|
||||
header:string;
|
||||
key:string;
|
||||
width:number
|
||||
}
|
||||
|
||||
interface Props {
|
||||
data: Recibo[];
|
||||
columns:Columns[];
|
||||
}
|
||||
|
||||
export default function DownloadReporteXLSX({ data,columns }: Props) {
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const formatearFechaMX = (fechaISO: string) => {
|
||||
return new Date(fechaISO).toLocaleString("es-MX", {
|
||||
timeZone: "America/Mexico_City",
|
||||
});
|
||||
};
|
||||
|
||||
const handleDownload = async () => {
|
||||
if (!data || data.length === 0) return;
|
||||
|
||||
setLoading(true);
|
||||
const token = Cookies.get("token");
|
||||
const headers = { Authorization: `Bearer ${token}` };
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${process.env.NEXT_PUBLIC_API_URL}/equipos/reporteXLSX`,//Modificar
|
||||
{ headers }
|
||||
);
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
const worksheet = workbook.addWorksheet("Recibos");
|
||||
|
||||
if (!response.ok) {
|
||||
toast.error("Error al descargar el archivo");
|
||||
return;
|
||||
}
|
||||
worksheet.columns = columns;
|
||||
|
||||
const blob = await response.blob();
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
data.forEach((recibo) => {
|
||||
worksheet.addRow({
|
||||
folio: recibo.folio_recibo,
|
||||
monto: Number(recibo.monto),
|
||||
fechaRecibo: recibo.fecha_recibo,
|
||||
fechaRegistro: formatearFechaMX(recibo.fecha_registro),
|
||||
usuario: recibo.user.usuario,
|
||||
});
|
||||
});
|
||||
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = "Reporte Recibos.xlsx";
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
worksheet.getColumn("monto").numFmt = '"$"#,##0.00';
|
||||
|
||||
window.URL.revokeObjectURL(url);
|
||||
const buffer = await workbook.xlsx.writeBuffer();
|
||||
const blob = new Blob([buffer], {
|
||||
type:
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
});
|
||||
|
||||
saveAs(blob, "Reporte_Recibos.xlsx");
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
toast.error("Hubo un problema al descargar el archivo.");
|
||||
console.error("Error generando Excel", error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -52,7 +83,13 @@ export default function DownloadReporteXLSX() {
|
||||
{loading ? (
|
||||
<div className={styles.loader}></div>
|
||||
) : (
|
||||
<img src="/excel.svg" alt="Excel" className={styles.iconExcel} width={30} height={30}/>
|
||||
<img
|
||||
src="/excel.svg"
|
||||
alt="Excel"
|
||||
className={styles.iconExcel}
|
||||
width={30}
|
||||
height={30}
|
||||
/>
|
||||
)}
|
||||
</button>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user