import { useState, useEffect } from "react"; import { WeeklyReport, NewWeeklyReport } from "../Types/goTypes"; import { api } from "../API/API"; import { useNavigate, useParams } from "react-router-dom"; import Button from "./Button"; /** * Renders the component for editing a weekly report. * @returns JSX.Element */ export default function GetWeeklyReport(): 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 token = localStorage.getItem("accessToken") ?? ""; const { projectName, fetchedWeek } = useParams<{ projectName: string; fetchedWeek: string; }>(); console.log(projectName, fetchedWeek); useEffect(() => { const fetchWeeklyReport = async (): Promise => { const response = await api.getWeeklyReport( projectName ?? "", fetchedWeek ?? "", token, ); if (response.success) { const report: WeeklyReport = response.data ?? { reportId: 0, userId: 0, projectId: 0, week: 0, developmentTime: 0, meetingTime: 0, adminTime: 0, ownWorkTime: 0, studyTime: 0, testingTime: 0, }; setWeek(report.week); setDevelopmentTime(report.developmentTime); setMeetingTime(report.meetingTime); setAdminTime(report.adminTime); setOwnWorkTime(report.ownWorkTime); setStudyTime(report.studyTime); setTestingTime(report.testingTime); } else { console.error("Failed to fetch weekly report:", response.message); } }; void fetchWeeklyReport(); }, [projectName, fetchedWeek, token]); const handleNewWeeklyReport = async (): Promise => { const newWeeklyReport: NewWeeklyReport = { projectName: projectName ?? "", week, developmentTime, meetingTime, adminTime, ownWorkTime, studyTime, testingTime, }; await api.submitWeeklyReport(newWeeklyReport, token); }; const navigate = useNavigate(); return ( <>

Edit Time Report

{ if (week === 0) { alert("Please enter a week number"); e.preventDefault(); return; } e.preventDefault(); void handleNewWeeklyReport(); navigate(-1); }} >

Week: {week}

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") 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") 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") 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") 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") 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") event.preventDefault(); }} />
); }