import React, {useEffect, useState} from "react"; import styles from "./AdminPage.module.css"; import {TilesContainer} from "react-tiles-dnd"; import HttpService from "../../scripts/net/HttpService.js"; import config from "../../scripts/net/netconfig.js"; import card from "../../assets/card-map.png"; import MapInfoModal from "../../components/MapInfoModal/MapInfoModal.jsx"; import {useAppContext} from "../../components/AppContext/AppContext.jsx"; import PublishForm from "../../components/PublishForm/PublishForm.jsx"; import Logo from "../../components/Logo/Logo.jsx"; import Profile from "../../components/Profile/Profile.jsx"; import Toast from "../../components/Toast/Toast.jsx"; const renderTile = ({data, isDragging}, handleApprove, handleDeny, openMapInfoModal, openPublishForm) => (
openMapInfoModal(data)}> Map Thumbnail
{data.mapName}
{/* e.stopPropagation()}*/} {/*/>*/} {/*
*/} {/* */} {/* */} {/*
*/}
); const tileSize = (tile) => ({ colSpan: tile.cols, rowSpan: tile.rows, }); export default function AdminPage() { const [pendingMaps, setPendingMaps] = useState([]); const [selectedMap, setSelectedMap] = useState(null); const [isMapInfoModalOpen, setIsMapInfoModalOpen] = useState(false); const [publishFormMap, setPublishFormMap] = useState(null); const [publishRequests, setPublishRequests] = useState([]); const [currentForm, setCurrentForm] = useState({}); const {username} = useAppContext(); const [toastMessage, setToastMessage] = useState(null); const [toastType, setToastType] = useState(1); useEffect(() => { const loadPendingMaps = async () => { const httpService = new HttpService(); httpService.setAuthenticated(); const respMaps = await httpService.get(`${config.admin.display}`); const mapTiles = respMaps.map((elem) => ({ mapName: elem.mapName, cols: 1, rows: 1, status: elem.mapStatus, created_at: elem.createdAt, modified_at: elem.modifiedAt, gmaps_url: elem.gmaps_url, image_url: card, })).filter((tile) => tile.status === "INVALID"); setPendingMaps(mapTiles); }; loadPendingMaps(); }, [username]); useEffect(() => { const loadPublishRequests = async () => { const httpService = new HttpService(true); const respPr = await httpService.get(`${config.admin.load_pr}`) setPublishRequests(respPr); } loadPublishRequests(); }, []); const handleApprove = async (id,mapName) => { const httpService = new HttpService(); httpService.setAuthenticated(); const url = `${config.admin.approve_pr}?id=${id}`; closePublishForm() try { await httpService.post(url); setPendingMaps((prev) => prev.filter((map) => map.mapName !== mapName)); showToast(`Publish Request "${id}" approved.`, 1) } catch (error) { console.error("Error approving pr:", error); // alert("Failed to approve pr."); showToast("Failed to approve pr.", 0) } }; const showToast = (message, type = 1) => { setToastMessage(message); setToastType(type); setTimeout(() => setToastMessage(null), 3000); // Automatically hide the toast after 3 seconds }; const handleDeny = async (id, mapName,reason) => { const httpService = new HttpService(); httpService.setAuthenticated(); const url = `${config.admin.deny_pr}?id=${id}&reason=${encodeURIComponent(reason)}`; closePublishForm() try { await httpService.post(url); setPendingMaps((prev) => prev.filter((map) => map.mapName !== mapName)); showToast(`Publish request ${id} denied.`, 1) } catch (error) { console.error("Error denying pr:", error); alert("Failed to deny pr."); showToast("Failed to deny pr.", 0) } }; const handlePublishFormSubmit = async (formData) => { const httpService = new HttpService(); httpService.setAuthenticated(); const url = `${config.admin.approve_pr}?mapName=${formData.mapName}`; try { await httpService.post(url, formData); // Assuming formData contains all required fields setPendingMaps((prev) => prev.filter((map) => map.mapName !== formData.mapName)); alert(`Map "${formData.mapName}" published successfully.`); closePublishForm(); } catch (error) { console.error("Error publishing map:", error); alert("Failed to publish map. Please try again."); } }; const openMapInfoModal = (map) => { setSelectedMap(map); setIsMapInfoModalOpen(true); }; const closeMapInfoModal = () => { setIsMapInfoModalOpen(false); setSelectedMap(null); }; const openPublishForm = (data) => { console.log("DATA MAP NAME", data.mapName) publishRequests.forEach(pr => { console.log("PR: " + JSON.stringify(pr)) }) const pr = publishRequests.find(p => p.mapName === data.mapName); console.log("FOUND PR: " + pr) setCurrentForm(pr) setPublishFormMap(data); }; const closePublishForm = () => { setPublishFormMap(null); }; return (

Pending Maps for Approval

{publishFormMap && ( )}
renderTile(tileProps, handleApprove, handleDeny, openMapInfoModal, openPublishForm)} tileSize={tileSize} forceTileWidth={200} forceTileHeight={250} />
{isMapInfoModalOpen && ( { }} onDelete={() => { }} published={true} /> )} {toastMessage && setToastMessage(null)}/>}
); }