feat: add Zustand store for grid state and a hook to save draft modifications

This commit is contained in:
Michael committed 2026-03-19 15:17:28 +01:00
1 parent aa69637f98
commit 7cdbe8ec21
2 files changed
+17 -1

No files matched your search

+4 -1
View File
@@ -10,7 +10,7 @@ import { GammeCode } from "@/types/grid";
* Filters changes to only include those in the provided codeins list (e.g. current supplier).
*/
export function useSaveDrafts(magasin: string, filterCodeins?: string[]) {
const { draftChanges, rows, resetDrafts, clearDrafts } = useGridStore();
const { draftChanges, rows, resetDrafts, clearDrafts, applyDraftsToRows } = useGridStore();
// Only consider changes that are in the filter list (if provided)
const activeDrafts = filterCodeins
@@ -39,6 +39,9 @@ export function useSaveDrafts(magasin: string, filterCodeins?: string[]) {
const result = await saveDraftChanges({ codeFournisseur, nomFournisseur, magasin, changes });
if (result.success) {
// Apply saved values onto row.codeGamme BEFORE clearing drafts
// so that isModified (codeGamme !== codeGammeInit) stays true
applyDraftsToRows(activeDrafts);
if (filterCodeins) {
clearDrafts(Object.keys(activeDrafts));
} else {
+13
View File
@@ -25,6 +25,8 @@ interface GridState {
resetDrafts: () => void;
/** Clear specific draft changes by codein */
clearDrafts: (codeins: string[]) => void;
/** Apply saved drafts onto row.codeGamme so change indicators persist after save */
applyDraftsToRows: (draftsToApply: Record<string, GammeCode>) => void;
setFilter: (key: keyof GridFilters, value: string | null) => void;
setDisplayDensity: (density: "compact" | "normal" | "comfortable") => void;
setActiveGridQuery: (query: string) => void;
@@ -108,6 +110,17 @@ export const useGridStore = create<GridState>()(
codeins.forEach((id) => delete draftChanges[id]);
set({ draftChanges, summary: computeSummary(get().rows, draftChanges) });
},
/** Apply saved drafts onto row.codeGamme so isModified works after clearing drafts */
applyDraftsToRows: (draftsToApply: Record<string, GammeCode>) => {
const rows = get().rows.map(r => {
const newGamme = draftsToApply[r.codein];
if (newGamme !== undefined) {
return { ...r, codeGamme: newGamme };
}
return r;
});
set({ rows });
},
setFilter: (key, value) => {
set((state) => ({ ...state, filters: { ...state.filters, [key]: value } }));
},