Refactor action updates to include productId in updateAction calls across mechanics components; enhance event handling in product store and trigger management. Add clear functions for various stores to reset state. Update action and trigger management to prevent duplicates and ensure integrity. Adjust initial load actions to use consistent naming conventions.

This commit is contained in:
2025-05-02 13:13:41 +05:30
parent 34c30bb5a2
commit 01a03f5166
19 changed files with 245 additions and 175 deletions

View File

@@ -4,26 +4,23 @@ import { immer } from 'zustand/middleware/immer';
interface MachineStore {
machines: MachineStatus[];
// Actions
addMachine: (productId: string, machine: MachineEventSchema) => void;
removeMachine: (modelUuid: string) => void;
updateMachine: (
modelUuid: string,
updates: Partial<Omit<MachineStatus, 'modelUuid' | 'productId'>>
) => void;
clearMachines: () => void;
addCurrentAction: (modelUuid: string, actionUuid: string) => void;
removeCurrentAction: (modelUuid: string) => void;
// Status updates
setMachineActive: (modelUuid: string, isActive: boolean) => void;
setMachineState: (modelUuid: string, newState: MachineStatus['state']) => void;
// Time tracking
incrementActiveTime: (modelUuid: string, incrementBy: number) => void;
incrementIdleTime: (modelUuid: string, incrementBy: number) => void;
// Helpers
getMachineById: (modelUuid: string) => MachineStatus | undefined;
getMachinesByProduct: (productId: string) => MachineStatus[];
getMachinesBystate: (state: string) => MachineStatus[];
@@ -35,17 +32,19 @@ export const useMachineStore = create<MachineStore>()(
immer((set, get) => ({
machines: [],
// Actions
addMachine: (productId, machine) => {
set((state) => {
state.machines.push({
...machine,
productId,
isActive: false,
idleTime: 0,
activeTime: 0,
state: 'idle',
});
const exists = state.machines.some(m => m.modelUuid === machine.modelUuid);
if (!exists) {
state.machines.push({
...machine,
productId,
isActive: false,
idleTime: 0,
activeTime: 0,
state: 'idle',
});
}
});
},
@@ -64,6 +63,11 @@ export const useMachineStore = create<MachineStore>()(
});
},
clearMachines: () => {
set((state) => {
state.machines = [];
});
},
addCurrentAction: (modelUuid) => {
set((state) => {
@@ -89,7 +93,6 @@ export const useMachineStore = create<MachineStore>()(
});
},
// Status updates
setMachineActive: (modelUuid, isActive) => {
set((state) => {
const machine = state.machines.find(m => m.modelUuid === modelUuid);
@@ -108,7 +111,6 @@ export const useMachineStore = create<MachineStore>()(
});
},
// Time tracking
incrementActiveTime: (modelUuid, incrementBy) => {
set((state) => {
const machine = state.machines.find(m => m.modelUuid === modelUuid);
@@ -127,7 +129,6 @@ export const useMachineStore = create<MachineStore>()(
});
},
// Helpers
getMachineById: (modelUuid) => {
return get().machines.find(m => m.modelUuid === modelUuid);
},