41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { useCallback } from 'react';
|
|
import { useLocationStore } from '../stores/useLocationStore.ts';
|
|
import { useDeviceStore } from '../stores/useDeviceStore.ts';
|
|
|
|
export const useCrossStoreHandlers = (
|
|
locationStore: ReturnType<typeof useLocationStore>,
|
|
deviceStore: ReturnType<typeof useDeviceStore>
|
|
) => {
|
|
const onDeleteFloor = useCallback(
|
|
(floorId: string) => {
|
|
locationStore.onDeleteFloor(floorId);
|
|
deviceStore.unassignDevicesFromFloor(floorId);
|
|
},
|
|
[locationStore, deviceStore]
|
|
);
|
|
|
|
const onDeleteCoop = useCallback(
|
|
(coopId: string) => {
|
|
const floorsToDelete = locationStore.floors.filter((f) => f.coopId === coopId);
|
|
floorsToDelete.forEach((floor) => onDeleteFloor(floor.id));
|
|
locationStore.onDeleteCoop(coopId);
|
|
},
|
|
[locationStore, onDeleteFloor]
|
|
);
|
|
|
|
const onDeleteLocation = useCallback(
|
|
(locationId: string) => {
|
|
const coopsToDelete = locationStore.coops.filter((c) => c.locationId === locationId);
|
|
coopsToDelete.forEach((coop) => onDeleteCoop(coop.id));
|
|
locationStore.onDeleteLocation(locationId);
|
|
},
|
|
[locationStore, onDeleteCoop]
|
|
);
|
|
|
|
return {
|
|
onDeleteFloor,
|
|
onDeleteCoop,
|
|
onDeleteLocation,
|
|
};
|
|
};
|