1 | import { useState, useEffect } from "react";
|
---|
2 | import HttpService from "../../../scripts/net/HttpService.js";
|
---|
3 | import {MapBuilder} from "../../../scripts/main/MapBuilder.js";
|
---|
4 | import parseMapData from "../../../scripts/util/parseMapData.js";
|
---|
5 | import ShapeRegistry from "../../../scripts/util/ShapeRegistry.js";
|
---|
6 | import saveMap from "../../../components/SaveMap/SaveMap.jsx";
|
---|
7 | import triggerMapSave from "../../../scripts/util/triggerMapSave.js";
|
---|
8 | import config from "../../../scripts/net/netconfig.js";
|
---|
9 |
|
---|
10 |
|
---|
11 |
|
---|
12 | const useMapLoader = (mapName, username, searchParams, setSearchParams) => {
|
---|
13 | const [floors, setFloors] = useState([]);
|
---|
14 | const [mapLoaded, setMapLoaded] = useState(false);
|
---|
15 | const [app, setApp] = useState(null);
|
---|
16 |
|
---|
17 | useEffect(() => {
|
---|
18 | if (!searchParams.has("floor")) {
|
---|
19 | setSearchParams({ floor: "0"},{replace:true});
|
---|
20 | }
|
---|
21 | }, [setSearchParams, searchParams]);
|
---|
22 |
|
---|
23 | useEffect(() => {
|
---|
24 | const loadFloors = async () => {
|
---|
25 | const httpService = new HttpService();
|
---|
26 | try {
|
---|
27 | const respFloors = await httpService.get(`${config.my_maps.load}?mapName=${mapName}&username=${username}`);
|
---|
28 | console.log("Floors loaded:", respFloors);
|
---|
29 | setFloors(respFloors);
|
---|
30 | setMapLoaded(true);
|
---|
31 |
|
---|
32 | respFloors.forEach(flr => {
|
---|
33 | parseMapData(flr.mapData,() => true,false)
|
---|
34 | })
|
---|
35 |
|
---|
36 | } catch (e) {
|
---|
37 | console.error("Can't load map:", e.message);
|
---|
38 | }
|
---|
39 | };
|
---|
40 |
|
---|
41 | if (!mapLoaded) {
|
---|
42 | loadFloors();
|
---|
43 |
|
---|
44 | }
|
---|
45 | }, [mapName, username]);
|
---|
46 |
|
---|
47 | // floor change
|
---|
48 | useEffect(() => {
|
---|
49 | if (!mapLoaded || floors === undefined || floors.length === 0) return;
|
---|
50 |
|
---|
51 | const floorNum = parseInt(searchParams.get("floor"));
|
---|
52 | const selectedFloor = floors.find((f) => f.num === floorNum);
|
---|
53 |
|
---|
54 | if (!selectedFloor) {
|
---|
55 | console.error(`Floor ${floorNum} not found`);
|
---|
56 | return;
|
---|
57 | }
|
---|
58 |
|
---|
59 | console.log("Changing to floor:", selectedFloor);
|
---|
60 |
|
---|
61 | const appInstance = new MapBuilder("container", floorNum, mapName);
|
---|
62 | appInstance.loadNewFloor(selectedFloor);
|
---|
63 | setApp(appInstance);
|
---|
64 | console.log("Fchange");
|
---|
65 | triggerMapSave();
|
---|
66 |
|
---|
67 | }, [searchParams, mapLoaded]);
|
---|
68 |
|
---|
69 |
|
---|
70 | const saveFloor = async () => {
|
---|
71 | const payload = app.saveShapeDetails();
|
---|
72 | const httpService = new HttpService(true);
|
---|
73 | try {
|
---|
74 | const responseFloor = await httpService.put(`${config.my_maps.save}?username=${username}`, payload);
|
---|
75 | console.log("FLOORS",floors.length)
|
---|
76 |
|
---|
77 | setFloors((prevFloors) =>
|
---|
78 | prevFloors.map(floor => floor.num === responseFloor.num ? responseFloor : floor)
|
---|
79 | )
|
---|
80 | console.log(responseFloor, "resp in builder");
|
---|
81 | } catch (err) {
|
---|
82 | console.log("ERROR --> Could not Save map --->", err);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | useEffect(() => {
|
---|
88 | if (app) {
|
---|
89 | const handleSaveFloor = () => {
|
---|
90 | console.log("mapsave event triggered");
|
---|
91 | saveFloor();
|
---|
92 | };
|
---|
93 |
|
---|
94 | window.addEventListener("mapsave", handleSaveFloor);
|
---|
95 | return () => {
|
---|
96 | window.removeEventListener("mapsave", handleSaveFloor);
|
---|
97 | };
|
---|
98 | }
|
---|
99 | }, [app]);
|
---|
100 |
|
---|
101 |
|
---|
102 | useEffect(() => {
|
---|
103 | return () => {
|
---|
104 | if (app) {
|
---|
105 | app.clearMap();
|
---|
106 | app.stage.destroy();
|
---|
107 | }
|
---|
108 | };
|
---|
109 | }, [app]);
|
---|
110 |
|
---|
111 | return { app, floors, saveFloor,setFloors};
|
---|
112 | };
|
---|
113 |
|
---|
114 | export default useMapLoader;
|
---|