1 | import React, { useState, useEffect } from "react";
|
---|
2 | import styles from "./RoomModal.module.css";
|
---|
3 | import { Ring } from "konva/lib/shapes/Ring";
|
---|
4 |
|
---|
5 | export default function RoomModal(props) {
|
---|
6 | const [modal, setModal] = useState(false);
|
---|
7 | const [room, setRoom] = useState(null);
|
---|
8 | const [formData, setFormData] = useState({
|
---|
9 | name: "",
|
---|
10 | type: "",
|
---|
11 | description: "",
|
---|
12 | });
|
---|
13 | const [roomTypes, setRoomTypes] = useState([]);
|
---|
14 |
|
---|
15 | const toggleModal = () => {
|
---|
16 | if(modal) {
|
---|
17 | room.info = formData;
|
---|
18 | props.map.updateRoomNames();
|
---|
19 | }
|
---|
20 | setModal(!modal);
|
---|
21 | };
|
---|
22 |
|
---|
23 | const saveDetails = () => {
|
---|
24 | if (room) {
|
---|
25 | room.info = formData;
|
---|
26 | toggleModal();
|
---|
27 | console.log(room.info);
|
---|
28 | }
|
---|
29 | };
|
---|
30 | // impl da sa gledat dali ti e zacuvana formava
|
---|
31 |
|
---|
32 | const handleInputChange = (e) => {
|
---|
33 | console.log(room.info)
|
---|
34 | const { name, value, type, checked } = e.target;
|
---|
35 | setFormData((prevData) => ({
|
---|
36 | ...prevData,
|
---|
37 | [name]: type === "checkbox" ? checked : value,
|
---|
38 | }));
|
---|
39 | room.info = formData;
|
---|
40 | };
|
---|
41 |
|
---|
42 | useEffect(() => {
|
---|
43 | const openModalHandler = (event) => {
|
---|
44 | const roomObj = event.detail.room;
|
---|
45 | console.log(roomObj, "ROOMOBJ OTV")
|
---|
46 | setRoom(roomObj);
|
---|
47 | setFormData({
|
---|
48 | name: roomObj.info.name,
|
---|
49 | type: roomObj.info.type,
|
---|
50 | description: roomObj.info.description,
|
---|
51 | });
|
---|
52 | setRoomTypes(event.detail.map.getRoomTypes());
|
---|
53 | toggleModal(true);
|
---|
54 | };
|
---|
55 |
|
---|
56 | window.addEventListener("openRoomModalEvent", openModalHandler);
|
---|
57 |
|
---|
58 | return () => {
|
---|
59 | window.removeEventListener("openRoomModalEvent", openModalHandler);
|
---|
60 | };
|
---|
61 | }, []);
|
---|
62 |
|
---|
63 | if (modal) {
|
---|
64 | document.body.classList.add(styles.activeModal);
|
---|
65 | } else {
|
---|
66 | document.body.classList.remove(styles.activeModal);
|
---|
67 | }
|
---|
68 |
|
---|
69 | return (
|
---|
70 | <>
|
---|
71 | {/* <button onClick={toggleModal} className={styles.btnModal}>
|
---|
72 | RoomModal
|
---|
73 | </button> */}
|
---|
74 |
|
---|
75 | {modal && (
|
---|
76 | <div className={styles.modal}>
|
---|
77 | <div onClick={toggleModal} className={styles.overlay}></div>
|
---|
78 | <div className={styles.modalContent}>
|
---|
79 | <h2>Enter Room Details</h2>
|
---|
80 | <form className={styles.form}>
|
---|
81 | <div className={styles.formGroup}>
|
---|
82 | <label htmlFor="name">Name:</label>
|
---|
83 | <input
|
---|
84 | type="text"
|
---|
85 | id="name"
|
---|
86 | name="name"
|
---|
87 | value={formData.name}
|
---|
88 | onChange={handleInputChange}
|
---|
89 | required
|
---|
90 | />
|
---|
91 | </div>
|
---|
92 | <div className={styles.formGroup}>
|
---|
93 | <label htmlFor="type">Type:</label>
|
---|
94 | <select id="type" name="type" onChange={handleInputChange} value={formData.type} required>
|
---|
95 | <option value="">Select Room Type</option>
|
---|
96 | {roomTypes.map((type, index) => (
|
---|
97 | <option key={index} value={type}>
|
---|
98 | {type}
|
---|
99 | </option>
|
---|
100 | ))}
|
---|
101 | </select>
|
---|
102 | </div>
|
---|
103 | <div className={styles.formGroup}>
|
---|
104 | <label htmlFor="description">Description:</label>
|
---|
105 | <textarea
|
---|
106 | id="description"
|
---|
107 | name="description"
|
---|
108 | value={formData.description}
|
---|
109 | onChange={handleInputChange}
|
---|
110 | rows="3"
|
---|
111 | />
|
---|
112 | </div>
|
---|
113 | <div className={styles.formGroup}>
|
---|
114 | <button
|
---|
115 | type="button"
|
---|
116 | id="submit-details"
|
---|
117 | onClick={saveDetails}
|
---|
118 | className={styles.submitButton}
|
---|
119 | >
|
---|
120 | Save
|
---|
121 | </button>
|
---|
122 | </div>
|
---|
123 | </form>
|
---|
124 | <button className={styles.closeModal} onClick={toggleModal}>
|
---|
125 | CLOSE
|
---|
126 | </button>
|
---|
127 | </div>
|
---|
128 | </div>
|
---|
129 | )}
|
---|
130 | </>
|
---|
131 | );
|
---|
132 | }
|
---|