From a7c282ca348c1e8e60559e5c064caee28ba11eec Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Wed, 9 Oct 2024 16:34:12 +0300 Subject: refactor: so much shit --- frontend/src/components/ModMenu.tsx | 44 ++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to 'frontend/src/components/ModMenu.tsx') diff --git a/frontend/src/components/ModMenu.tsx b/frontend/src/components/ModMenu.tsx index a0945eb..5b0d1c8 100644 --- a/frontend/src/components/ModMenu.tsx +++ b/frontend/src/components/ModMenu.tsx @@ -5,14 +5,16 @@ import { MapSummary } from '../types/Map'; import { ModMenuContent } from '../types/Content'; import { API } from '../api/Api'; import "../css/ModMenu.css" +import { useNavigate } from 'react-router-dom'; interface ModMenuProps { + token?: string; data: MapSummary; selectedRun: number; mapID: string; } -const ModMenu: React.FC = ({ data, selectedRun, mapID }) => { +const ModMenu: React.FC = ({ token, data, selectedRun, mapID }) => { const [menu, setMenu] = React.useState(0); const [showButton, setShowButton] = React.useState(true); @@ -30,6 +32,8 @@ const ModMenu: React.FC = ({ data, selectedRun, mapID }) => { const [image, setImage] = React.useState(""); const [md, setMd] = React.useState(""); + const navigate = useNavigate(); + function compressImage(file: File): Promise { const reader = new FileReader(); reader.readAsDataURL(file); @@ -61,26 +65,56 @@ const ModMenu: React.FC = ({ data, selectedRun, mapID }) => { const _edit_map_summary_image = async () => { if (window.confirm("Are you sure you want to submit this to the database?")) { - await API.put_map_image(mapID, image); + if (token) { + const success = await API.put_map_image(token, mapID, image); + if (success) { + navigate(0); + } else { + alert("Error. Check logs.") + } + } } }; const _edit_map_summary_route = async () => { if (window.confirm("Are you sure you want to submit this to the database?")) { - await API.put_map_summary(mapID, routeContent); + if (token) { + routeContent.date += "T00:00:00Z"; + const success = await API.put_map_summary(token, mapID, routeContent); + if (success) { + navigate(0); + } else { + alert("Error. Check logs.") + } + } } }; const _create_map_summary_route = async () => { if (window.confirm("Are you sure you want to submit this to the database?")) { - await API.post_map_summary(mapID, routeContent); + if (token) { + routeContent.date += "T00:00:00Z"; + const success = await API.post_map_summary(token, mapID, routeContent); + if (success) { + navigate(0); + } else { + alert("Error. Check logs.") + } + } } }; const _delete_map_summary_route = async () => { if (window.confirm(`Are you sure you want to delete this run from the database? ${data.summary.routes[selectedRun].category.name} ${data.summary.routes[selectedRun].history.score_count} portals ${data.summary.routes[selectedRun].history.runner_name}`)) { - await API.delete_map_summary(mapID, data.summary.routes[selectedRun].route_id); + if (token) { + const success = await API.delete_map_summary(token, mapID, data.summary.routes[selectedRun].route_id); + if (success) { + navigate(0); + } else { + alert("Error. Check logs.") + } + } } }; -- cgit v1.2.3