1 | import React, { useEffect, useState } from "react";
|
---|
2 | import styles from "./MapInfoModal.module.css";
|
---|
3 | import edit_icon from "../../assets/edit_icon_black.png";
|
---|
4 | import PublishForm from "../PublishForm/PublishForm.jsx";
|
---|
5 | import HttpService from "../../scripts/net/HttpService.js";
|
---|
6 | import config from "../../scripts/net/netconfig.js";
|
---|
7 | import { useAppContext } from "../AppContext/AppContext.jsx";
|
---|
8 | import { useNavigate } from "react-router-dom";
|
---|
9 |
|
---|
10 | export default function MapInfoModal({
|
---|
11 | isOpen,
|
---|
12 | onClose,
|
---|
13 | map,
|
---|
14 | onDelete,
|
---|
15 | onUpdate,
|
---|
16 | onPublish,
|
---|
17 | published = false,
|
---|
18 | }) {
|
---|
19 | const [isEditPopupOpen, setEditPopupOpen] = useState(false);
|
---|
20 | const [editedName, setEditedName] = useState("");
|
---|
21 | const [editedGmapsUrl, setEditedGmapsUrl] = useState("");
|
---|
22 | const [editedType, setEditedType] = useState("");
|
---|
23 | const [publishFormOpen, setPublishFormOpen] = useState(false);
|
---|
24 | const navigate = useNavigate();
|
---|
25 | const [loadedFormData, setLoadedFormData] = useState(null);
|
---|
26 | const { setLoading } = useAppContext();
|
---|
27 | const { loading } = useAppContext();
|
---|
28 | const { username } = useAppContext();
|
---|
29 |
|
---|
30 | useEffect(() => {
|
---|
31 | if (map) {
|
---|
32 | setEditedName(map.mapName || "");
|
---|
33 | setEditedGmapsUrl(map.gmaps_url || "");
|
---|
34 | setEditedType(map.mapType || "");
|
---|
35 | }
|
---|
36 | }, [map, isEditPopupOpen]);
|
---|
37 |
|
---|
38 | if (!isOpen || !map) return null;
|
---|
39 |
|
---|
40 | const handleView = () => {
|
---|
41 | navigate(`/myMaps/View/${map.mapName}`);
|
---|
42 | };
|
---|
43 |
|
---|
44 | const handleEdit = () => {
|
---|
45 | navigate(`/myMaps/Draw/${map.mapName}`);
|
---|
46 | };
|
---|
47 |
|
---|
48 | const handleDelete = () => {
|
---|
49 | if (
|
---|
50 | window.confirm(`Are you sure you want to delete the map "${map.mapName}"?`)
|
---|
51 | ) {
|
---|
52 | onDelete(map.mapName);
|
---|
53 | onClose();
|
---|
54 | }
|
---|
55 | };
|
---|
56 |
|
---|
57 | const openEditPopup = () => {
|
---|
58 | setEditPopupOpen(true);
|
---|
59 | };
|
---|
60 |
|
---|
61 | const closeEditPopup = () => {
|
---|
62 | setEditPopupOpen(false);
|
---|
63 | };
|
---|
64 |
|
---|
65 | const handleEditSubmit = async () => {
|
---|
66 | const updatedMap = {
|
---|
67 | initialName: map.mapName,
|
---|
68 | name: editedName,
|
---|
69 | gmapsUrl: editedGmapsUrl,
|
---|
70 | type: editedType,
|
---|
71 | };
|
---|
72 | onUpdate(updatedMap);
|
---|
73 | closeEditPopup()
|
---|
74 | };
|
---|
75 |
|
---|
76 | return (
|
---|
77 | <div className={styles.modalOverlay} onClick={onClose}>
|
---|
78 | <div
|
---|
79 | className={styles.modalContent}
|
---|
80 | onClick={(e) => e.stopPropagation()}
|
---|
81 | >
|
---|
82 | {!loading && (
|
---|
83 | <>
|
---|
84 | <img
|
---|
85 | src={map.image_url}
|
---|
86 | alt="Map Thumbnail"
|
---|
87 | className={styles.mapImage}
|
---|
88 | />
|
---|
89 | <h2 className={styles.title}>
|
---|
90 | {map.mapName}
|
---|
91 | <img
|
---|
92 | src={edit_icon}
|
---|
93 | alt="Edit"
|
---|
94 | className={styles.editIcon}
|
---|
95 | onClick={openEditPopup}
|
---|
96 | />
|
---|
97 | </h2>
|
---|
98 | <p>
|
---|
99 | <strong>Status:</strong> {map.status}
|
---|
100 | </p>
|
---|
101 | <p>
|
---|
102 | <strong>Created At:</strong>{" "}
|
---|
103 | {new Date(map.created_at).toLocaleString()}
|
---|
104 | </p>
|
---|
105 | <p>
|
---|
106 | <strong>Modified At:</strong>{" "}
|
---|
107 | {new Date(map.modified_at).toLocaleString()}
|
---|
108 | </p>
|
---|
109 | <p>
|
---|
110 | <strong>Published At:</strong>{" "}
|
---|
111 | {map.published_at
|
---|
112 | ? new Date(map.published_at).toLocaleString()
|
---|
113 | : "Not published yet"}
|
---|
114 | </p>
|
---|
115 | <p>
|
---|
116 | <strong>Google Maps URL:</strong>{" "}
|
---|
117 | <a href={`${map.gmaps_url}`} rel="noopener noreferrer">
|
---|
118 | Open in Google Maps
|
---|
119 | </a>
|
---|
120 | </p>
|
---|
121 |
|
---|
122 | <div className={styles.buttonContainer}>
|
---|
123 | <button className={styles.viewButton} onClick={handleView}>
|
---|
124 | View
|
---|
125 | </button>
|
---|
126 | <button className={styles.editButton} onClick={handleEdit}>
|
---|
127 | Edit
|
---|
128 | </button>
|
---|
129 | <button className={styles.deleteButton} onClick={handleDelete}>
|
---|
130 | Delete
|
---|
131 | </button>
|
---|
132 | {!map.is_published && !published && (
|
---|
133 | <button
|
---|
134 | className={styles.publishButton}
|
---|
135 | onClick={() => setPublishFormOpen(true)}
|
---|
136 | >
|
---|
137 | Publish
|
---|
138 | </button>
|
---|
139 | )}
|
---|
140 | </div>
|
---|
141 | <button className={styles.closeButton} onClick={onClose}>
|
---|
142 | Close
|
---|
143 | </button>
|
---|
144 |
|
---|
145 | {isEditPopupOpen && (
|
---|
146 | <div
|
---|
147 | className={styles.editPopupOverlay}
|
---|
148 | onClick={closeEditPopup}
|
---|
149 | >
|
---|
150 | <div
|
---|
151 | className={styles.editPopupContent}
|
---|
152 | onClick={(e) => e.stopPropagation()}
|
---|
153 | >
|
---|
154 | <h3 className={styles.title}>Edit Map Details</h3>
|
---|
155 | <div className={styles.editField}>
|
---|
156 | <label>Map Name:</label>
|
---|
157 | <input
|
---|
158 | type="text"
|
---|
159 | value={editedName}
|
---|
160 | onChange={(e) => setEditedName(e.target.value)}
|
---|
161 | />
|
---|
162 | </div>
|
---|
163 | <div className={styles.editField}>
|
---|
164 | <label>Google Maps URL:</label>
|
---|
165 | <input
|
---|
166 | type="text"
|
---|
167 | value={editedGmapsUrl}
|
---|
168 | onChange={(e) => setEditedGmapsUrl(e.target.value)}
|
---|
169 | />
|
---|
170 | </div>
|
---|
171 | <div className={styles.editField}>
|
---|
172 | <label>Map Type:</label>
|
---|
173 | <input
|
---|
174 | type="text"
|
---|
175 | value={editedType}
|
---|
176 | onChange={(e) => setEditedType(e.target.value)}
|
---|
177 | />
|
---|
178 | </div>
|
---|
179 | <div className={styles.editPopupButtons}>
|
---|
180 | <button
|
---|
181 | className={styles.submitButton}
|
---|
182 | onClick={handleEditSubmit}
|
---|
183 | >
|
---|
184 | Submit
|
---|
185 | </button>
|
---|
186 | <button
|
---|
187 | className={styles.cancelButton}
|
---|
188 | onClick={closeEditPopup}
|
---|
189 | >
|
---|
190 | Cancel
|
---|
191 | </button>
|
---|
192 | </div>
|
---|
193 | </div>
|
---|
194 | </div>
|
---|
195 | )}
|
---|
196 | </>
|
---|
197 | )}
|
---|
198 | </div>
|
---|
199 | </div>
|
---|
200 | );
|
---|
201 | }
|
---|