@@ -4,33 +4,30 @@ import { useMutation } from '@apollo/client';
44import { format } from 'date-fns' ;
55import { RECORD_ATTENDANCE } from '../Mutations/Attendance' ;
66import AttendanceSymbols from './AttendanceSymbols' ;
7+ import { AttendanceDataInterface } from '../pages/TraineeAttendanceTracker' ;
78
89interface ModalProps {
910 isVisible : boolean ;
1011 onClose : ( ) => void ;
11- setAttendanceData : React . Dispatch < React . SetStateAction < any [ ] > > ;
12+ setAttendanceData : React . Dispatch <
13+ React . SetStateAction < AttendanceDataInterface | undefined >
14+ > ;
1215 trainees : any ;
1316 week : number ;
1417 date : string ;
15- day : string ;
18+ dayType : 'today' | 'yesterday' | 'others' ;
1619 team : string ;
1720 teamName : string ;
1821}
1922interface attendanceProps {
2023 name : string ;
2124 score : number ;
2225 id : string ;
23- day : string ;
24- }
25-
26- interface StatusInput {
27- day : string ;
28- score : string ;
2926}
3027
3128export interface recordTraineeProps {
3229 trainee : string ;
33- status : StatusInput ;
30+ score : number ;
3431}
3532
3633function ModalAttendance ( {
@@ -39,7 +36,7 @@ function ModalAttendance({
3936 trainees,
4037 week,
4138 date,
42- day ,
39+ dayType ,
4340 setAttendanceData,
4441 team,
4542 teamName,
@@ -60,52 +57,18 @@ function ModalAttendance({
6057 { data : recordAttendanceData , loading : loadingRecordAttendance , error } ,
6158 ] = useMutation ( RECORD_ATTENDANCE , {
6259 variables : {
60+ today : dayType === 'today' ,
61+ yesterday : dayType === 'yesterday' ,
6362 week,
6463 team,
65- date,
6664 trainees : recordTrainees ,
6765 orgToken : localStorage . getItem ( 'orgToken' ) ,
6866 } ,
6967 fetchPolicy : 'no-cache' ,
7068 onCompleted : ( data ) => {
71- if ( data ) {
72- toast . success ( 'Attendance recorded successfully.' ) ;
73- }
69+ toast . success ( 'Attendance recorded successfully.' ) ;
7470
75- setAttendanceData ( ( prev ) => {
76- let isSet = false ;
77- const result = prev . map ( ( attendance ) => {
78- if (
79- attendance . week . toString ( ) === week . toString ( ) &&
80- attendance . cohort . id === data . recordAttendance . team . cohort . id &&
81- attendance . phase . id === data . recordAttendance . team . cohort . phase . id
82- ) {
83- isSet = true ;
84- return {
85- ...attendance ,
86- teams : [ data . recordAttendance ] ,
87- } ;
88- }
89- return attendance ;
90- } ) ;
91-
92- if ( ! isSet && team === data . recordAttendance . team . id ) {
93- result . push ( {
94- week : week . toString ( ) ,
95- id : Math . random ( ) . toString ( ) ,
96- cohort : {
97- id : data . recordAttendance . team . cohort . id ,
98- name : data . recordAttendance . team . cohort . name ,
99- } ,
100- phase : {
101- id : data . recordAttendance . team . cohort . phase . id ,
102- name : data . recordAttendance . team . cohort . phase . name ,
103- } ,
104- teams : [ data . recordAttendance ] ,
105- } ) ;
106- }
107- return result ;
108- } ) ;
71+ setAttendanceData ( data . recordAttendance ) ;
10972 setTraineesAttendance ( [ ] ) ;
11073 setRecordTrainees ( [ ] ) ;
11174 onClose ( ) ;
@@ -141,12 +104,7 @@ function ModalAttendance({
141104
142105 if ( ! isVisible ) return null ;
143106
144- const handleGiveAttendance = (
145- name : string ,
146- score : number ,
147- id : string ,
148- day : string ,
149- ) => {
107+ const handleGiveAttendance = ( name : string , score : number , id : string ) => {
150108 let updatedAttendance ;
151109 const updateAttendance = traineesAttendance . find (
152110 ( attendance ) => attendance . id === id ,
@@ -159,7 +117,7 @@ function ModalAttendance({
159117 return attendance ;
160118 } ) ;
161119 } else {
162- updatedAttendance = [ ...traineesAttendance , { name, score, id, day } ] ;
120+ updatedAttendance = [ ...traineesAttendance , { name, score, id } ] ;
163121 }
164122
165123 setTraineesAttendance ( updatedAttendance ) ;
@@ -175,10 +133,7 @@ function ModalAttendance({
175133
176134 const updatedRecords = traineesAttendance . map ( ( attendance ) => ( {
177135 trainee : attendance . id ,
178- status : {
179- day : attendance . day ,
180- score : attendance . score . toString ( ) ,
181- } ,
136+ score : attendance . score ,
182137 } ) ) ;
183138
184139 setRecordTrainees ( updatedRecords ) ;
@@ -288,7 +243,6 @@ function ModalAttendance({
288243 trainee . profile . name ,
289244 2 ,
290245 trainee . id ,
291- day ,
292246 ) ;
293247 } }
294248 className = "cursor-pointer hover:brightness-75"
@@ -303,7 +257,6 @@ function ModalAttendance({
303257 trainee . profile . name ,
304258 1 ,
305259 trainee . id ,
306- day ,
307260 ) ;
308261 } }
309262 className = "cursor-pointer hover:brightness-75"
@@ -318,7 +271,6 @@ function ModalAttendance({
318271 trainee . profile . name ,
319272 0 ,
320273 trainee . id ,
321- day ,
322274 ) ;
323275 } }
324276 className = "cursor-pointer hover:brightness-75"
0 commit comments