source: imaps-frontend/src/components/EntranceModal/EntranceModal.jsx@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 7.5 KB
Line 
1import React, { useState, useEffect } from "react";
2import styles from "./EntranceModal.module.css";
3
4export default function EntranceModal(props) {
5 const [modal, setModal] = useState(false);
6 const [room, setRoom] = useState(null);
7 const [pins, setPins] = useState([]);
8
9 const [formData, setFormData] = useState({
10 name: "",
11 connectedRoom: "",
12 description: "",
13 availableRooms: [],
14 availablePins: [],
15 selectedPins: [],
16 isMainEntrance: false,
17 selectedPin: "",
18 });
19
20 const toggleModal = () => {
21 if (modal) {
22 room.info = formData;
23 props.map.updateRoomNames();
24 console.log("Se vikna");
25 }
26 setModal(!modal);
27 };
28
29 const handleInputChange = (e) => {
30 const { name, value, type, checked } = e.target;
31 setFormData((prevData) => ({
32 ...prevData,
33 [name]: type === "checkbox" ? checked : value,
34 }));
35
36 console.log(formData);
37 };
38
39 const addPinToList = () => {
40 if (!formData.selectedPin || pins.includes(formData.selectedPin)) return;
41
42 setPins((prevPins) => {
43 const updatedPins = [...prevPins, formData.selectedPin];
44
45 setFormData((prevFormData) => ({
46 ...prevFormData,
47 selectedPin: "",
48 selectedPins: updatedPins,
49 }));
50
51 return updatedPins;
52 });
53 };
54
55 const removePinFromList = (pinToRemove) => {
56 setPins((prevPins) => {
57 const updatedPins = prevPins.filter((pin) => pin !== pinToRemove);
58 setFormData((prevFormData) => ({
59 ...prevFormData,
60 selectedPins: updatedPins,
61 }));
62 return updatedPins;
63 });
64 props.map.removeConnection(formData.name, pinToRemove);
65 };
66
67 const saveDetails = () => {
68 if (room) {
69 room.info = formData;
70 toggleModal();
71 }
72 };
73
74 useEffect(() => {
75 const openModalHandler = (event) => {
76 const roomObj = event.detail.room;
77 setRoom(roomObj);
78
79 const savedPins = roomObj.info.selectedPins || [];
80
81 setFormData({
82 name: roomObj.info.name || "",
83 connectedRoom: roomObj.info.connectedRoom || "",
84 description: roomObj.info.description || "",
85 availablePins: event.detail.map.getConnections() || [],
86 availableRooms: event.detail.map.getRooms() || [],
87 isMainEntrance: roomObj.info.isMainEntrance || false,
88 selectedPin: "",
89 selectedPins: savedPins,
90 });
91
92 setPins(savedPins);
93 setModal(true);
94 event.detail.map.updateConnections();
95
96 console.log(savedPins, "Loaded pins on modal open");
97 };
98
99 window.addEventListener("openEntranceModalEvent", openModalHandler);
100
101 return () => {
102 window.removeEventListener("openEntranceModalEvent", openModalHandler);
103 };
104 }, []);
105
106 if (modal) {
107 document.body.classList.add(styles.activeModal);
108 } else {
109 document.body.classList.remove(styles.activeModal);
110 }
111
112 return (
113 <>
114 {/* <button onClick={toggleModal} className={styles.btnModal}>
115 Entrance Modal
116 </button> */}
117
118 {modal && (
119 <div className={styles.modal}>
120 <div onClick={toggleModal} className={styles.overlay}></div>
121 <div className={styles.modalContent}>
122 <h2>Enter Entrance Details</h2>
123 <form className={styles.form}>
124 {/* Entrance Name */}
125 <div className={styles.formGroup}>
126 <label htmlFor="name">Name:</label>
127 <input
128 type="text"
129 id="name"
130 name="name"
131 value={formData.name}
132 onChange={handleInputChange}
133 placeholder="Enter the entrance name" // Suggest user input
134 required
135 />
136 </div>
137
138 {/* Select Room for Entrance */}
139 <div className={styles.formGroup}>
140 <label htmlFor="connectedRoom">Select room associated with entrance:</label>
141 <select
142 id="connectedRoom"
143 name="connectedRoom"
144 value={formData.connectedRoom}
145 onChange={handleInputChange}
146 required
147 >
148 <option value="">Select Room</option>
149 {formData.availableRooms.map((room, index) => (
150 <option key={index} value={room.name}>
151 {room.name}
152 </option>
153 ))}
154 </select>
155 </div>
156
157 {/* Pins Dropdown for Hallway Navigation */}
158 <div className={styles.formGroup}>
159 <label htmlFor="selectedPin">Select connections:</label>
160 <select
161 id="selectedPin"
162 name="selectedPin"
163 value={formData.selectedPin}
164 onChange={handleInputChange}
165 required
166 >
167 <option value="">Select Connection</option>
168 {formData.availablePins
169 .filter(
170 (pin) =>
171 formData.selectedPins.includes(pin.name) === false &&
172 pin.name !== "" &&
173 pin.name !== formData.name
174 )
175 .map((pin, index) => (
176 <option key={index} value={pin.name}>
177 {pin.name}
178 </option>
179 ))}
180 </select>
181 <button type="button" onClick={addPinToList} className={styles.addButton}>
182 Add Connection
183 </button>
184 </div>
185
186 {/* Display added pins */}
187 <h3>Connections:</h3>
188 <ul className={styles.pinList}>
189 {pins.length > 0 ? (
190 pins.map((pin, index) => (
191 <li key={index} className={styles.pinItem}>
192 {pin}
193 <button onClick={() => removePinFromList(pin)} className={styles.removeButton}>
194 Remove
195 </button>
196 </li>
197 ))
198 ) : (
199 <li>No connections added</li>
200 )}
201 </ul>
202 <br />
203
204 {/* Description */}
205 <div className={styles.formGroup}>
206 <label htmlFor="description">Description:</label>
207 <textarea
208 id="description"
209 name="description"
210 value={formData.description}
211 onChange={handleInputChange}
212 rows="3"
213 />
214 </div>
215
216 {/* Main Entrance Checkbox */}
217 <div className={styles.formGroupCheckbox}>
218 <input
219 type="checkbox"
220 id="isMainEntrance"
221 name="isMainEntrance"
222 checked={formData.isMainEntrance}
223 onChange={handleInputChange}
224 />
225 <label htmlFor="isMainEntrance">Is Main Entrance?</label>
226 </div>
227
228 {/* Save Button */}
229 <div className={styles.formGroup}>
230 <button
231 type="button"
232 id="submit-details"
233 onClick={saveDetails}
234 className={styles.submitButton}
235 >
236 Save
237 </button>
238 </div>
239 </form>
240 <button className={styles.closeModal} onClick={toggleModal}>
241 CLOSE
242 </button>
243 </div>
244 </div>
245 )}
246 </>
247 );
248}
Note: See TracBrowser for help on using the repository browser.