Files
dashboard/context/hooks/useCrossStoreHandlers.ts
Alberto-Audrix 32a36cceff
CI / lint-and-test (push) Canceled after 0s
first commit
2026-07-28 08:55:05 +07:00

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,
};
};