source: imaps-frontend/src/pages/MapView/MapView.jsx

main
Last change on this file was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 9.7 KB
Line 
1import {useNavigate, useParams, useSearchParams} from "react-router-dom";
2import React, {useContext, useEffect, useRef, useState} from "react";
3import {MapDisplay} from "../../scripts/main/MapDisplay.js";
4import styles from "./MapView.module.css";
5import SearchBar from "../../components/SearchBar/SearchBar.jsx";
6import FilterBar from "../../components/FilterBar/FilterBar.jsx";
7import Profile from "../../components/Profile/Profile.jsx";
8import RoomInfoPanel from "../../components/RoomInfoPanel/RoomInfoPanel.jsx";
9import HttpService from "../../scripts/net/HttpService.js";
10import floorIcon from "../../assets/floor_icon.png";
11import Logo from "../../components/Logo/Logo.jsx";
12import config from "../../scripts/net/netconfig.js";
13import parseMapData from "../../scripts/util/parseMapData.js";
14import ShapeRegistry from "../../scripts/util/ShapeRegistry.js";
15import {useAppContext} from "../../components/AppContext/AppContext.jsx";
16
17const MapView = ({isPrivate}) => {
18 const {mapName} = useParams();
19 const {username} = useAppContext();
20
21 const [mapLoaded, setMapLoaded] = useState(false);
22 const [app, setApp] = useState(null);
23 const [isPanelOpen, setIsPanelOpen] = useState(true);
24 const [selectedRoom, setSelectedRoom] = useState(null);
25 const [floors, setFloors] = useState([]); // ova trebit da sa objekti
26 const navigate = useNavigate();
27 const [shapes, setShapes] = useState([]);
28 const [searchParams, setSearchParams] = useSearchParams();
29 const [mainEntrance, setMainEntrance] = useState({});
30
31 const defaultNavObj = {
32 enabled: false,
33 nextFloor: 0,
34 nodes: [],
35 offset: 0
36 }
37
38 const [navNext, setNavNext] = useState(defaultNavObj)
39 const [roomTypes, setRoomTypes] = useState([]);
40
41 useEffect(() => {
42 const loadRoomTypes = async () => {
43 const httpService = new HttpService();
44 let roomTypes;
45 if (isPrivate) {
46 roomTypes = await httpService.get(`${config.room_types.display(true)}?mapName=${mapName}&username=${username}`)
47 } else {
48 roomTypes = await httpService.get(`${config.room_types.display(false)}?mapName=${mapName}`)
49 }
50
51 console.log("loaded ROOM TYPES: " + roomTypes)
52 setRoomTypes(roomTypes);
53 }
54 loadRoomTypes().then(resp => {
55 console.log("LOADED ROOM TYPES")
56 })
57 }, []);
58
59
60 useEffect(() => {
61 if (!searchParams.has("floor")) {
62 setSearchParams({floor: "0"}, {replace: true});
63 }
64 }, [setSearchParams, searchParams]);
65
66
67 useEffect(() => {
68
69
70 const floorNum = parseInt(searchParams.get("floor") || 0);
71 const appInstance = new MapDisplay("map", floorNum);
72
73 const load = async () => {
74 const httpService = new HttpService();
75
76 try {
77 let url;
78 if (isPrivate) {
79 httpService.setAuthenticated();
80 url = `${config.view_maps.load(true)}?mapName=${mapName}&username=${username}`
81 } else {
82 url = `${config.view_maps.load(false)}?mapName=${mapName}`
83 }
84
85 return httpService.get(url);
86 } catch (e) {
87 throw new Error("Can't load map: " + e.message);
88 }
89 };
90
91 load()
92 .then(respFloors => {
93 let tlFloor = respFloors.filter(f => f.num === floorNum)[0];
94
95 let parsedShapes = [];
96
97 respFloors.forEach(flr => {
98 console.log("FLR", flr)
99 const parsed = parseMapData(flr.mapData, (shape => shape.className !== "InfoPin"), true)
100 parsedShapes = [...parsedShapes, ...parsed];
101 })
102
103 setShapes(parsedShapes)
104
105 parsedShapes.forEach(shape => {
106 console.info("PARSED Shapes: " + shape.info.name)
107 })
108
109 const mainEntrance = parsedShapes.find(shape => shape.info.isMainEntrance);
110 setMainEntrance(mainEntrance)
111
112 console.log("MAIN ENTRANCE: " + mainEntrance)
113
114
115 setFloors(respFloors);
116
117 appInstance.loadMapN(tlFloor?.mapData)
118 setApp(appInstance);
119 setMapLoaded(true);
120 })
121 .catch(reason => {
122 console.log("ERROR LOADING MAP VIEW: " + reason)
123 });
124
125
126 }, []);
127
128 //nova load, ne vo MapDisplay
129
130 //loso
131 useEffect(() => {
132 const openRoomInfoPanel = (e) => {
133 setSelectedRoom(e.detail.room);
134 setIsPanelOpen(true)
135 console.log("SHAPES REG", ShapeRegistry.getShapes().length)
136
137 }
138 window.addEventListener("openRoomInfoPanel", openRoomInfoPanel);
139
140 return () => window.removeEventListener("openRoomInfoPanel", openRoomInfoPanel);
141 }, [])
142
143 const handleDirectionsSubmit = (fromSearch = null, toSearch = null) => {
144
145 if (fromSearch === null && toSearch === null) {
146 return;
147 }
148 if (fromSearch === null || fromSearch === "") {
149 fromSearch = mainEntrance.info.name;
150 }
151 let shapeFrom = shapes.find(sh => sh.info.name === fromSearch)
152
153
154 if (shapeFrom.floorNum != searchParams.get("floor")) {
155 handleFloorChange(shapeFrom.floorNum);
156 }
157
158 const httpService = new HttpService();
159
160 if (isPrivate) httpService.setAuthenticated();
161
162 const fromEncoded = encodeURIComponent(fromSearch).trimEnd()
163 const toEncoded = encodeURIComponent(toSearch).trimEnd()
164
165 httpService.get(`${config.view_maps.navigate}?from=${fromEncoded}&to=${toEncoded}`).then(path => {
166 app.drawRouteNEW(path);
167 }).catch(reason => {
168 console.log("err", reason)
169 })
170 };
171
172
173
174 const multiFloorNavigate = () => {
175 if (navNext && app) {
176 console.log("NAVNEXT F: " + navNext.nextFloor)
177 const f = navNext.nextFloor;
178 handleFloorChange(f)
179 setTimeout(() => {
180 app.drawRouteNEW(navNext.nodes, navNext.offset)
181 }, 50)
182 setNavNext(defaultNavObj)
183
184 }
185 }
186
187
188 useEffect(() => {
189
190 const handleNavigate = (event) => {
191 console.log("SHAPES NAV", shapes)
192
193 setNavNext({
194 enabled: true,
195 nextFloor: event.detail.changeFloorTo,
196 nodes: event.detail.nodes,
197 offset: event.detail.offset
198 })
199
200 console.log("ROUTE LAYER", app.routeLayer);
201 }
202
203 window.addEventListener("navigate", handleNavigate)
204 return () => {
205 window.removeEventListener("navigate", handleNavigate);
206 };
207
208 }, [app, mapLoaded]);
209
210 const handleFloorChange = (floorNum) => {
211 setSearchParams({floor: floorNum}, {replace: true});
212 const chFloor = floors.find(floor => floor.num === floorNum)
213
214 console.log("FLOOR NUM:", floorNum, "CHFLOOR:", chFloor)
215 app.clearRoute()
216 app.loadMapN(chFloor.mapData)
217 app.floorNum = floorNum;
218
219
220 console.log(`Floor changed to: ${floorNum}`);
221 };
222
223 const closePanel = () => {
224 setIsPanelOpen(false);
225 selectedRoom.unHighlight()
226 };
227
228
229 return (
230 <div id="main" className={styles.main}>
231 {navNext.enabled && (
232 <div className={styles.nextButtonContainer}>
233 <button onClick={multiFloorNavigate} className={styles.nextButton}>
234 NEXT FLOOR
235 </button>
236 </div>
237 )}
238
239 <div id="map" className={styles.mapContainer}></div>
240
241 <RoomInfoPanel isOpen={isPanelOpen} onClose={closePanel} floor={searchParams.get("floor")}
242 room={selectedRoom} handleDirectionsSubmit={handleDirectionsSubmit}/>
243 <div className={styles.toolbar}>
244 <div className={styles.toolbarContainer}>
245 <div className={styles.logoContainer}>
246 <Logo position="relative"/>
247 </div>
248 <h1 className={styles.mapTitle}>{mapName}</h1>
249 {mapLoaded && app && (
250 <div className={styles.searchFilterContainer}>
251 <SearchBar
252 map={app}
253 handleDirectionsSubmit={handleDirectionsSubmit}
254 setIsPanelOpen={setIsPanelOpen}
255 setSelectedRoom={setSelectedRoom}
256 availableShapes={shapes}
257 handleFloorChange={handleFloorChange}
258 />
259 <FilterBar map={app} roomTypes={roomTypes}/>
260 </div>
261 )}
262 <div className={styles.profileContainer}>
263 <Profile position="relative"/>
264 </div>
265 </div>
266 </div>
267
268 <div className={styles.floorSelectorContainer}>
269 <div className={styles.floorSelector}>
270 <img src={floorIcon} alt="Floor Icon" className={styles.floorIcon}/>
271 <select
272 value={searchParams.get("floor")}
273 onChange={(e) => handleFloorChange(parseInt(e.target.value, 10))}
274 className={styles.floorDropdown}
275 >
276 {floors?.map((floor) => (
277 <option key={floor.num} value={floor.num}>
278 Floor {floor.num}
279 </option>
280 ))}
281 </select>
282 </div>
283 </div>
284
285 </div>
286 );
287};
288
289export default MapView;
Note: See TracBrowser for help on using the repository browser.