mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-22 14:38:10 +02:00
feat: add Zustand store for grid state and a hook to save draft modifications
This commit is contained in:
1 parent
aa69637f98
commit
7cdbe8ec21
2 files changed
+17
-1
No files matched your search
@@ -10,7 +10,7 @@ import { GammeCode } from "@/types/grid";
|
|||||||
* Filters changes to only include those in the provided codeins list (e.g. current supplier).
|
* Filters changes to only include those in the provided codeins list (e.g. current supplier).
|
||||||
*/
|
*/
|
||||||
export function useSaveDrafts(magasin: string, filterCodeins?: string[]) {
|
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)
|
// Only consider changes that are in the filter list (if provided)
|
||||||
const activeDrafts = filterCodeins
|
const activeDrafts = filterCodeins
|
||||||
@@ -39,6 +39,9 @@ export function useSaveDrafts(magasin: string, filterCodeins?: string[]) {
|
|||||||
const result = await saveDraftChanges({ codeFournisseur, nomFournisseur, magasin, changes });
|
const result = await saveDraftChanges({ codeFournisseur, nomFournisseur, magasin, changes });
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
|
// Apply saved values onto row.codeGamme BEFORE clearing drafts
|
||||||
|
// so that isModified (codeGamme !== codeGammeInit) stays true
|
||||||
|
applyDraftsToRows(activeDrafts);
|
||||||
if (filterCodeins) {
|
if (filterCodeins) {
|
||||||
clearDrafts(Object.keys(activeDrafts));
|
clearDrafts(Object.keys(activeDrafts));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ interface GridState {
|
|||||||
resetDrafts: () => void;
|
resetDrafts: () => void;
|
||||||
/** Clear specific draft changes by codein */
|
/** Clear specific draft changes by codein */
|
||||||
clearDrafts: (codeins: string[]) => void;
|
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;
|
setFilter: (key: keyof GridFilters, value: string | null) => void;
|
||||||
setDisplayDensity: (density: "compact" | "normal" | "comfortable") => void;
|
setDisplayDensity: (density: "compact" | "normal" | "comfortable") => void;
|
||||||
setActiveGridQuery: (query: string) => void;
|
setActiveGridQuery: (query: string) => void;
|
||||||
@@ -108,6 +110,17 @@ export const useGridStore = create<GridState>()(
|
|||||||
codeins.forEach((id) => delete draftChanges[id]);
|
codeins.forEach((id) => delete draftChanges[id]);
|
||||||
set({ draftChanges, summary: computeSummary(get().rows, draftChanges) });
|
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) => {
|
setFilter: (key, value) => {
|
||||||
set((state) => ({ ...state, filters: { ...state.filters, [key]: value } }));
|
set((state) => ({ ...state, filters: { ...state.filters, [key]: value } }));
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in new issue
Block a user