From a2c4a065ddbd411cbe7777465c28945ece467331 Mon Sep 17 00:00:00 2001 From: Michael SCHAL Date: Fri, 10 Apr 2026 13:25:19 +0200 Subject: [PATCH] feat: add ExportDropdown component with Excel and PDF export functionality for grid data --- .../grid/components/export-dropdown.tsx | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/features/grid/components/export-dropdown.tsx b/src/features/grid/components/export-dropdown.tsx index 46296f3..4cff9ec 100644 --- a/src/features/grid/components/export-dropdown.tsx +++ b/src/features/grid/components/export-dropdown.tsx @@ -1,7 +1,7 @@ "use client"; import { useState } from "react"; -import { Download, FileSpreadsheet, FileText, ChevronDown } from "lucide-react"; +import { Download, FileSpreadsheet, FileText, ChevronDown, Table2 } from "lucide-react"; import * as ExcelJS from "exceljs"; import jsPDF from "jspdf"; import autoTable from "jspdf-autotable"; @@ -11,6 +11,52 @@ export function ExportDropdown({ nomFournisseur }: { nomFournisseur?: string }) const [isOpen, setIsOpen] = useState(false); const { rows, draftChanges } = useGridStore(); + const handleExportAllProducts = async () => { + setIsOpen(false); + if (rows.length === 0) { + alert("Aucune donnée à exporter."); + return; + } + + const supplier = nomFournisseur || rows[0]?.nomFournisseur || "Fournisseur"; + + const payload = { + nomFournisseur: supplier, + rows: rows.map(r => ({ + codein: r.codein, + libelle1: r.libelle1 || "", + code3: r.code3 || "", + libelle3: r.libelle3 || "", + codeGamme: (draftChanges[r.codein] ?? r.codeGamme) as string | null, + totalQuantite: r.totalQuantite, + totalCa: r.totalCa, + tauxMarge: r.tauxMarge, + gammeAvant: r.codeGammeInit ?? null, + })), + }; + + try { + const res = await fetch("/api/export/excel", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(payload), + }); + + if (!res.ok) throw new Error("Erreur lors de l'export"); + + const blob = await res.blob(); + const url = window.URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = url; + a.download = `Assortiment_Complet_${supplier.replace(/\s+/g, "_")}_${new Date().toISOString().slice(0, 10)}.xlsx`; + a.click(); + window.URL.revokeObjectURL(url); + } catch (error) { + console.error(error); + alert("Une erreur s'est produite lors de l'export Excel."); + } + }; + const handleExportExcel = async () => { setIsOpen(false); const workbook = new ExcelJS.Workbook(); @@ -157,6 +203,19 @@ export function ExportDropdown({ nomFournisseur }: { nomFournisseur?: string })
+ + +
+