From 7fe146f97ddd3f5a8d0c1a996a73cb296c28b9cc Mon Sep 17 00:00:00 2001
From: Pinapelz <yukais@pinapelz.com>
Date: Mon, 7 Jul 2025 01:07:34 -0700
Subject: implement score deletion

---
 frontend/src/pages/Score.tsx | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

(limited to 'frontend/src/pages/Score.tsx')

diff --git a/frontend/src/pages/Score.tsx b/frontend/src/pages/Score.tsx
index e32f001..0d18d7a 100644
--- a/frontend/src/pages/Score.tsx
+++ b/frontend/src/pages/Score.tsx
@@ -5,7 +5,6 @@ import { NavBar } from "../components/NavBar";
 import SessionExpiredPopup from "../components/SessionExpiredPopup";
 import ScoreDisplay from "../components/displays/GenericScoreDisplay";
 import DancerushScoreDisplay from "../components/displays/DancerushScoreDisplay";
-// TODO: selector for PB/Recent
 type SortField = string;
 type SortDirection = "asc" | "desc";
 
@@ -79,7 +78,7 @@ const Score = () => {
         url.searchParams.append("sortKey", requestOrder);
         url.searchParams.append("direction", "asc");
 
-        const response = await fetch(url.toString());
+        const response = await fetch(url.toString(), {credentials: 'include'});
         if (!response.ok) throw new Error("Failed to fetch scores");
         const data = await response.json();
         const flattened = data.scores.map(flattenScoreData);
@@ -96,6 +95,33 @@ const Score = () => {
     [user, gameName, requestOrder],
   );
 
+  const handleDeleteScore = async (scoreId: number) => {
+    if (!user) return;
+
+    if (!confirm("Are you sure you want to delete this score? This action cannot be undone.")) {
+      return;
+    }
+
+    try {
+      const url = new URL(import.meta.env.VITE_API_URL + "/scores");
+      url.searchParams.append("userId", user.id);
+      url.searchParams.append("internalGameName", gameName);
+      url.searchParams.append("scoreId", scoreId.toString());
+
+      const response = await fetch(url.toString(), {
+        method: "DELETE",
+        credentials: "include",
+      });
+
+      if (!response.ok) throw new Error("Failed to delete score");
+
+      await fetchScores(currentPage);
+    } catch (error) {
+      console.error("Failed to delete score:", error);
+      alert("Failed to delete score. Please try again.");
+    }
+  };
+
   useEffect(() => {
     if (user) fetchScores(1);
   }, [user, fetchScores]);
@@ -175,6 +201,7 @@ const Score = () => {
                   sortField={sortField}
                   sortDirection={sortDirection}
                   onSort={handleSort}
+                  onDelete={handleDeleteScore}
                 />
               );
             default:
@@ -185,6 +212,7 @@ const Score = () => {
                   sortField={sortField}
                   sortDirection={sortDirection}
                   onSort={handleSort}
+                  onDelete={handleDeleteScore}
                 />
               );
           }
-- 
cgit v1.2.3

