feat(grid): improve draft persistence and fix global save in national view

This commit is contained in:
Michael committed 2026-02-21 15:49:52 +01:00
1 parent cb7cbf873f
commit de7aa1560e
2 files changed
+15 -10

No files matched your search

+14 -10
View File
@@ -5,6 +5,8 @@ import { ventesProduits } from "@/db/schema";
import { eq, and } from "drizzle-orm"; import { eq, and } from "drizzle-orm";
import { z } from "zod"; import { z } from "zod";
import { revalidatePath } from "next/cache";
const SaveDraftsSchema = z.object({ const SaveDraftsSchema = z.object({
changes: z.array( changes: z.array(
z.object({ z.object({
@@ -26,21 +28,23 @@ export async function saveDraftChanges(
const { changes } = parsed.data; const { changes } = parsed.data;
try { try {
// Batch update all rows for given codein × magasin // Batch update all rows for given codein.
// If magasin is "TOTAL", update ALL stores for that product.
await Promise.all( await Promise.all(
changes.map(({ codein, magasin, codeGamme }) => changes.map(({ codein, magasin, codeGamme }) => {
db const whereClause = [eq(ventesProduits.codein, codein)];
if (magasin !== "TOTAL") {
whereClause.push(eq(ventesProduits.magasin, magasin));
}
return db
.update(ventesProduits) .update(ventesProduits)
.set({ codeGamme }) .set({ codeGamme })
.where( .where(and(...whereClause));
and( })
eq(ventesProduits.codein, codein),
eq(ventesProduits.magasin, magasin)
)
)
)
); );
revalidatePath("/grid");
return { success: true, saved: changes.length }; return { success: true, saved: changes.length };
} catch (err) { } catch (err) {
const msg = err instanceof Error ? err.message : "Unknown error"; const msg = err instanceof Error ? err.message : "Unknown error";
@@ -82,6 +82,7 @@ export const useGridStore = create<GridState>()(
partialize: (state) => ({ partialize: (state) => ({
filters: state.filters, filters: state.filters,
displayDensity: state.displayDensity, displayDensity: state.displayDensity,
draftChanges: state.draftChanges,
}), }),
} }
) )