33import { Avatar } from "@/components/ui/avatar" ;
44import { Button } from "@/components/ui/button" ;
55import { ConfirmDialog } from "@/components/ui/confirm-dialog" ;
6+ import type { StudentAttendance , StudentTuple } from '@/contexts/sessionContext' ;
7+ import { useSessionContext } from '@/contexts/sessionContext' ;
68import { formatRecurrence , useSession , useSessions } from "@/hooks/useSessions" ;
79import {
810 useSessionStudents ,
@@ -31,7 +33,7 @@ import {
3133} from "lucide-react" ;
3234import Link from "next/link" ;
3335import { useSearchParams } from "next/navigation" ;
34- import { use , useState } from "react" ;
36+ import { use , useEffect , useState } from "react" ;
3537
3638interface PageProps {
3739 params : Promise < {
@@ -79,6 +81,43 @@ export default function SessionPage({ params }: PageProps) {
7981 const [ deleteType , setDeleteType ] = useState < "single" | "recurring" | null > (
8082 null
8183 ) ;
84+ const { attendance, setAttendance, setStudents, students : contextStudents , attendance : contextAttendance } = useSessionContext ( ) ;
85+
86+ useEffect ( ( ) => {
87+ if ( ! studentsLoading && sessionStudents . length > 0 ) {
88+ // 1. Calculate the NEW student tuples based on current session students
89+ const studentTuples : StudentTuple [ ] = sessionStudents
90+ . filter ( s => s . session_student_id )
91+ . map ( s => ( {
92+ studentId : s . id ,
93+ sessionStudentId : s . session_student_id ! ,
94+ } ) ) ;
95+
96+ // 2. Calculate the NEW attendance map
97+ const initialAttendance : StudentAttendance = sessionStudents . reduce ( ( acc , student ) => {
98+ if ( student . session_student_id ) {
99+ acc [ student . session_student_id ] = student . present ?? true ;
100+ }
101+ return acc ;
102+ } , { } as StudentAttendance ) ;
103+
104+ // --- CRITICAL CHECK: Prevent infinite loop by comparing objects/arrays ---
105+
106+ // Check if the student list length has changed (simplest check)
107+ const studentsChanged = studentTuples . length !== contextStudents . length ;
108+
109+ // Check if the attendance map contents have changed (more robust)
110+ const attendanceJson = JSON . stringify ( initialAttendance ) ;
111+ const contextAttendanceJson = JSON . stringify ( contextAttendance ) ;
112+ const attendanceChanged = attendanceJson !== contextAttendanceJson ;
113+
114+ if ( studentsChanged || attendanceChanged ) {
115+ // Only update if there's a difference
116+ setStudents ( studentTuples ) ;
117+ setAttendance ( initialAttendance ) ;
118+ }
119+ }
120+ } , [ sessionStudents , studentsLoading , setAttendance , setStudents , contextStudents , contextAttendance ] ) ; // Add context state dependencies
82121
83122 if ( sessionLoading || studentsLoading ) {
84123 return (
@@ -139,12 +178,25 @@ export default function SessionPage({ params }: PageProps) {
139178 } ) ;
140179 } ;
141180
142- const handleToggleAttendance = ( studentId : string , present : boolean ) => {
181+ const handleToggleAttendance = (
182+ studentId : string ,
183+ sessionStudentId : number | undefined ,
184+ present : boolean
185+ ) => {
186+ // 1. Update the backend/API
143187 updateSessionStudent ( {
144188 session_id : id ,
145189 student_id : studentId ,
146190 present,
147191 } ) ;
192+
193+ // 2. Update the Session Context for immediate UI filtering in other views
194+ if ( sessionStudentId ) {
195+ setAttendance ( {
196+ ...( attendance ?? { } ) ,
197+ [ sessionStudentId ] : present ,
198+ } ) ;
199+ }
148200 } ;
149201
150202 const handleEditClick = ( ) => {
@@ -526,7 +578,7 @@ export default function SessionPage({ params }: PageProps) {
526578 < Button
527579 onClick = { ( e ) => {
528580 e . preventDefault ( ) ;
529- handleToggleAttendance ( student . id , true ) ;
581+ handleToggleAttendance ( student . id , student . session_student_id , true ) ;
530582 } }
531583 variant = { student . present ? "default" : "outline" }
532584 size = "sm"
@@ -536,7 +588,7 @@ export default function SessionPage({ params }: PageProps) {
536588 < Button
537589 onClick = { ( e ) => {
538590 e . preventDefault ( ) ;
539- handleToggleAttendance ( student . id , false ) ;
591+ handleToggleAttendance ( student . id , student . session_student_id , false ) ;
540592 } }
541593 variant = { ! student . present ? "default" : "outline" }
542594 size = "sm"
0 commit comments