Files
ERP/frontend/src/store/index.ts
T
ZCode b469238153 refactor(catalog): reduce the category taxonomy to a supplier label
Categories are kept only as the label each supplier carries (suppliers.category_id
+ the material_categories list it points at). Everything that treated category as
a shared key across materials, inventory, quotes and unit conversions is removed —
including two features that were never populated (0 rows on every volume).

Removed (backend):
- routers category_attributes, category_ref_units, unit_conversions + registration
- ?category_id= filters on /materials, /inventory and /quotes (the /suppliers one
  stays — it filters suppliers by their own label)
- GET /inventory/materials/options: required a category and had no caller
- the material-payload category derivation (expand_service.category_by_material,
  the inventory serializer lookup) and the material category on /pm/on-the-way;
  the wire keys stay, now always null, so the payload shape is unchanged
- startup DDL: DROP the empty category_attributes / category_reference_units /
  unit_conversions tables (idempotent) and their CREATE statements; seed.py
  truncate list updated

Removed (frontend):
- the category-keyed conversion path: refUnitForCategory / findConversion /
  refConverted, the categoryUnits + unitConversions store slices, api clients,
  hooks and the "≈ converted quantity" line on quote detail and project pages
  (it needed rows in those tables to render, and there were none)
- material.supplier_category and its read sites (the edit modal grew one
  yesterday to restore labels; the label lives on the supplier now)
- the inventory page's category chips, the row filter and the «دسته» column,
  plus the lookup's categories fetch and catName helper

Kept: supplier label + its list and picker, /suppliers?category_id=, the global
standard_conversions feature and the units page (material_units conversions).

Verified against a restore of the production dump: startup DDL drops exactly the
three tables, suppliers keep their label (3/3), material_categories still serves
its 8 rows, the removed endpoints 404, and materials/inventory/quotes/units/
standard-conversions all return 200 with quote line items intact.
2026-09-20 19:44:23 +00:00

109 lines
3.7 KiB
TypeScript

// ============================================================
// Zustand store — داده‌ها + state UI
// داده‌ها از React Query می‌آیند (api client) و اینجا mirror می‌شوند؛
// mutationها اول خوش‌بینانه اینجا آپدیت می‌کنند، بعد API را صدا
// می‌زنند و آخر cache را invalidate می‌کنند تا با حقیقت همگرا شود.
// ============================================================
import { create } from "zustand";
import type {
Material,
Project,
Quote,
StandardConversion,
Store,
Supplier,
Topic,
} from "../lib/types";
interface AppStore {
// داده‌ها (همگام با API)
suppliers: Supplier[];
materials: Material[];
projects: Project[];
quotes: Quote[];
standardConversions: StandardConversion[];
topics: Topic[]; // دسته‌بندی‌های ۸گانه — از /material-categories
// UI state
expandedQuoteIds: Set<string>;
// wizard
wizardStep: number;
wizardHighestVisited: number;
// actions — data
setData: (store: Store) => void;
addQuote: (q: Quote) => void;
updateQuote: (id: string, patch: Partial<Quote>) => void;
deleteQuote: (id: string) => void;
addSupplier: (s: Supplier) => void;
updateSupplier: (id: string, patch: Partial<Supplier>) => void;
deleteSupplier: (id: string) => void;
addMaterial: (m: Material) => void;
addProject: (p: Project) => void;
setStandardConversion: (sc: StandardConversion) => void;
deleteStandardConversion: (id: string) => void;
// actions — UI
toggleExpand: (id: string) => void;
setWizardStep: (step: number) => void;
setWizardHighestVisited: (n: number) => void;
}
export const useAppStore = create<AppStore>()((set) => ({
suppliers: [],
materials: [],
projects: [],
quotes: [],
standardConversions: [],
topics: [],
expandedQuoteIds: new Set<string>(),
wizardStep: 1,
wizardHighestVisited: 1,
setData: (store) =>
set({
suppliers: store.suppliers,
materials: store.materials,
projects: store.projects,
quotes: store.quotes,
standardConversions: store.standardConversions,
topics: store.topics,
}),
addQuote: (q) => set((s) => ({ quotes: [...s.quotes, q] })),
updateQuote: (id, patch) =>
set((s) => ({
quotes: s.quotes.map((q) => (q.id === id ? { ...q, ...patch } : q)),
})),
deleteQuote: (id) => set((s) => ({ quotes: s.quotes.filter((q) => q.id !== id) })),
addSupplier: (sup) => set((s) => ({ suppliers: [...s.suppliers, sup] })),
updateSupplier: (id, patch) =>
set((s) => ({
suppliers: s.suppliers.map((x) => (x.id === id ? { ...x, ...patch } : x)),
})),
deleteSupplier: (id) => set((s) => ({ suppliers: s.suppliers.filter((x) => x.id !== id) })),
addMaterial: (m) => set((s) => ({ materials: [...s.materials, m] })),
addProject: (p) => set((s) => ({ projects: [...s.projects, p] })),
setStandardConversion: (sc) =>
set((s) => {
const idx = s.standardConversions.findIndex((x) => x.id === sc.id);
const standardConversions = [...s.standardConversions];
if (idx >= 0) standardConversions[idx] = sc;
else standardConversions.push(sc);
return { standardConversions };
}),
deleteStandardConversion: (id) =>
set((s) => ({ standardConversions: s.standardConversions.filter((x) => x.id !== id) })),
toggleExpand: (id) =>
set((s) => {
const next = new Set(s.expandedQuoteIds);
if (next.has(id)) next.delete(id);
else next.add(id);
return { expandedQuoteIds: next };
}),
setWizardStep: (step) => set({ wizardStep: step }),
setWizardHighestVisited: (n) => set({ wizardHighestVisited: n }),
}));