forked from expertiza/reimplementation-front-end
/
Assignment.tsx
134 lines (117 loc) · 4.05 KB
/
Assignment.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Button, Col, Container, Row } from "react-bootstrap";
import { Outlet, useLocation, useNavigate } from "react-router-dom";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { assignmentColumns as ASSIGNMENT_COLUMNS } from "./AssignmentColumns";
import { BsFileText } from "react-icons/bs";
import DeleteAssignment from "./AssignmentDelete";
import { IAssignmentResponse } from "../../utils/interfaces";
import { RootState } from "../../store/store";
import { Row as TRow } from "@tanstack/react-table";
import Table from "components/Table/Table";
import { alertActions } from "store/slices/alertSlice";
import useAPI from "hooks/useAPI";
const Assignments = () => {
const { error, isLoading, data: assignmentResponse, sendRequest: fetchAssignments } = useAPI();
const { data: coursesResponse, sendRequest: fetchCourses } = useAPI();
const auth = useSelector(
(state: RootState) => state.authentication,
(prev, next) => prev.isAuthenticated === next.isAuthenticated
);
const navigate = useNavigate();
const location = useLocation();
const dispatch = useDispatch();
const [showDeleteConfirmation, setShowDeleteConfirmation] = useState<{
visible: boolean;
data?: IAssignmentResponse;
}>({ visible: false });
const fetchData = useCallback(async () => {
try {
const [assignments, courses] = await Promise.all([
fetchAssignments({ url: `/assignments` }),
fetchCourses({ url: "/courses" }),
]);
// Handle the responses as needed
} catch (err) {
// Handle any errors that occur during the fetch
console.error("Error fetching data:", err);
}
}, [fetchAssignments, fetchCourses]);
useEffect(() => {
if (!showDeleteConfirmation.visible) {
fetchData();
}
}, [fetchData, showDeleteConfirmation.visible, auth.user.id]);
let mergedData: Array<any & { courseName?: string }> = [];
if (assignmentResponse && coursesResponse) {
mergedData = assignmentResponse.data.map((assignment: any) => {
const course = coursesResponse.data.find((c: any) => c.id === assignment.course_id);
return { ...assignment, courseName: course ? course.name : "Unknown" };
});
}
// Error alert
useEffect(() => {
if (error) {
dispatch(alertActions.showAlert({ variant: "danger", message: error }));
}
}, [error, dispatch]);
const onDeleteAssignmentHandler = useCallback(
() => setShowDeleteConfirmation({ visible: false }),
[]
);
const onEditHandle = useCallback(
(row: TRow<IAssignmentResponse>) => navigate(`edit/${row.original.id}`),
[navigate]
);
const onDeleteHandle = useCallback(
(row: TRow<IAssignmentResponse>) =>
setShowDeleteConfirmation({ visible: true, data: row.original }),
[]
);
const tableColumns = useMemo(
() => ASSIGNMENT_COLUMNS(onEditHandle, onDeleteHandle),
[onDeleteHandle, onEditHandle]
);
const tableData = useMemo(
() => (isLoading || !mergedData?.length ? [] : mergedData),
[mergedData, isLoading]
);
return (
<>
<Outlet />
<main>
<Container fluid className="px-md-4">
<Row className="mt-md-2 mb-md-2">
<Col className="text-center">
<h1>Manage Assignments</h1>
</Col>
<hr />
</Row>
<Row>
<Col md={{ span: 1, offset: 11 }}>
<Button variant="outline-info" onClick={() => navigate("new")}>
<BsFileText />
</Button>
</Col>
{showDeleteConfirmation.visible && (
<DeleteAssignment
assignmentData={showDeleteConfirmation.data!}
onClose={onDeleteAssignmentHandler}
/>
)}
</Row>
<Row>
<Table
data={tableData}
columns={tableColumns}
columnVisibility={{
id: false,
}}
/>
</Row>
</Container>
</main>
</>
);
};
export default Assignments;