Version 1.0 squash
This commit is contained in:
65
web/src/services/floor-plans.ts
Normal file
65
web/src/services/floor-plans.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Floor Plans Service
|
||||
*
|
||||
* Handles all data operations for floor plans including fetching
|
||||
* floor plan data with associated rooms and real-time subscriptions.
|
||||
*
|
||||
* Returns data directly from PocketBase with expand relations.
|
||||
* File URLs are resolved at display time using getFileUrl() helper.
|
||||
*/
|
||||
|
||||
import { pb, Collections } from "@/lib/pocketbase";
|
||||
import type { FloorPlan, FloorPlanWithRooms, Room } from "@/types/floor-plan";
|
||||
import { safely } from "@/lib/result";
|
||||
|
||||
/**
|
||||
* Fetches all floor plans with their associated rooms
|
||||
* Expands room -> company relations for complete data
|
||||
* Returns floor plans sorted by sort_order
|
||||
*
|
||||
* @returns Array of floor plans with nested rooms and company data
|
||||
*/
|
||||
export async function getFloorPlans(): Promise<FloorPlanWithRooms[]> {
|
||||
/* Fetch all floor plans */
|
||||
const floorPlans = await pb.collection(Collections.FloorPlans).getFullList<FloorPlan>({
|
||||
sort: "sort_order",
|
||||
});
|
||||
|
||||
/* Fetch all rooms with company relations expanded */
|
||||
const rooms = await pb.collection(Collections.Rooms).getFullList<Room>({
|
||||
expand: "company",
|
||||
});
|
||||
|
||||
/* Combine floor plans with their rooms */
|
||||
return floorPlans.map((floorPlan) => ({
|
||||
...floorPlan,
|
||||
rooms: rooms.filter((room) => room.floor_plan === floorPlan.id),
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes to real-time floor plan and room updates
|
||||
* Callback is invoked whenever floor plans or rooms change
|
||||
* Monitors both collections to ensure complete updates
|
||||
*
|
||||
* @param callback Function to call when floor plans change
|
||||
* @returns Unsubscribe function to clean up all subscriptions
|
||||
*/
|
||||
export function subscribeToFloorPlans(callback: (floorPlans: FloorPlanWithRooms[]) => void): () => void {
|
||||
pb.collection(Collections.FloorPlans).subscribe<FloorPlan>("*", async () => {
|
||||
const floorPlans = await getFloorPlans();
|
||||
callback(floorPlans);
|
||||
});
|
||||
|
||||
pb.collection(Collections.Rooms).subscribe<Room>("*", async () => {
|
||||
const floorPlans = await getFloorPlans();
|
||||
callback(floorPlans);
|
||||
});
|
||||
|
||||
return () => {
|
||||
safely(() => {
|
||||
pb.collection(Collections.FloorPlans).unsubscribe("*");
|
||||
pb.collection(Collections.Rooms).unsubscribe("*");
|
||||
});
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user