feat: Refactor point management in PointsCreator and update product store for better event handling
This commit is contained in:
@@ -18,8 +18,8 @@ type ProductsStore = {
|
||||
updateEvent: (productUuid: string, modelUuid: string, updates: Partial<EventsSchema>) => EventsSchema | undefined;
|
||||
|
||||
// Point-level actions
|
||||
addPoint: (productUuid: string, modelUuid: string, point: ConveyorPointSchema | VehiclePointSchema | RoboticArmPointSchema | MachinePointSchema | StoragePointSchema | HumanPointSchema) => void;
|
||||
removePoint: (productUuid: string, modelUuid: string, pointUuid: string) => void;
|
||||
addPoint: (productUuid: string, modelUuid: string, point: ConveyorPointSchema | VehiclePointSchema | RoboticArmPointSchema | MachinePointSchema | StoragePointSchema | HumanPointSchema) => EventsSchema | undefined;
|
||||
removePoint: (productUuid: string, modelUuid: string, pointUuid: string) => EventsSchema | undefined;
|
||||
updatePoint: (
|
||||
productUuid: string,
|
||||
modelUuid: string,
|
||||
@@ -72,6 +72,7 @@ type ProductsStore = {
|
||||
getModelUuidByActionUuid: (productUuid: string, actionUuid: string) => (string) | undefined;
|
||||
getPointUuidByActionUuid: (productUuid: string, actionUuid: string) => (string) | undefined;
|
||||
getTriggerByUuid: (productUuid: string, triggerUuid: string) => TriggerSchema | undefined;
|
||||
getTriggersByTriggeredPointUuid: (productUuid: string, triggeredPointUuid: string) => TriggerSchema[];
|
||||
getIsEventInProduct: (productUuid: string, modelUuid: string) => boolean;
|
||||
};
|
||||
|
||||
@@ -251,6 +252,7 @@ export const createProductStore = () => {
|
||||
|
||||
// Point-level actions
|
||||
addPoint: (productUuid, modelUuid, point) => {
|
||||
let updatedEvent: EventsSchema | undefined = undefined;
|
||||
set((state) => {
|
||||
const product = state.products.find(p => p.productUuid === productUuid);
|
||||
if (product) {
|
||||
@@ -259,29 +261,35 @@ export const createProductStore = () => {
|
||||
const existingPoint = (event as ConveyorEventSchema).points.find(p => p.uuid === point.uuid);
|
||||
if (!existingPoint) {
|
||||
(event as ConveyorEventSchema).points.push(point as ConveyorPointSchema);
|
||||
updatedEvent = JSON.parse(JSON.stringify(event));
|
||||
}
|
||||
} else if (event && 'point' in event) {
|
||||
const existingPoint = (event as any).point?.uuid === point.uuid;
|
||||
if (!existingPoint) {
|
||||
(event as VehicleEventSchema | RoboticArmEventSchema | MachineEventSchema | StorageEventSchema).point = point as any;
|
||||
updatedEvent = JSON.parse(JSON.stringify(event));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return updatedEvent;
|
||||
},
|
||||
|
||||
removePoint: (productUuid, modelUuid, pointUuid) => {
|
||||
let updatedEvent: EventsSchema | undefined = undefined;
|
||||
set((state) => {
|
||||
const product = state.products.find(p => p.productUuid === productUuid);
|
||||
if (product) {
|
||||
const event = product.eventDatas.find(e => 'modelUuid' in e && e.modelUuid === modelUuid);
|
||||
if (event && 'points' in event) {
|
||||
(event as ConveyorEventSchema).points = (event as ConveyorEventSchema).points.filter(p => p.uuid !== pointUuid);
|
||||
updatedEvent = JSON.parse(JSON.stringify(event));
|
||||
} else if (event && 'point' in event && (event as any).point.uuid === pointUuid) {
|
||||
// For events with single point, we can't remove it, only reset to empty
|
||||
}
|
||||
}
|
||||
});
|
||||
return updatedEvent;
|
||||
},
|
||||
|
||||
updatePoint: (productUuid, modelUuid, pointUuid, updates) => {
|
||||
@@ -881,6 +889,48 @@ export const createProductStore = () => {
|
||||
return undefined;
|
||||
},
|
||||
|
||||
getTriggersByTriggeredPointUuid: (productUuid, triggeredPointUuid) => {
|
||||
const product = get().products.find(p => p.productUuid === productUuid);
|
||||
if (!product) return [];
|
||||
|
||||
const triggers: TriggerSchema[] = [];
|
||||
|
||||
for (const event of product.eventDatas) {
|
||||
if ('points' in event) {
|
||||
for (const point of (event as ConveyorEventSchema).points) {
|
||||
if (point.action?.triggers) {
|
||||
for (const trigger of point.action.triggers) {
|
||||
if (trigger.triggeredAsset?.triggeredPoint?.pointUuid === triggeredPointUuid) {
|
||||
triggers.push(trigger);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if ('point' in event) {
|
||||
const point = (event as any).point;
|
||||
if ('action' in point && point.action?.triggers) {
|
||||
for (const trigger of point.action.triggers) {
|
||||
if (trigger.triggeredAsset?.triggeredPoint?.pointUuid === triggeredPointUuid) {
|
||||
triggers.push(trigger);
|
||||
}
|
||||
}
|
||||
} else if ('actions' in point) {
|
||||
for (const action of point.actions) {
|
||||
if (action.triggers) {
|
||||
for (const trigger of action.triggers) {
|
||||
if (trigger.triggeredAsset?.triggeredPoint?.pointUuid === triggeredPointUuid) {
|
||||
triggers.push(trigger);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return triggers;
|
||||
},
|
||||
|
||||
getIsEventInProduct: (productUuid, modelUuid) => {
|
||||
const product = get().getProductById(productUuid);
|
||||
if (!product) return false;
|
||||
|
||||
Reference in New Issue
Block a user