Refactor handleNewWeeklyReport function to return a boolean indicating success or failure if week already has a report
This commit is contained in:
parent
d8a73329a1
commit
ebc59e0c11
1 changed files with 23 additions and 9 deletions
|
@ -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">
|
||||
|
|
Loading…
Reference in a new issue