From 3da4023466bb4d4d7b0e2d920d35222f2d310e70 Mon Sep 17 00:00:00 2001
From: Quaternions <krakow20@gmail.com>
Date: Mon, 24 Mar 2025 23:03:53 +0000
Subject: [PATCH] web: throw error on failure status (#16)

Thanks to ai for knowing javascript

Co-authored-by: rhpidfyre <brandon@rhpidfyre.io>
Reviewed-on: https://git.itzana.me/StrafesNET/maps-service/pulls/16
Co-authored-by: Quaternions <krakow20@gmail.com>
Co-committed-by: Quaternions <krakow20@gmail.com>
---
 .../[submissionId]/_reviewButtons.tsx         | 25 ++++++++++++++-----
 web/src/app/submit/page.tsx                   |  8 ++++++
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/web/src/app/submissions/[submissionId]/_reviewButtons.tsx b/web/src/app/submissions/[submissionId]/_reviewButtons.tsx
index a038d9e..fd51d73 100644
--- a/web/src/app/submissions/[submissionId]/_reviewButtons.tsx
+++ b/web/src/app/submissions/[submissionId]/_reviewButtons.tsx
@@ -15,13 +15,26 @@ interface ReviewId {
 	submissionId: string
 }
 
-function ReviewButtonClicked(action: Action, submissionId: string) {
-	fetch(`/api/submissions/${submissionId}/status/${action}`, {
-		method: "POST",
-		headers: {
-			"Content-type": "application/json",
+async function ReviewButtonClicked(action: Action, submissionId: string) {
+	try {
+		const response = await fetch(`/api/submissions/${submissionId}/status/${action}`, {
+			method: "POST",
+			headers: {
+				"Content-type": "application/json",
+			}
+		});
+		// Check if the HTTP request was successful
+		if (!response.ok) {
+			const errorDetails = await response.text();
+
+			// Throw an error with detailed information
+			throw new Error(`HTTP error! status: ${response.status}, details: ${errorDetails}`);
 		}
-	}).then(() => { window.location.reload(); })
+
+		window.location.reload();
+	} catch (error) {
+		console.error("Error updating submission status:", error);
+	}
 }
 
 function ReviewButton(props: ReviewButton) {
diff --git a/web/src/app/submit/page.tsx b/web/src/app/submit/page.tsx
index c562ad3..6f3c1b7 100644
--- a/web/src/app/submit/page.tsx
+++ b/web/src/app/submit/page.tsx
@@ -51,6 +51,14 @@ export default function SubmissionInfoPage() {
 				body: JSON.stringify(payload),
 			});
 
+			// Check if the HTTP request was successful
+			if (!response.ok) {
+				const errorDetails = await response.text();
+
+				// Throw an error with detailed information
+				throw new Error(`HTTP error! status: ${response.status}, details: ${errorDetails}`);
+			}
+
 			// Allow any HTTP status
 			const id_response:IdResponse = await response.json();