diff --git a/src/pages/teacherDashboard/classes/class/StudentTable.tsx b/src/pages/teacherDashboard/classes/class/StudentTable.tsx index 673fb97..7f50574 100644 --- a/src/pages/teacherDashboard/classes/class/StudentTable.tsx +++ b/src/pages/teacherDashboard/classes/class/StudentTable.tsx @@ -27,13 +27,15 @@ const StudentTable: FC = ({ classId }) => { const [dialog, setDialog] = useState< "reset-students-password" | "delete-students" >() - const [studentUsers, setStudentUsers] = useState< + const [selectedStudentUsers, setSelectedStudentUsers] = useState< Record< ListUsersResult["data"][number]["id"], StudentUser > >({}) + const selectButtonDisabled = !Object.keys(selectedStudentUsers).length + function closeDialog() { setDialog(undefined) } @@ -56,7 +58,9 @@ const StudentTable: FC = ({ classId }) => { }} > {/* @ts-expect-error users are student-users */} - {(users: Array>) => ( + {( + studentUsers: Array>, + ) => ( = ({ classId }) => { children: ( studentUser.id in selectedStudentUsers, + )} onChange={event => { - setStudentUsers( + setSelectedStudentUsers( event.target.checked - ? users.reduce( - (users, user) => ({ ...users, [user.id]: user }), - {} as typeof studentUsers, + ? studentUsers.reduce( + (selectedStudentUsers, studentUser) => ({ + ...selectedStudentUsers, + [studentUser.id]: studentUser, + }), + {} as typeof selectedStudentUsers, ) : {}, ) @@ -81,23 +91,28 @@ const StudentTable: FC = ({ classId }) => { { align: "center", children: "Action" }, ]} > - {users.length ? ( - users.map(user => ( - - {user.first_name} + {studentUsers.length ? ( + studentUsers.map(studentUser => ( + + + {studentUser.first_name} + { - setStudentUsers( + setSelectedStudentUsers( event.target.checked - ? previousStudentUsers => ({ - ...previousStudentUsers, - [user.id]: user, + ? previousSelectedStudentUsers => ({ + ...previousSelectedStudentUsers, + [studentUser.id]: studentUser, }) - : previousStudentUsers => { - const studentUsers = { ...previousStudentUsers } - delete studentUsers[user.id] - return studentUsers + : previousSelectedStudentUsers => { + const selectedStudentUsers = { + ...previousSelectedStudentUsers, + } + delete selectedStudentUsers[studentUser.id] + return selectedStudentUsers }, ) }} @@ -108,7 +123,7 @@ const StudentTable: FC = ({ classId }) => { to={generatePath( paths.teacher.dashboard.tab.classes.class.students .studentUser._, - { classId, studentUserId: user.id }, + { classId, studentUserId: studentUser.id }, )} endIcon={} > @@ -128,21 +143,24 @@ const StudentTable: FC = ({ classId }) => { {LinkButton({ children: "Release", + disabled: selectButtonDisabled, to: generatePath( paths.teacher.dashboard.tab.classes.class.students.release._, { classId }, ), - state: { studentUsers: Object.values(studentUsers) }, + state: { studentUsers: Object.values(selectedStudentUsers) }, })} {LinkButton({ children: "Move", + disabled: selectButtonDisabled, to: generatePath( paths.teacher.dashboard.tab.classes.class.students.transfer._, { classId }, ), - state: { studentUsers: Object.values(studentUsers) }, + state: { studentUsers: Object.values(selectedStudentUsers) }, })}