From ebc59e0c111dd2ef783be6537f3a1c04ef4b108c Mon Sep 17 00:00:00 2001
From: Davenludd <david.ludde01@gmail.com>
Date: Thu, 21 Mar 2024 12:31:05 +0100
Subject: [PATCH] Refactor handleNewWeeklyReport function to return a boolean
 indicating success or failure if week already has a report

---
 frontend/src/Components/NewWeeklyReport.tsx | 32 +++++++++++++++------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/frontend/src/Components/NewWeeklyReport.tsx b/frontend/src/Components/NewWeeklyReport.tsx
index 1204e11..b0bccac 100644
--- a/frontend/src/Components/NewWeeklyReport.tsx
+++ b/frontend/src/Components/NewWeeklyReport.tsx
@@ -22,7 +22,7 @@ export default function NewWeeklyReport(): JSX.Element {
   const { projectName } = useParams();
   const token = localStorage.getItem("accessToken") ?? "";
 
-  const handleNewWeeklyReport = async (): Promise<void> => {
+  const handleNewWeeklyReport = async (): Promise<boolean> => {
     const newWeeklyReport: NewWeeklyReport = {
       projectName: projectName ?? "",
       week: week,
@@ -34,7 +34,13 @@ export default function NewWeeklyReport(): JSX.Element {
       testingTime: testingTime,
     };
 
-    await api.submitWeeklyReport(newWeeklyReport, token);
+    const response = await api.submitWeeklyReport(newWeeklyReport, token);
+    console.log(response);
+    if (response.success) {
+      return true;
+    } else {
+      return false;
+    }
   };
 
   const navigate = useNavigate();
@@ -46,14 +52,22 @@ export default function NewWeeklyReport(): JSX.Element {
       <div className="border-4 border-black bg-white flex flex-col justify-start min-h-[65vh] h-fit w-[50vw] rounded-3xl overflow-scroll space-y-[2vh] p-[30px] items-center">
         <form
           onSubmit={(e) => {
-            if (week === 0 || week > 53 || week < 1) {
-              alert("Please enter a valid week number");
-              e.preventDefault();
-              return;
-            }
             e.preventDefault();
-            void handleNewWeeklyReport();
-            navigate(-1);
+            void (async (): Promise<void> => {
+              if (week === 0 || week > 53 || week < 1) {
+                alert("Please enter a valid week number");
+                return;
+              }
+
+              const success = await handleNewWeeklyReport();
+              if (!success) {
+                alert(
+                  "A Time Report for this week already exists, please go to the edit page to edit it or change week number.",
+                );
+                return;
+              }
+              navigate(-1);
+            })();
           }}
         >
           <div className="flex flex-col items-center">