1 | import React, { useState, useEffect } from "react";
|
---|
2 | import styles from "./RoomTypeModal.module.css";
|
---|
3 |
|
---|
4 | export default function RoomTypeModal(args) {
|
---|
5 | const [modal, setModal] = useState(false);
|
---|
6 | const [roomTypes, setRoomTypes] = useState([]);
|
---|
7 | const [formData, setFormData] = useState({
|
---|
8 | name: "",
|
---|
9 | type: "",
|
---|
10 | description: "",
|
---|
11 | });
|
---|
12 |
|
---|
13 | const toggleModal = () => {
|
---|
14 | setModal(!modal);
|
---|
15 | };
|
---|
16 |
|
---|
17 | // Add new room type to the list
|
---|
18 | const addRoomType = () => {
|
---|
19 | if (!formData.type || roomTypes.includes(formData.type)) return; // Prevent empty or duplicate room types
|
---|
20 | setRoomTypes((prevTypes) => [...prevTypes, formData.type]);
|
---|
21 | setFormData({ ...formData, type: "" }); // Reset type input after adding
|
---|
22 | args.map.addRoomType(formData.type);
|
---|
23 | console.log(args.map.roomTypes);
|
---|
24 | };
|
---|
25 |
|
---|
26 | // Remove room type from the list
|
---|
27 | const removeRoomType = (typeToRemove) => {
|
---|
28 | setRoomTypes((prevTypes) => prevTypes.filter((type) => type !== typeToRemove));
|
---|
29 | };
|
---|
30 |
|
---|
31 | // Handle form input changes
|
---|
32 | const handleInputChange = (e) => {
|
---|
33 | const { name, value } = e.target;
|
---|
34 | setFormData((prevData) => ({
|
---|
35 | ...prevData,
|
---|
36 | [name]: value,
|
---|
37 | }));
|
---|
38 | };
|
---|
39 |
|
---|
40 | // Load room types from local storage on mount
|
---|
41 | // useEffect(() => {
|
---|
42 | // //const storedRoomTypes = JSON.parse(localStorage.getItem("roomTypes")) || [];
|
---|
43 | // if(args && args.map){
|
---|
44 | // setRoomTypes(args.map.roomTypes);
|
---|
45 | // }
|
---|
46 |
|
---|
47 | // }, [args]);
|
---|
48 |
|
---|
49 | // Save room types to local storage whenever they change
|
---|
50 | // useEffect(() => {
|
---|
51 | // localStorage.setItem("roomTypes", JSON.stringify(roomTypes));
|
---|
52 | // }, [roomTypes]);
|
---|
53 |
|
---|
54 | return (
|
---|
55 | <>
|
---|
56 | <button onClick={toggleModal} className={styles.btnModal}>
|
---|
57 | Room Types
|
---|
58 | </button>
|
---|
59 |
|
---|
60 | {modal && (
|
---|
61 | <div className={styles.modal}>
|
---|
62 | <div onClick={toggleModal} className={styles.overlay}></div>
|
---|
63 | <div className={styles.modalContent}>
|
---|
64 | <h2>Manage Room Types</h2>
|
---|
65 |
|
---|
66 | <form className={styles.form}>
|
---|
67 | <div className={styles.formGroup}>
|
---|
68 | <label htmlFor="type">Add New Room Type:</label>
|
---|
69 | <input
|
---|
70 | type="text"
|
---|
71 | id="type"
|
---|
72 | name="type"
|
---|
73 | value={formData.type}
|
---|
74 | onChange={handleInputChange}
|
---|
75 | placeholder="Enter a new room type (e.g., Office, Classroom)"
|
---|
76 | />
|
---|
77 | <button type="button" className={styles.addButton} onClick={addRoomType}>
|
---|
78 | Add Type
|
---|
79 | </button>
|
---|
80 | </div>
|
---|
81 | </form>
|
---|
82 |
|
---|
83 | <h3>Available Room Types:</h3>
|
---|
84 | <ul className={styles.roomTypeList}>
|
---|
85 | {roomTypes.length > 0 ? (
|
---|
86 | roomTypes.map((type, index) => (
|
---|
87 | <li key={index} className={styles.roomTypeItem}>
|
---|
88 | {type}
|
---|
89 | <button className={styles.removeButton} onClick={() => removeRoomType(type)}>
|
---|
90 | Remove
|
---|
91 | </button>
|
---|
92 | </li>
|
---|
93 | ))
|
---|
94 | ) : (
|
---|
95 | <li>No room types available</li>
|
---|
96 | )}
|
---|
97 | </ul>
|
---|
98 |
|
---|
99 | <button className={styles.closeModal} onClick={toggleModal}>
|
---|
100 | CLOSE
|
---|
101 | </button>
|
---|
102 | </div>
|
---|
103 | </div>
|
---|
104 | )}
|
---|
105 | </>
|
---|
106 | );
|
---|
107 | }
|
---|