Update OtherUsersTR component to include unsign and delete functionality

This commit is contained in:
Davenludd 2024-04-09 22:14:00 +02:00
parent 56e566ea9b
commit ff07bd1ed6

View file

@ -1,7 +1,7 @@
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { WeeklyReport } from "../Types/goTypes"; import { WeeklyReport } from "../Types/goTypes";
import { api } from "../API/API"; import { api } from "../API/API";
import { useParams } from "react-router-dom"; import { useParams, useNavigate } from "react-router-dom";
import Button from "./Button"; import Button from "./Button";
/** /**
@ -18,6 +18,7 @@ export default function OtherUsersTR(): JSX.Element {
const [ownWorkTime, setOwnWorkTime] = useState(0); const [ownWorkTime, setOwnWorkTime] = useState(0);
const [studyTime, setStudyTime] = useState(0); const [studyTime, setStudyTime] = useState(0);
const [testingTime, setTestingTime] = useState(0); const [testingTime, setTestingTime] = useState(0);
const [reportId, setReportId] = useState(0);
const token = localStorage.getItem("accessToken") ?? ""; const token = localStorage.getItem("accessToken") ?? "";
const { projectName } = useParams(); const { projectName } = useParams();
@ -48,6 +49,7 @@ export default function OtherUsersTR(): JSX.Element {
studyTime: 0, studyTime: 0,
testingTime: 0, testingTime: 0,
}; };
setReportId(report.reportId);
setWeek(report.week); setWeek(report.week);
setDevelopmentTime(report.developmentTime); setDevelopmentTime(report.developmentTime);
setMeetingTime(report.meetingTime); setMeetingTime(report.meetingTime);
@ -63,6 +65,27 @@ export default function OtherUsersTR(): JSX.Element {
void fetchUsersWeeklyReport(); void fetchUsersWeeklyReport();
}); });
const handleUnsignWeeklyReport = async (): Promise<boolean> => {
const response = await api.unsignReport(reportId, token);
console.log(response);
console.log(reportId);
if (response.success) {
return true;
} else {
return false;
}
};
const handleDeleteWeeklyReport = async (): Promise<boolean> => {
const response = await api.deleteWeeklyReport(reportId, token);
console.log(response);
if (response.success) {
return true;
}
return false;
};
const navigate = useNavigate();
return ( return (
<> <>
<h1 className="text-[30px] font-bold">{username}&apos;s Report</h1> <h1 className="text-[30px] font-bold">{username}&apos;s Report</h1>
@ -156,15 +179,48 @@ export default function OtherUsersTR(): JSX.Element {
</tr> </tr>
</tbody> </tbody>
</table> </table>
<div className="flex space-x-4">
{signedOrUnsigned === "signed" && ( {signedOrUnsigned === "signed" && (
<Button <Button
text="Unsign Report" text="Unsign Report"
onClick={(): void => { onClick={(): void => {
void (async (): Promise<void> => {
const success = await handleUnsignWeeklyReport();
if (success) {
alert("Report successfully unsigned!");
navigate(-1);
} else {
alert("Failed to unsign report");
return; return;
}
})();
}} }}
type="submit" type={"button"}
/> />
)} )}
<Button
text="Delete Time Report"
onClick={(): void => {
void (async (): Promise<void> => {
const confirmDelete = window.confirm(
"Are you sure you want to delete this report? This action cannot be undone.",
);
if (!confirmDelete) {
return;
}
const success = await handleDeleteWeeklyReport();
if (success) {
alert("Report successfully deleted!");
navigate(-1);
} else {
alert("Failed to delete report");
return;
}
})();
}}
type={"button"}
/>
</div>
</div> </div>
</div> </div>
</> </>