77 lines
2.5 KiB
TypeScript
77 lines
2.5 KiB
TypeScript
|
import { create } from 'zustand';
|
||
|
import { immer } from 'zustand/middleware/immer';
|
||
|
|
||
|
type MaterialsStore = {
|
||
|
materials: MaterialsSchema;
|
||
|
|
||
|
addMaterial: (material: MaterialSchema) => void;
|
||
|
removeMaterial: (materialId: string) => void;
|
||
|
updateMaterial: (materialId: string, updates: Partial<MaterialSchema>) => void;
|
||
|
|
||
|
setStartTime: (materialId: string, startTime: string) => void;
|
||
|
setEndTime: (materialId: string, endTime: string) => void;
|
||
|
setCost: (materialId: string, cost: number) => void;
|
||
|
setWeight: (materialId: string, weight: number) => void;
|
||
|
|
||
|
getMaterialById: (materialId: string) => MaterialSchema | undefined;
|
||
|
};
|
||
|
|
||
|
export const useMaterialStore = create<MaterialsStore>()(
|
||
|
immer((set, get) => ({
|
||
|
materials: [],
|
||
|
|
||
|
addMaterial: (material) => {
|
||
|
set((state) => {
|
||
|
state.materials.push(material);
|
||
|
});
|
||
|
},
|
||
|
|
||
|
removeMaterial: (materialId) => {
|
||
|
set((state) => {
|
||
|
state.materials = state.materials.filter(m => m.materialId !== materialId);
|
||
|
});
|
||
|
},
|
||
|
|
||
|
updateMaterial: (materialId, updates) => {
|
||
|
set((state) => {
|
||
|
const material = state.materials.find(m => m.materialId === materialId);
|
||
|
if (material) {
|
||
|
Object.assign(material, updates);
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
setStartTime: (materialId, startTime) => {
|
||
|
set((state) => {
|
||
|
const material = state.materials.find(m => m.materialId === materialId);
|
||
|
if (material) material.startTime = startTime;
|
||
|
});
|
||
|
},
|
||
|
|
||
|
setEndTime: (materialId, endTime) => {
|
||
|
set((state) => {
|
||
|
const material = state.materials.find(m => m.materialId === materialId);
|
||
|
if (material) material.endTime = endTime;
|
||
|
});
|
||
|
},
|
||
|
|
||
|
setCost: (materialId, cost) => {
|
||
|
set((state) => {
|
||
|
const material = state.materials.find(m => m.materialId === materialId);
|
||
|
if (material) material.cost = cost;
|
||
|
});
|
||
|
},
|
||
|
|
||
|
setWeight: (materialId, weight) => {
|
||
|
set((state) => {
|
||
|
const material = state.materials.find(m => m.materialId === materialId);
|
||
|
if (material) material.weight = weight;
|
||
|
});
|
||
|
},
|
||
|
|
||
|
getMaterialById: (materialId) => {
|
||
|
return get().materials.find(m => m.materialId === materialId);
|
||
|
},
|
||
|
}))
|
||
|
);
|