1 | import React, { useState, useEffect } from "react";
|
---|
2 | import styles from "./InfoPinModal.module.css"; // Reusing the same styles
|
---|
3 |
|
---|
4 | export default function InfoPinModal(props) {
|
---|
5 | const [modal, setModal] = useState(false);
|
---|
6 | const [pins, setPins] = useState([]);
|
---|
7 | const [room, setRoom] = useState(null);
|
---|
8 |
|
---|
9 | const [formData, setFormData] = useState({
|
---|
10 | name: "",
|
---|
11 | description: "",
|
---|
12 | selectedPin: "",
|
---|
13 | availablePins: [],
|
---|
14 | selectedPins: [],
|
---|
15 | });
|
---|
16 |
|
---|
17 | const toggleModal = () => {
|
---|
18 | if (modal) {
|
---|
19 | room.info = formData;
|
---|
20 | props.map.updateRoomNames();
|
---|
21 | }
|
---|
22 | setModal(!modal);
|
---|
23 | };
|
---|
24 |
|
---|
25 | const handleInputChange = (e) => {
|
---|
26 | const { name, value } = e.target;
|
---|
27 | setFormData((prevData) => ({
|
---|
28 | ...prevData,
|
---|
29 | [name]: value,
|
---|
30 | }));
|
---|
31 | };
|
---|
32 |
|
---|
33 | const addPinToList = () => {
|
---|
34 | if (!formData.selectedPin || pins.includes(formData.selectedPin)) return;
|
---|
35 |
|
---|
36 | setPins((prevPins) => {
|
---|
37 | const updatedPins = [...prevPins, formData.selectedPin];
|
---|
38 |
|
---|
39 | setFormData((prevFormData) => ({
|
---|
40 | ...prevFormData,
|
---|
41 | selectedPin: "",
|
---|
42 | selectedPins: updatedPins,
|
---|
43 | }));
|
---|
44 |
|
---|
45 | return updatedPins;
|
---|
46 | });
|
---|
47 | };
|
---|
48 |
|
---|
49 | const removePinFromList = (pinToRemove) => {
|
---|
50 | setPins((prevPins) => {
|
---|
51 | const updatedPins = prevPins.filter((pin) => pin !== pinToRemove);
|
---|
52 |
|
---|
53 | setFormData((prevFormData) => ({
|
---|
54 | ...prevFormData,
|
---|
55 | selectedPins: updatedPins,
|
---|
56 | }));
|
---|
57 |
|
---|
58 | props.map.removeConnection(formData.name, pinToRemove);
|
---|
59 | return updatedPins;
|
---|
60 | });
|
---|
61 | };
|
---|
62 |
|
---|
63 | const saveDetails = () => {
|
---|
64 | room.info = formData;
|
---|
65 | toggleModal();
|
---|
66 | };
|
---|
67 |
|
---|
68 | useEffect(() => {
|
---|
69 | const openModalHandler = (event) => {
|
---|
70 | const roomObj = event.detail.room;
|
---|
71 |
|
---|
72 | setRoom(roomObj);
|
---|
73 |
|
---|
74 | // Populate formData and pins based on the room information
|
---|
75 | setFormData({
|
---|
76 | name: roomObj.info.name || "", // Room name
|
---|
77 | description: roomObj.info.description || "",
|
---|
78 | selectedPin: "",
|
---|
79 | availablePins: event.detail.map.getConnections(),
|
---|
80 | selectedPins: roomObj.info.selectedPins,
|
---|
81 | });
|
---|
82 |
|
---|
83 | setPins(roomObj.info.selectedPins || []); // Set the already connected pins
|
---|
84 |
|
---|
85 | setModal(true); // Open the modal
|
---|
86 | event.detail.map.updateConnections();
|
---|
87 | };
|
---|
88 |
|
---|
89 | // Add event listener to open modal
|
---|
90 | window.addEventListener("openPinModalEvent", openModalHandler);
|
---|
91 |
|
---|
92 | return () => {
|
---|
93 | // Cleanup the event listener when the component unmounts
|
---|
94 | window.removeEventListener("openPinModalEvent", openModalHandler);
|
---|
95 | };
|
---|
96 | }, []);
|
---|
97 |
|
---|
98 | if (modal) {
|
---|
99 | document.body.classList.add(styles.activeModal);
|
---|
100 | } else {
|
---|
101 | document.body.classList.remove(styles.activeModal);
|
---|
102 | }
|
---|
103 |
|
---|
104 | return (
|
---|
105 | <>
|
---|
106 | {/* <button onClick={toggleModal} className={styles.btnModal}>
|
---|
107 | Info Pin Modal
|
---|
108 | </button> */}
|
---|
109 |
|
---|
110 | {modal && (
|
---|
111 | <div className={styles.modal}>
|
---|
112 | <div onClick={toggleModal} className={styles.overlay}></div>
|
---|
113 | <div className={styles.modalContent}>
|
---|
114 | <h2>Enter Pin Details</h2>
|
---|
115 | <form className={styles.form}>
|
---|
116 | <div className={styles.formGroup}>
|
---|
117 | <label htmlFor="name">Name:</label>
|
---|
118 | <input
|
---|
119 | type="text"
|
---|
120 | id="name"
|
---|
121 | name="name"
|
---|
122 | value={formData.name}
|
---|
123 | onChange={handleInputChange}
|
---|
124 | placeholder="Enter the pin name"
|
---|
125 | required
|
---|
126 | />
|
---|
127 | </div>
|
---|
128 |
|
---|
129 | <div className={styles.formGroup}>
|
---|
130 | <label htmlFor="selectedPin">Select connections:</label>
|
---|
131 | <select
|
---|
132 | id="selectedPin"
|
---|
133 | name="selectedPin"
|
---|
134 | value={formData.selectedPin}
|
---|
135 | onChange={handleInputChange}
|
---|
136 | required
|
---|
137 | >
|
---|
138 | <option value="">Select Connection</option>
|
---|
139 | {formData.availablePins
|
---|
140 | .filter(
|
---|
141 | (pin) =>
|
---|
142 | formData.selectedPins.includes(pin.name) == false &&
|
---|
143 | pin.name != "" &&
|
---|
144 | pin.name != formData.name
|
---|
145 | )
|
---|
146 | .map((pin, index) => (
|
---|
147 | <option key={index} value={pin.name}>
|
---|
148 | {pin.name}
|
---|
149 | </option>
|
---|
150 | ))}
|
---|
151 | </select>
|
---|
152 | <button type="button" onClick={addPinToList} className={styles.addButton}>
|
---|
153 | Add Connection
|
---|
154 | </button>
|
---|
155 | </div>
|
---|
156 |
|
---|
157 | <h3>Connections:</h3>
|
---|
158 | <ul className={styles.pinList}>
|
---|
159 | {pins.length > 0 ? (
|
---|
160 | pins.map((pin, index) => (
|
---|
161 | <li key={index} className={styles.pinItem}>
|
---|
162 | {pin}
|
---|
163 | <button onClick={() => removePinFromList(pin)} className={styles.removeButton}>
|
---|
164 | Remove
|
---|
165 | </button>
|
---|
166 | </li>
|
---|
167 | ))
|
---|
168 | ) : (
|
---|
169 | <li>No connections added</li>
|
---|
170 | )}
|
---|
171 | </ul>
|
---|
172 | <br />
|
---|
173 | <div className={styles.formGroup}>
|
---|
174 | <label htmlFor="description">Description:</label>
|
---|
175 | <textarea
|
---|
176 | id="description"
|
---|
177 | name="description"
|
---|
178 | value={formData.description}
|
---|
179 | onChange={handleInputChange}
|
---|
180 | rows="3"
|
---|
181 | placeholder="Enter description for the pin"
|
---|
182 | />
|
---|
183 | </div>
|
---|
184 |
|
---|
185 | <div className={styles.formGroup}>
|
---|
186 | <button
|
---|
187 | type="button"
|
---|
188 | id="submit-details"
|
---|
189 | onClick={saveDetails}
|
---|
190 | className={styles.submitButton}
|
---|
191 | >
|
---|
192 | Save
|
---|
193 | </button>
|
---|
194 | </div>
|
---|
195 | </form>
|
---|
196 | <button className={styles.closeModal} onClick={toggleModal}>
|
---|
197 | CLOSE
|
---|
198 | </button>
|
---|
199 | </div>
|
---|
200 | </div>
|
---|
201 | )}
|
---|
202 | </>
|
---|
203 | );
|
---|
204 | }
|
---|