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