Refactor handleNewWeeklyReport function to return a boolean indicating success or failure if week already has a report

This commit is contained in:
Davenludd 2024-03-21 12:31:05 +01:00
parent d8a73329a1
commit ebc59e0c11

View file

@ -22,7 +22,7 @@ export default function NewWeeklyReport(): JSX.Element {
const { projectName } = useParams(); const { projectName } = useParams();
const token = localStorage.getItem("accessToken") ?? ""; const token = localStorage.getItem("accessToken") ?? "";
const handleNewWeeklyReport = async (): Promise<void> => { const handleNewWeeklyReport = async (): Promise<boolean> => {
const newWeeklyReport: NewWeeklyReport = { const newWeeklyReport: NewWeeklyReport = {
projectName: projectName ?? "", projectName: projectName ?? "",
week: week, week: week,
@ -34,7 +34,13 @@ export default function NewWeeklyReport(): JSX.Element {
testingTime: testingTime, 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(); 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"> <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 <form
onSubmit={(e) => { onSubmit={(e) => {
e.preventDefault();
void (async (): Promise<void> => {
if (week === 0 || week > 53 || week < 1) { if (week === 0 || week > 53 || week < 1) {
alert("Please enter a valid week number"); alert("Please enter a valid week number");
e.preventDefault();
return; return;
} }
e.preventDefault();
void handleNewWeeklyReport(); 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); navigate(-1);
})();
}} }}
> >
<div className="flex flex-col items-center"> <div className="flex flex-col items-center">