//info: New weekly report form component to create a new weekly report to //sumbit development time, meeting time, admin time, own work time, study time and testing time import { useState } from "react"; import type { NewWeeklyReport } from "../Types/goTypes"; import { api } from "../API/API"; import { useNavigate, useParams } from "react-router-dom"; import Button from "./Button"; /** * Renders a form for creating a new weekly report. * @returns The JSX element representing the new weekly report form. */ export default function NewWeeklyReport(): JSX.Element { const [week, setWeek] = useState(0); const [developmentTime, setDevelopmentTime] = useState(0); const [meetingTime, setMeetingTime] = useState(0); const [adminTime, setAdminTime] = useState(0); const [ownWorkTime, setOwnWorkTime] = useState(0); const [studyTime, setStudyTime] = useState(0); const [testingTime, setTestingTime] = useState(0); const { projectName } = useParams(); const token = localStorage.getItem("accessToken") ?? ""; const handleNewWeeklyReport = async (): Promise => { const newWeeklyReport: NewWeeklyReport = { projectName: projectName ?? "", week: week, developmentTime: developmentTime, meetingTime: meetingTime, adminTime: adminTime, ownWorkTime: ownWorkTime, studyTime: studyTime, testingTime: testingTime, }; const response = await api.submitWeeklyReport(newWeeklyReport, token); console.log(response); if (response.success) { return true; } else { return false; } }; const navigate = useNavigate(); // Check if the browser is Chrome or Edge const isChromeOrEdge = /Chrome|Edg/.test(navigator.userAgent); return ( <>
{ e.preventDefault(); void (async (): Promise => { 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; } alert("Weekly report submitted successfully"); navigate(-1); })(); }} >
{isChromeOrEdge ? ( { const weekNumber = parseInt(e.target.value.split("-W")[1]); setWeek(weekNumber); }} onKeyDown={(event) => { const keyValue = event.key; if (!/\d/.test(keyValue) && keyValue !== "Backspace") event.preventDefault(); }} onPaste={(event) => { event.preventDefault(); }} /> ) : ( { const weekNumber = parseInt(e.target.value); setWeek(weekNumber); }} onKeyDown={(event) => { const keyValue = event.key; if (!/\d/.test(keyValue) && keyValue !== "Backspace") event.preventDefault(); }} onPaste={(event) => { event.preventDefault(); }} /> )}
Activity Total Time (min)
Development { if (e.target.value === "") { setDevelopmentTime(0); return; } else { setDevelopmentTime(parseInt(e.target.value)); } }} onKeyDown={(event) => { const keyValue = event.key; if ( !/\d/.test(keyValue) && keyValue !== "Backspace" && keyValue !== "ArrowLeft" && keyValue !== "ArrowRight" ) event.preventDefault(); }} />
Meeting { if (e.target.value === "") { setMeetingTime(0); return; } else { setMeetingTime(parseInt(e.target.value)); } }} onKeyDown={(event) => { const keyValue = event.key; if ( !/\d/.test(keyValue) && keyValue !== "Backspace" && keyValue !== "ArrowLeft" && keyValue !== "ArrowRight" ) event.preventDefault(); }} />
Administration { if (e.target.value === "") { setAdminTime(0); return; } else { setAdminTime(parseInt(e.target.value)); } }} onKeyDown={(event) => { const keyValue = event.key; if ( !/\d/.test(keyValue) && keyValue !== "Backspace" && keyValue !== "ArrowLeft" && keyValue !== "ArrowRight" ) event.preventDefault(); }} />
Own Work { if (e.target.value === "") { setOwnWorkTime(0); return; } else { setOwnWorkTime(parseInt(e.target.value)); } }} onKeyDown={(event) => { const keyValue = event.key; if ( !/\d/.test(keyValue) && keyValue !== "Backspace" && keyValue !== "ArrowLeft" && keyValue !== "ArrowRight" ) event.preventDefault(); }} />
Studies { if (e.target.value === "") { setStudyTime(0); return; } else { setStudyTime(parseInt(e.target.value)); } }} onKeyDown={(event) => { const keyValue = event.key; if ( !/\d/.test(keyValue) && keyValue !== "Backspace" && keyValue !== "ArrowLeft" && keyValue !== "ArrowRight" ) event.preventDefault(); }} />
Testing { if (e.target.value === "") { setTestingTime(0); return; } else { setTestingTime(parseInt(e.target.value)); } }} onKeyDown={(event) => { const keyValue = event.key; if ( !/\d/.test(keyValue) && keyValue !== "Backspace" && keyValue !== "ArrowLeft" && keyValue !== "ArrowRight" ) event.preventDefault(); }} />
); }