source: imaps-frontend/src/pages/AdminPage/AdminPage.jsx@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 8.1 KB
Line 
1import React, {useEffect, useState} from "react";
2import styles from "./AdminPage.module.css";
3import {TilesContainer} from "react-tiles-dnd";
4import HttpService from "../../scripts/net/HttpService.js";
5import config from "../../scripts/net/netconfig.js";
6import card from "../../assets/card-map.png";
7import MapInfoModal from "../../components/MapInfoModal/MapInfoModal.jsx";
8import {useAppContext} from "../../components/AppContext/AppContext.jsx";
9import PublishForm from "../../components/PublishForm/PublishForm.jsx";
10import Logo from "../../components/Logo/Logo.jsx";
11import Profile from "../../components/Profile/Profile.jsx";
12import Toast from "../../components/Toast/Toast.jsx";
13
14const renderTile = ({data, isDragging}, handleApprove, handleDeny, openMapInfoModal, openPublishForm) => (
15 <div className={`${styles.tile} ${isDragging ? styles.dragging : ""}`} onClick={() => openMapInfoModal(data)}>
16 <img src={card} className={styles.imgStyle} alt="Map Thumbnail"/>
17 <div className={styles.mapTitle}>{data.mapName}</div>
18 <button className={styles.viewPublishFormButton} onClick={(e) => {
19 e.stopPropagation();
20 openPublishForm(data);
21 }}>
22 View Form
23 </button>
24 <div className={styles.buttonContainer}>
25 {/*<input*/}
26 {/* type="text"*/}
27 {/* placeholder="Reason"*/}
28 {/* className={styles.reasonInput}*/}
29 {/* onClick={(e) => e.stopPropagation()}*/}
30 {/*/>*/}
31 {/*<div className={styles.buttonsGroup}>*/}
32 {/* <button className={styles.approveButton} onClick={(e) => {*/}
33 {/* e.stopPropagation();*/}
34 {/* handleApprove(data.mapName);*/}
35 {/* }}>*/}
36 {/* Approve*/}
37 {/* </button>*/}
38 {/* <button className={styles.denyButton} onClick={(e) => {*/}
39 {/* e.stopPropagation();*/}
40 {/* const reason = e.target.closest('div').previousSibling.value;*/}
41 {/* handleDeny(data.mapName, reason);*/}
42 {/* }}>*/}
43 {/* Deny*/}
44 {/* </button>*/}
45
46 {/*</div>*/}
47 </div>
48 </div>
49);
50
51const tileSize = (tile) => ({
52 colSpan: tile.cols,
53 rowSpan: tile.rows,
54});
55
56export default function AdminPage() {
57 const [pendingMaps, setPendingMaps] = useState([]);
58 const [selectedMap, setSelectedMap] = useState(null);
59 const [isMapInfoModalOpen, setIsMapInfoModalOpen] = useState(false);
60 const [publishFormMap, setPublishFormMap] = useState(null);
61 const [publishRequests, setPublishRequests] = useState([]);
62 const [currentForm, setCurrentForm] = useState({});
63 const {username} = useAppContext();
64
65 const [toastMessage, setToastMessage] = useState(null);
66 const [toastType, setToastType] = useState(1);
67
68 useEffect(() => {
69 const loadPendingMaps = async () => {
70 const httpService = new HttpService();
71 httpService.setAuthenticated();
72
73 const respMaps = await httpService.get(`${config.admin.display}`);
74
75 const mapTiles = respMaps.map((elem) => ({
76 mapName: elem.mapName,
77 cols: 1,
78 rows: 1,
79 status: elem.mapStatus,
80 created_at: elem.createdAt,
81 modified_at: elem.modifiedAt,
82 gmaps_url: elem.gmaps_url,
83 image_url: card,
84 })).filter((tile) => tile.status === "INVALID");
85
86 setPendingMaps(mapTiles);
87 };
88
89 loadPendingMaps();
90 }, [username]);
91
92 useEffect(() => {
93 const loadPublishRequests = async () => {
94 const httpService = new HttpService(true);
95 const respPr = await httpService.get(`${config.admin.load_pr}`)
96
97 setPublishRequests(respPr);
98 }
99
100 loadPublishRequests();
101 }, []);
102
103 const handleApprove = async (id,mapName) => {
104 const httpService = new HttpService();
105 httpService.setAuthenticated();
106 const url = `${config.admin.approve_pr}?id=${id}`;
107 closePublishForm()
108 try {
109 await httpService.post(url);
110 setPendingMaps((prev) => prev.filter((map) => map.mapName !== mapName));
111 showToast(`Publish Request "${id}" approved.`, 1)
112 } catch (error) {
113 console.error("Error approving pr:", error);
114 // alert("Failed to approve pr.");
115 showToast("Failed to approve pr.", 0)
116 }
117 };
118
119 const showToast = (message, type = 1) => {
120 setToastMessage(message);
121 setToastType(type);
122 setTimeout(() => setToastMessage(null), 3000); // Automatically hide the toast after 3 seconds
123 };
124
125 const handleDeny = async (id, mapName,reason) => {
126 const httpService = new HttpService();
127 httpService.setAuthenticated();
128 const url = `${config.admin.deny_pr}?id=${id}&reason=${encodeURIComponent(reason)}`;
129 closePublishForm()
130 try {
131 await httpService.post(url);
132 setPendingMaps((prev) => prev.filter((map) => map.mapName !== mapName));
133 showToast(`Publish request ${id} denied.`, 1)
134 } catch (error) {
135 console.error("Error denying pr:", error);
136 alert("Failed to deny pr.");
137 showToast("Failed to deny pr.", 0)
138 }
139 };
140
141 const handlePublishFormSubmit = async (formData) => {
142 const httpService = new HttpService();
143 httpService.setAuthenticated();
144 const url = `${config.admin.approve_pr}?mapName=${formData.mapName}`;
145
146 try {
147 await httpService.post(url, formData); // Assuming formData contains all required fields
148 setPendingMaps((prev) => prev.filter((map) => map.mapName !== formData.mapName));
149 alert(`Map "${formData.mapName}" published successfully.`);
150 closePublishForm();
151 } catch (error) {
152 console.error("Error publishing map:", error);
153 alert("Failed to publish map. Please try again.");
154 }
155 };
156
157
158 const openMapInfoModal = (map) => {
159 setSelectedMap(map);
160 setIsMapInfoModalOpen(true);
161 };
162
163 const closeMapInfoModal = () => {
164 setIsMapInfoModalOpen(false);
165 setSelectedMap(null);
166 };
167
168 const openPublishForm = (data) => {
169 console.log("DATA MAP NAME", data.mapName)
170 publishRequests.forEach(pr => {
171 console.log("PR: " + JSON.stringify(pr))
172 })
173
174 const pr = publishRequests.find(p => p.mapName === data.mapName);
175 console.log("FOUND PR: " + pr)
176 setCurrentForm(pr)
177 setPublishFormMap(data);
178 };
179
180 const closePublishForm = () => {
181 setPublishFormMap(null);
182 };
183
184 return (
185 <div className={styles.container}>
186 <Logo></Logo>
187 <Profile></Profile>
188 <h1>Pending Maps for Approval</h1>
189
190 {publishFormMap && (
191 <PublishForm
192 isAdmin={true}
193 formData={currentForm}
194 onSubmit={handlePublishFormSubmit}
195 onCancel={closePublishForm}
196 onApprove={handleApprove}
197 onDeny={handleDeny}
198
199 />
200 )}
201
202
203 <div className={styles.mapsContainer}>
204 <TilesContainer
205 data={pendingMaps}
206 renderTile={(tileProps) => renderTile(tileProps, handleApprove, handleDeny, openMapInfoModal, openPublishForm)}
207 tileSize={tileSize}
208 forceTileWidth={200}
209 forceTileHeight={250}
210 />
211 </div>
212 {isMapInfoModalOpen && (
213 <MapInfoModal
214 isOpen={isMapInfoModalOpen}
215 onClose={closeMapInfoModal}
216 map={selectedMap}
217 onUpdate={() => {
218 }}
219 onDelete={() => {
220 }}
221 published={true}
222 />
223 )}
224 {toastMessage && <Toast message={toastMessage} type={toastType} onClose={() => setToastMessage(null)}/>}
225
226 </div>
227 );
228}
Note: See TracBrowser for help on using the repository browser.