import React, { useState, useEffect } from "react"; import styles from "./InfoPinModal.module.css"; // Reusing the same styles export default function InfoPinModal(props) { const [modal, setModal] = useState(false); const [pins, setPins] = useState([]); const [room, setRoom] = useState(null); const [formData, setFormData] = useState({ name: "", description: "", selectedPin: "", availablePins: [], selectedPins: [], }); const toggleModal = () => { if (modal) { room.info = formData; props.map.updateRoomNames(); } setModal(!modal); }; const handleInputChange = (e) => { const { name, value } = e.target; setFormData((prevData) => ({ ...prevData, [name]: value, })); }; 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, })); props.map.removeConnection(formData.name, pinToRemove); return updatedPins; }); }; const saveDetails = () => { room.info = formData; toggleModal(); }; useEffect(() => { const openModalHandler = (event) => { const roomObj = event.detail.room; setRoom(roomObj); // Populate formData and pins based on the room information setFormData({ name: roomObj.info.name || "", // Room name description: roomObj.info.description || "", selectedPin: "", availablePins: event.detail.map.getConnections(), selectedPins: roomObj.info.selectedPins, }); setPins(roomObj.info.selectedPins || []); // Set the already connected pins setModal(true); // Open the modal event.detail.map.updateConnections(); }; // Add event listener to open modal window.addEventListener("openPinModalEvent", openModalHandler); return () => { // Cleanup the event listener when the component unmounts window.removeEventListener("openPinModalEvent", openModalHandler); }; }, []); if (modal) { document.body.classList.add(styles.activeModal); } else { document.body.classList.remove(styles.activeModal); } return ( <> {/* */} {modal && (

Enter Pin Details

Connections:

    {pins.length > 0 ? ( pins.map((pin, index) => (
  • {pin}
  • )) ) : (
  • No connections added
  • )}