import React, { useState, useEffect } from "react"; import styles from "./EntranceModal.module.css"; export default function EntranceModal(props) { const [modal, setModal] = useState(false); const [room, setRoom] = useState(null); const [pins, setPins] = useState([]); const [formData, setFormData] = useState({ name: "", connectedRoom: "", description: "", availableRooms: [], availablePins: [], selectedPins: [], isMainEntrance: false, selectedPin: "", }); const toggleModal = () => { if (modal) { room.info = formData; props.map.updateRoomNames(); console.log("Se vikna"); } setModal(!modal); }; const handleInputChange = (e) => { const { name, value, type, checked } = e.target; setFormData((prevData) => ({ ...prevData, [name]: type === "checkbox" ? checked : value, })); console.log(formData); }; const addPinToList = () => { if (!formData.selectedPin || pins.includes(formData.selectedPin)) return; setPins((prevPins) => { const updatedPins = [...prevPins, formData.selectedPin]; setFormData((prevFormData) => ({ ...prevFormData, selectedPin: "", selectedPins: updatedPins, })); return updatedPins; }); }; const removePinFromList = (pinToRemove) => { setPins((prevPins) => { const updatedPins = prevPins.filter((pin) => pin !== pinToRemove); setFormData((prevFormData) => ({ ...prevFormData, selectedPins: updatedPins, })); return updatedPins; }); props.map.removeConnection(formData.name, pinToRemove); }; const saveDetails = () => { if (room) { room.info = formData; toggleModal(); } }; useEffect(() => { const openModalHandler = (event) => { const roomObj = event.detail.room; setRoom(roomObj); const savedPins = roomObj.info.selectedPins || []; setFormData({ name: roomObj.info.name || "", connectedRoom: roomObj.info.connectedRoom || "", description: roomObj.info.description || "", availablePins: event.detail.map.getConnections() || [], availableRooms: event.detail.map.getRooms() || [], isMainEntrance: roomObj.info.isMainEntrance || false, selectedPin: "", selectedPins: savedPins, }); setPins(savedPins); setModal(true); event.detail.map.updateConnections(); console.log(savedPins, "Loaded pins on modal open"); }; window.addEventListener("openEntranceModalEvent", openModalHandler); return () => { window.removeEventListener("openEntranceModalEvent", openModalHandler); }; }, []); if (modal) { document.body.classList.add(styles.activeModal); } else { document.body.classList.remove(styles.activeModal); } return ( <> {/* */} {modal && (