1 | import {useContext, useEffect, useState} from "react";
|
---|
2 | import styles from "./Draw.module.css";
|
---|
3 | import RoomModal from "../../components/Modals/RoomModal/RoomModal.jsx";
|
---|
4 | import EntranceModal from "../../components/Modals/EntranceModal/EntranceModal.jsx";
|
---|
5 | import DrawGuide from "../../components/DrawGuide/DrawGuide.jsx";
|
---|
6 | import RoomTypeModal from "../../components/Modals/RoomTypeModal/RoomTypeModal.jsx";
|
---|
7 | import InfoPinModal from "../../components/Modals/InfoPinModal/InfoPinModal.jsx";
|
---|
8 | import SaveMap from "../../components/SaveMap/SaveMap.jsx";
|
---|
9 | import Logo from "../../components/Logo/Logo.jsx";
|
---|
10 | import {Link, useNavigate, useParams, useSearchParams} from "react-router-dom";
|
---|
11 | import Profile from "../../components/Profile/Profile.jsx";
|
---|
12 | import HttpService from "../../scripts/net/HttpService.js";
|
---|
13 | import StairsModal from "../../components/Modals/StairsModal/StairsModal.jsx";
|
---|
14 | import useMapLoader from "./Hooks/useMapLoader.js";
|
---|
15 | import {FloorSelector} from "./FloorSelector.jsx";
|
---|
16 | import {useRoomTypesLoader} from "./Hooks/useRoomTypesLoader.js";
|
---|
17 | import {useAppContext} from "../../components/AppContext/AppContext.jsx";
|
---|
18 | import config from "../../scripts/net/netconfig.js";
|
---|
19 | import ShapeRegistry from "../../scripts/util/ShapeRegistry.js";
|
---|
20 | import {TopPanel} from "./TopPanel/TopPanel.jsx";
|
---|
21 |
|
---|
22 | function Draw() {
|
---|
23 | const {mapName} = useParams();
|
---|
24 | const {username} = useAppContext();
|
---|
25 |
|
---|
26 | const [isPopupVisible, setIsPopupVisible] = useState(false);
|
---|
27 | const [errorMessage, setErrorMessage] = useState("Error");
|
---|
28 | const [hasError, setHasError] = useState(false);
|
---|
29 | const [searchParams, setSearchParams] = useSearchParams();
|
---|
30 |
|
---|
31 | const [roomTypes, setRoomTypes] = useState([]);
|
---|
32 |
|
---|
33 | const {app, floors, saveFloor, setFloors} = useMapLoader(mapName, username, searchParams, setSearchParams)
|
---|
34 | const {addRoomType} = useRoomTypesLoader(setRoomTypes, mapName, username);
|
---|
35 |
|
---|
36 | const addFloorHandler = async (newFloorNum) => {
|
---|
37 | const httpService = new HttpService();
|
---|
38 | httpService.setAuthenticated();
|
---|
39 |
|
---|
40 | const payload = {
|
---|
41 | num: newFloorNum,
|
---|
42 | mapName: mapName,
|
---|
43 | };
|
---|
44 |
|
---|
45 | try {
|
---|
46 | await httpService.put(`${config.floors.add}`, payload);
|
---|
47 | console.log(`Added floor ${newFloorNum}`);
|
---|
48 | setFloors((prevFloors) => [...prevFloors, {num: newFloorNum}]);
|
---|
49 | } catch (error) {
|
---|
50 | console.error("Error adding floor:", error);
|
---|
51 | }
|
---|
52 | };
|
---|
53 |
|
---|
54 | const deleteFloorHandler = async (floorNum) => {
|
---|
55 | if (floorNum === 0) return
|
---|
56 |
|
---|
57 | const httpService = new HttpService();
|
---|
58 | httpService.setAuthenticated();
|
---|
59 |
|
---|
60 | try {
|
---|
61 | await httpService.delete(`${config.floors.delete}?floorNum=${floorNum}&mapName=${mapName}`);
|
---|
62 | setFloors((prevFloors) => prevFloors.filter(f => f.num !== floorNum))
|
---|
63 |
|
---|
64 | const currFloor = searchParams.get("floor");
|
---|
65 | if (currFloor == floorNum) {
|
---|
66 | setSearchParams({floor: "0"}, {replace: true})
|
---|
67 | }
|
---|
68 | console.log(`Deleted floor ${floorNum}`);
|
---|
69 | } catch (error) {
|
---|
70 | console.error("Error deleting floor:", error);
|
---|
71 | }
|
---|
72 | };
|
---|
73 |
|
---|
74 | useEffect(() => {
|
---|
75 | return () => {
|
---|
76 | ShapeRegistry.clear();
|
---|
77 | }
|
---|
78 | }, []);
|
---|
79 |
|
---|
80 |
|
---|
81 | const handleSaveClick = async () => {
|
---|
82 | saveFloor()
|
---|
83 | setIsPopupVisible(true);
|
---|
84 | setTimeout(() => {
|
---|
85 | setIsPopupVisible(false);
|
---|
86 | },
|
---|
87 | 3000);
|
---|
88 | };
|
---|
89 |
|
---|
90 | return (
|
---|
91 | <div className={styles.wrapper} id="wrapper">
|
---|
92 | {/* <SideBar></SideBar> */}
|
---|
93 | <Logo></Logo>
|
---|
94 | <div id="container" className={styles.cont}></div>
|
---|
95 | <div className={styles.panel}>
|
---|
96 | <div className={styles.topPanelH}>
|
---|
97 | <Profile position="inline"></Profile>
|
---|
98 | </div>
|
---|
99 | <Link to={`/myMaps/View/${mapName}`} className={styles.titleLink}>
|
---|
100 | <h1 className={styles.title}>{mapName}</h1>
|
---|
101 | </Link>
|
---|
102 | <div className={styles.guideWrapper}>
|
---|
103 | <DrawGuide/>
|
---|
104 | </div>
|
---|
105 | <hr/>
|
---|
106 | <br/>
|
---|
107 | {/* {<h2 className={styles.paragraph}>Objects:</h2>} */}
|
---|
108 | <ul className={styles.shapeOptions} id="shapeOptions">
|
---|
109 | <li data-info="Entrance" className={`${styles.shapeOption} ${styles.entrance}`}></li>
|
---|
110 | <li data-info="Wall" className={`${styles.shapeOption} ${styles.wall}`} id="wall"></li>
|
---|
111 | <li data-info="Room" className={`${styles.shapeOption} ${styles.room}`} id="room"></li>
|
---|
112 | <li data-info="Stairs" className={`${styles.shapeOption} ${styles.stairs}`} id="stairs"></li>
|
---|
113 | </ul>
|
---|
114 | <RoomTypeModal map={app} roomTypes={roomTypes} addRoomTypeDB={addRoomType}></RoomTypeModal>
|
---|
115 | <br/>
|
---|
116 | <hr/>
|
---|
117 | <br/>
|
---|
118 | <FloorSelector floorConfig={{
|
---|
119 | floors, searchParams,
|
---|
120 | setSearchParams, addFloorHandler,
|
---|
121 | deleteFloorHandler
|
---|
122 | }}></FloorSelector>
|
---|
123 |
|
---|
124 | <br/>
|
---|
125 |
|
---|
126 | <hr/>
|
---|
127 | <br/>
|
---|
128 | {hasError && <p style={{color: "red", textAlign: "center"}}>{errorMessage}</p>}
|
---|
129 | <div className={styles.templateCont}>
|
---|
130 | <SaveMap submitHandler={handleSaveClick}></SaveMap>
|
---|
131 | </div>
|
---|
132 |
|
---|
133 | <div className={styles.hide}>
|
---|
134 | <RoomModal map={app} roomTypes={roomTypes}></RoomModal>
|
---|
135 | <EntranceModal map={app}></EntranceModal>
|
---|
136 | <InfoPinModal map={app}></InfoPinModal>
|
---|
137 | <StairsModal map={app}></StairsModal>
|
---|
138 | </div>
|
---|
139 | </div>
|
---|
140 |
|
---|
141 | {isPopupVisible && (
|
---|
142 | <div className={styles.popup}>
|
---|
143 | <div className={styles.popupContent}>
|
---|
144 | <h2>Map Saved!</h2>
|
---|
145 | <p>Your map has been successfully saved.</p>
|
---|
146 | </div>
|
---|
147 | </div>
|
---|
148 | )}
|
---|
149 | </div>
|
---|
150 | );
|
---|
151 | }
|
---|
152 |
|
---|
153 | export default Draw;
|
---|