Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | 1x 111x 162x 162x 111x 111x 111x 111x 111x 111x 111x 111x 4x 4x 4x 4x 1x 1x 1x 3x 3x 111x 10x 10x 10x 53x 4x 10x 111x 20x 111x 15x 111x 14x 4x 10x 8x 2x 2x 14x 111x 109x 445x 440x 430x 425x 420x 415x 422x 111x 8x 8x 8x 5x 8x 3x 3x 3x 2x 2x 3x 3x 8x 111x 6x 111x 5x 5x 5x 2x 2x 3x 3x 111x 2x 2x 2x 1x 1x 1x 1x 111x 3x 111x 4x 4x 4x 1x 1x 3x 3x 111x 7x 111x 8x 8x 8x 1x 1x 7x 7x 111x 10x 111x 1x 1x 1x 1x 1x 111x 422x 342x 80x 103x 79x 79x 1x 111x 422x 111x 110x 7x 111x 422x 80x 3x 342x 342x 342x 80x 3x 7x 103x 10x 79x 1x 79x 1x 1x 422x 111x 87x 87x 87x 87x 7x 7x 80x 3x 3x 87x 111x 52x 111x 2x 1x | /**
* Modified by Allan Ninal — 2026-08-11
*
* Student profile lookup delegated to studentProfileLookup.js, and the lookup email
* is now URL-encoded so plus-addressed students can be found (PR #206).
*/
import {
Box,
Button,
Chip,
Dialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
FormControl,
MenuItem,
Select,
} from "@mui/material";
import WarningIcon from "@mui/icons-material/Warning";
import { useTheme } from "@mui/material/styles";
import PinnedTable from "@/utils/components/tables/pinned-table/PinnedTable";
import {
buildStudentLookupUrl,
findExactStudentByEmail,
toStudentProfile,
} from "@/app/teacher-page/components/class-roster/studentProfileLookup";
import {
getHeroSoftToolbarSx,
getHeroSoftSearchFieldSx,
getHeroSoftSelectSx,
} from "@/utils/components/tables/heroTableStyles";
import { MRT_GlobalFilterTextField } from "material-react-table";
import { useDispatch, useSelector } from "react-redux";
import {
_fetchClassRoster,
_acceptStudent,
_removeStudent,
_approveLeave,
} from "@/store/slices/teacher/TeacherClassRoster";
import { messageFromRejection } from "@/utils/helpers/rejectionMessage";
import React, { useEffect, useState, useMemo, useRef } from "react";
import { enqueueSnackbar, closeSnackbar } from "notistack";
import SearchProfile from "@/app/teacher-page/components/class-roster/SearchProfile";
import DefaultImage from "@/assets/default_image.png";
import axios from "axios";
import SimpleLoading from "@/utils/components/SimpleLoading";
const ClassRoster = () => {
const theme = useTheme();
const { specificClassData } = useSelector((state) => state.TeacherSpecificClass);
const { classRosterData, isLoading } = useSelector((state) => state.TeacherClassRoster);
const dispatch = useDispatch();
const [classStatus, setClassStatus] = useState("all");
const [viewProfile, setViewProfile] = useState(null);
const [isDialog, setDialog] = useState(false);
const [pageLoading, setPageLoading] = useState(false);
const [confirmDialog, setConfirmDialog] = useState({
open: false,
action: null,
row: null,
title: "",
message: "",
});
// EI-1163 (Allan Ninal, 2026-10-04): Undo after remove/reject — reuses accept; reject undo returns the student as Enrolled.
// Server (teacher student_accepted) accepts Pending, Rejected AND Removed -> Enrolled, so:
// - undoing a REMOVE is an exact reversal (Enrolled -> Removed -> Enrolled);
// - undoing a REJECT yields Enrolled, NOT Pending (no restore-to-Pending endpoint exists).
// Only one undo is pending at a time: a new remove/reject replaces the previous snackbar
// (the earlier action simply stays applied).
// Delivered through notistack (the automation contract detects success toasts by notistack classes).
const undoKeyRef = useRef(null);
const handleUndo = (key, { classID, studentID, name }) => {
closeSnackbar(key);
Eif (undoKeyRef.current === key) undoKeyRef.current = null;
dispatch(_acceptStudent({ classID, studentID })).then((response) => {
if (!_acceptStudent.fulfilled.match(response)) {
reportRosterFailure(response);
// Keep the roster consistent with the server after a failed restore.
dispatch(_fetchClassRoster(classID));
return;
}
enqueueSnackbar(`${name} restored`, { variant: "success" });
dispatch(_fetchClassRoster(classID));
});
};
const showUndo = ({ classID, studentID, name, kind }) => {
if (undoKeyRef.current !== null) closeSnackbar(undoKeyRef.current);
const target = { classID, studentID, name };
const key = enqueueSnackbar(kind === "rejected" ? `${name} rejected.` : `${name} removed from the class.`, {
variant: "success",
autoHideDuration: 10000,
action: (snackKey) => (
<Button
id="teacher-roster-undo-button"
color="inherit"
size="small"
onClick={() => handleUndo(snackKey, target)}
>
Undo
</Button>
),
});
undoKeyRef.current = key;
};
const openConfirmDialog = (action, row, title, message) => {
setConfirmDialog({
open: true,
action,
row,
title,
message,
});
};
const handleConfirmDialogClose = () => {
setConfirmDialog({
open: false,
action: null,
row: null,
title: "",
message: "",
});
};
const handleConfirmAction = () => {
if (confirmDialog.action === "reject") {
executeRejectStudent(confirmDialog.row);
} else if (confirmDialog.action === "remove") {
executeRemoveStudent(confirmDialog.row);
E} else if (confirmDialog.action === "approveLeave") {
executeApproveLeave(confirmDialog.row);
}
handleConfirmDialogClose();
};
const getAllEntries = (students) => {
return students
.filter((student) => {
if (classStatus === "enrolled") return student.status === "Enrolled";
if (classStatus === "pending") return student.status === "Pending";
if (classStatus === "removed") return student.status === "Removed";
if (classStatus === "rejected") return student.status === "Rejected";
if (classStatus === "requesting to leave") return student.is_requesting_to_leave === true;
return true; // for default case
})
.map((student) => ({
_id: student._id,
status: student.status,
lastName: student.last_name,
firstName: student.first_name,
middleName: student.middle_name,
fullName: [student.first_name, student.middle_name, student.last_name].filter(Boolean).join(" "),
email: student.email,
is_requesting_to_leave: student.is_requesting_to_leave,
}));
};
const viewStudentProfile = async (row) => {
setPageLoading(true);
try {
const { data } = await axios.get(buildStudentLookupUrl(row.original.email));
const matchedStudent = findExactStudentByEmail(data?.data, row.original.email);
if (!matchedStudent) {
enqueueSnackbar("No exact student match found for the given email.", {
variant: "error",
});
setViewProfile({});
return;
}
setViewProfile(toStudentProfile(matchedStudent));
setDialog(true);
} catch (error) {
enqueueSnackbar(error.response?.data || error.message, {
variant: "error",
});
setViewProfile({});
} finally {
setPageLoading(false);
}
};
/**
* Modified by Allan Ninal — these four reported SUCCESS when the request failed.
*
* With rejectWithValue, a rejected action's `payload` IS the error body, so the
* `if (response.payload)` guard these used is TRUE on failure. A teacher whose
* approval was refused was told "Student successfully accepted" — while the
* student stayed at "Waiting For Approval" and locked out of the class.
* approve-leave read `response.payload.status`, which is undefined on that same
* body, so it said nothing at all instead.
*
* `fulfilled.match` is the only reliable test of which happened.
*/
const reportRosterFailure = (action) => {
enqueueSnackbar(messageFromRejection(action), { variant: "error" });
};
const acceptStudent = (row) => {
const payload = {
classID: specificClassData._id,
studentID: row.original._id,
};
dispatch(_acceptStudent(payload)).then((response) => {
if (!_acceptStudent.fulfilled.match(response)) {
reportRosterFailure(response);
return;
}
enqueueSnackbar("Student successfully accepted", {
variant: "success",
});
dispatch(_fetchClassRoster(specificClassData._id));
});
};
const executeApproveLeave = (row) => {
const payload = {
classID: specificClassData._id,
studentID: row.original._id,
};
dispatch(_approveLeave(payload)).then((response) => {
if (!_approveLeave.fulfilled.match(response)) {
reportRosterFailure(response);
return;
}
enqueueSnackbar("Student has been removed from the class", {
variant: "success",
});
dispatch(_fetchClassRoster(specificClassData._id));
});
};
const approveLeave = (row) => {
openConfirmDialog(
"approveLeave",
row,
"Approve Leave Request",
`Are you sure you want to approve ${row.original.fullName}'s request to leave this class? This action will remove them from the class.`
);
};
const executeRejectStudent = (row) => {
const payload = {
classID: specificClassData._id,
studentID: row.original._id,
};
dispatch(_removeStudent(payload)).then((response) => {
if (!_removeStudent.fulfilled.match(response)) {
reportRosterFailure(response);
return;
}
showUndo({
classID: payload.classID,
studentID: payload.studentID,
name: row.original.fullName,
kind: "rejected",
});
dispatch(_fetchClassRoster(specificClassData._id));
});
};
const rejectStudent = (row) => {
openConfirmDialog(
"reject",
row,
"Reject Student",
`Are you sure you want to reject ${row.original.fullName}? This action will update their status to "Rejected".`
);
};
const executeRemoveStudent = (row) => {
const payload = {
classID: specificClassData._id,
studentID: row.original._id,
};
dispatch(_removeStudent(payload)).then((response) => {
if (!_removeStudent.fulfilled.match(response)) {
reportRosterFailure(response);
return;
}
showUndo({
classID: payload.classID,
studentID: payload.studentID,
name: row.original.fullName,
kind: "removed",
});
dispatch(_fetchClassRoster(specificClassData._id));
});
};
const removeStudent = (row) => {
openConfirmDialog(
"remove",
row,
"Remove Student",
`Are you sure you want to remove ${row.original.fullName} from this class? This action will update their status to "Removed".`
);
};
const sendEmail = (row) => {
const URL = "https://mail.google.com/mail/?view=cm&fs=1"; //gmail
const TO = row.original.email;
const SUBJECT = "";
const BODY = "";
window.open(URL + "&to=" + TO + "&su=" + SUBJECT + "&body=" + BODY, "_blank").focus();
};
const chipCellValue = (value, is_requesting_to_leave) => {
if (is_requesting_to_leave) return <Chip label={"Requesting to leave"} size="small" color="warning" />;
switch (value) {
case "Pending":
return <Chip label={value} size="small" color="info" />;
case "Enrolled":
return <Chip label={value} size="small" color="success" />;
case "Removed":
return <Chip label={value} size="small" color="error" />;
case "Rejected":
return <Chip label={value} size="small" color="error" />;
default:
break;
}
};
const columns = [
{ accessorKey: "lastName", header: "Last name" },
{ accessorKey: "firstName", header: "First name" },
{ accessorKey: "middleName", header: "Middle Name" },
{ accessorKey: "email", header: "Email" },
{
accessorKey: "status",
header: "Status",
Cell: ({ cell }) => chipCellValue(cell.getValue(), cell.row.original.is_requesting_to_leave),
},
{
accessorKey: "fullName",
header: "Full Name",
enableHiding: false,
visibleInShowHideMenu: false,
size: 0,
},
];
const topToolbar = ({ table }) => {
return (
<Box sx={{ ...getHeroSoftToolbarSx(theme), alignItems: "center" }}>
<MRT_GlobalFilterTextField
table={table}
sx={getHeroSoftSearchFieldSx(theme)}
placeholder="Search roster"
/>
<FormControl size="small" sx={{ width: 200, ...getHeroSoftSelectSx(theme) }}>
<Select
value={classStatus}
onChange={(e) => {
setClassStatus(e.target.value);
}}
>
<MenuItem value="all">All</MenuItem>
<MenuItem value="enrolled">Enrolled</MenuItem>
<MenuItem value="pending">Pending</MenuItem>
<MenuItem value="removed">Removed</MenuItem>
<MenuItem value="rejected">Rejected</MenuItem>
<MenuItem value="requesting to leave">Requesting to leave</MenuItem>
</Select>
</FormControl>
</Box>
);
};
const actionButtons = (row) => {
if (row.original.is_requesting_to_leave) {
return [
<MenuItem key="approveLeave" onClick={() => approveLeave(row)}>
Approve
</MenuItem>,
];
}
const viewProfileItem = (
<MenuItem key="viewStudentProfile" onClick={() => viewStudentProfile(row)}>
View Profile
</MenuItem>
);
const emailItem = (
<MenuItem key="emailClassRoster" onClick={() => sendEmail(row)}>
Email
</MenuItem>
);
switch (row.original.status) {
case "Pending":
return [
viewProfileItem,
<MenuItem key="acceptClassRoster" onClick={() => acceptStudent(row)}>
Accept
</MenuItem>,
<MenuItem key="rejectClassRoster" onClick={() => rejectStudent(row)}>
Reject
</MenuItem>,
];
case "Enrolled":
return [
viewProfileItem,
<MenuItem key="removeClassRoster" onClick={() => removeStudent(row)}>
Remove
</MenuItem>,
emailItem,
];
case "Removed":
return [
viewProfileItem,
<MenuItem key="rejoinClassRoster" onClick={() => acceptStudent(row)}>
Rejoin
</MenuItem>,
emailItem,
];
case "Rejected":
return [
viewProfileItem,
<MenuItem key="acceptClassRoster" onClick={() => acceptStudent(row)}>
Accept
</MenuItem>,
emailItem,
];
default:
return [];
}
};
const rowActions = ({ row }) => actionButtons(row);
const confirmButtonProps = useMemo(() => {
const action = confirmDialog.action;
let color = "warning";
let text = "Remove";
if (action === "reject") {
color = "error";
text = "Reject";
} else if (action === "approveLeave") {
color = "primary";
text = "Approve";
}
return { color, text };
}, [confirmDialog.action]);
useEffect(() => {
dispatch(_fetchClassRoster(specificClassData._id));
}, []);
return (
<React.Fragment>
<SimpleLoading showLoading={pageLoading} />
<SearchProfile data={viewProfile} isDialog={isDialog} setDialog={setDialog} />
{/* Confirmation Dialog */}
<Dialog
open={confirmDialog.open}
onClose={handleConfirmDialogClose}
aria-labelledby="confirm-dialog-title"
aria-describedby="confirm-dialog-description"
maxWidth="sm"
fullWidth
onKeyDown={(e) => {
if (e.key === "Enter") {
handleConfirmAction();
}
}}
>
<DialogTitle id="confirm-dialog-title" sx={{ display: "flex", alignItems: "center", gap: 1.5 }}>
<WarningIcon color={confirmButtonProps.color} sx={{ fontSize: 28 }} />
{confirmDialog.title}
</DialogTitle>
<DialogContent dividers>
<DialogContentText id="confirm-dialog-description" sx={{ fontSize: "1rem" }}>
{confirmDialog.message}
</DialogContentText>
</DialogContent>
<DialogActions sx={{ px: 3, py: 2 }}>
<Button onClick={handleConfirmDialogClose} variant="outlined">
Cancel
</Button>
<Button
onClick={handleConfirmAction}
variant="contained"
color={confirmButtonProps.color}
autoFocus
>
{confirmButtonProps.text}
</Button>
</DialogActions>
</Dialog>
<Box sx={{ width: "100%", minHeight: 420, display: "flex", flexDirection: "column" }}>
<Box sx={{ flex: 1, minHeight: 360, display: "flex", flexDirection: "column" }}>
<PinnedTable
tableId="teacher-class-roster"
variant="soft"
columns={columns}
data={classRosterData ? getAllEntries(classRosterData) : []}
topToolbar={topToolbar}
pinnedColumn={false}
rowActions={rowActions}
isLoading={isLoading}
enableStickyHeader
initialState={{
columnVisibility: { fullName: false },
}}
muiTableContainerProps={{ sx: { minHeight: 260 } }}
/>
</Box>
</Box>
</React.Fragment>
);
};
export default ClassRoster;
|