Skip to content

Commit ff93c46

Browse files
committed
fix: move client-side attendance logic to server
1 parent b7cb194 commit ff93c46

35 files changed

+5081
-1866
lines changed

src/Mutations/Attendance.tsx

Lines changed: 355 additions & 61 deletions
Large diffs are not rendered by default.

src/components/AttendanceSymbols.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function AttendanceSymbols({ status }: Props) {
1717
const property: Property = {
1818
color: 'bg-[#0E8E0B]',
1919
// icon: <i className="fa-solid fa-check font-semibold" />,
20-
icon: <FaCheck className="text-[.8rem]" />,
20+
icon: <FaCheck className="mt-[.1rem] md:mt-0 text-[.75rem] md:text-[.8rem]" />,
2121
};
2222

2323
if (score === '1') {
@@ -26,11 +26,13 @@ function AttendanceSymbols({ status }: Props) {
2626
}
2727
if (score === '0') {
2828
property.color = 'bg-[#C30909]';
29-
property.icon = <FaXmark className="text-base" />;
29+
property.icon = (
30+
<FaXmark className="-ml-[.1rem] md:ml-0 text-[.93rem] md:text-base" />
31+
);
3032
}
3133
return (
3234
<div
33-
className={`${property.color} flex items-center justify-center w-[1.5rem] h-[1.5rem] rounded-full text-white`}
35+
className={`${property.color} flex items-center justify-center w-[1.3rem] h-[1.3rem] md:w-[1.5rem] md:h-[1.5rem] rounded-full text-white`}
3436
>
3537
{property.icon}
3638
</div>

src/components/EditAttendenceButton.tsx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React, { useEffect, useState } from 'react';
2+
import { number } from 'zod';
23
import AttendanceSymbols from './AttendanceSymbols';
34
import { TraineeAttendanceDataInterface } from '../pages/TraineeAttendanceTracker';
45

56
interface EditAttendanceProps {
67
setTraineeAttendanceData: React.Dispatch<React.SetStateAction<any[]>>;
78
setUpdated: React.Dispatch<React.SetStateAction<boolean>>;
8-
week: string;
9+
week: number;
910
day: string;
1011
phase: string;
1112
traineeId: string;
@@ -25,21 +26,22 @@ function EditAttendanceButton({
2526
setOpenEdit(false);
2627
}, [week, phase, day]);
2728

28-
const handleUpdateAttendance = (score: string) => {
29+
const handleUpdateAttendance = (score: number) => {
30+
2931
setTraineeAttendanceData((prev) =>
3032
prev.map((attendanceData) => {
31-
if (attendanceData.week === week && attendanceData.phase === phase) {
33+
if (attendanceData.week === week && attendanceData.phase.id === phase) {
3234
const updatedDay = attendanceData.days[
3335
day as keyof typeof attendanceData.days
3436
].map((traineeData: TraineeAttendanceDataInterface) => {
3537
if (
3638
traineeData.trainee.id === traineeId &&
37-
traineeData.status.toString() !== score.toString()
39+
traineeData.score !== score
3840
) {
3941
setUpdated(true);
4042
return {
4143
trainee: traineeData.trainee,
42-
status: score,
44+
score,
4345
};
4446
}
4547
return traineeData;
@@ -61,30 +63,21 @@ function EditAttendanceButton({
6163
{!openEdit && (
6264
<span
6365
onClick={() => setOpenEdit(true)}
64-
className="px-2 py-[2px] border dark:border-white border-black rounded-md text-[.85rem]"
66+
className="px-3 py-[3px] border dark:border-white border-black rounded-md font-medium text-[.85rem]"
6567
data-testid="edit-button"
6668
>
6769
Edit
6870
</span>
6971
)}
7072
{openEdit && (
7173
<div className="flex items-center gap-1 xmd:gap-2">
72-
<div
73-
onClick={() => handleUpdateAttendance('2')}
74-
data-testid="score-2"
75-
>
74+
<div onClick={() => handleUpdateAttendance(2)} data-testid="score-2">
7675
<AttendanceSymbols status={2} />
7776
</div>
78-
<div
79-
onClick={() => handleUpdateAttendance('1')}
80-
data-testid="score-1"
81-
>
77+
<div onClick={() => handleUpdateAttendance(1)} data-testid="score-1">
8278
<AttendanceSymbols status={1} />
8379
</div>
84-
<div
85-
onClick={() => handleUpdateAttendance('0')}
86-
data-testid="score-0"
87-
>
80+
<div onClick={() => handleUpdateAttendance(0)} data-testid="score-0">
8881
<AttendanceSymbols status={0} />
8982
</div>
9083
</div>

src/components/ModalAttendance.tsx

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,30 @@ import { useMutation } from '@apollo/client';
44
import { format } from 'date-fns';
55
import { RECORD_ATTENDANCE } from '../Mutations/Attendance';
66
import AttendanceSymbols from './AttendanceSymbols';
7+
import { AttendanceDataInterface } from '../pages/TraineeAttendanceTracker';
78

89
interface 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
}
1922
interface 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

3128
export interface recordTraineeProps {
3229
trainee: string;
33-
status: StatusInput;
30+
score: number;
3431
}
3532

3633
function 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"

src/components/Sidebar.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,32 @@ function Sidebar({ style, toggle }: { style: string; toggle: () => void }) {
140140
<SideNavLink onClick={toggle} to="/sessions" name="Sessions">
141141
<BookOpenIcon className="w-5" />
142142
</SideNavLink>
143-
<SideNavLink onClick={toggle} name="Attendance" to="/attendance">
144-
<ClipboardCheckIcon className="w-5 " />
145-
</SideNavLink>
146143

147144
<SideNavLink onClick={toggle} name="Docs" to="/coordinatorDocs">
148145
<FolderIcon className="w-5" />
149146
</SideNavLink>
150147
</CheckRole>
151148

152-
{/* manger role */}
153-
<CheckRole roles={['manager']}>
154-
<SideNavLink onClick={toggle} name="Teams" to="/team-cards">
155-
<UserGroupIcon className="w-5" />
156-
</SideNavLink>
157-
</CheckRole>
158149
{/* TTL role */}
159150
<CheckRole roles={['ttl']}>
160151
<SideNavLink onClick={toggle} to="/ttl-trainees" name="Trainees">
161152
<UserGroupIcon className="w-5" />
162153
</SideNavLink>
163154
</CheckRole>
155+
156+
{/* FOR COORDINATORS AND A TTL */}
157+
<CheckRole roles={['coordinator', 'ttl']}>
158+
<SideNavLink onClick={toggle} name="Attendance" to="/attendance">
159+
<ClipboardCheckIcon className="w-5 " />
160+
</SideNavLink>
161+
</CheckRole>
162+
163+
{/* manger role */}
164+
<CheckRole roles={['manager']}>
165+
<SideNavLink onClick={toggle} name="Teams" to="/team-cards">
166+
<UserGroupIcon className="w-5" />
167+
</SideNavLink>
168+
</CheckRole>
164169

165170
<CheckRole roles={['ttl']}>
166171
<SideNavLink onClick={toggle} to="/docs/ttl" name="Docs">
@@ -186,7 +191,7 @@ function Sidebar({ style, toggle }: { style: string; toggle: () => void }) {
186191
</SideNavLink>
187192

188193
<SideNavLink onClick={toggle} name="Help" to="/support">
189-
<SupportIcon className="w-5 text-red-700 dark:text-red-600 hover:text-red-900" />
194+
<SupportIcon className="w-5" />
190195
</SideNavLink>
191196
<SideNavLink onClick={logout} to="#link">
192197
<LogoutIcon className="w-5 text-red-700 dark:text-red-600 hover:text-red-900" />

src/containers/admin-dashBoard/Sessions.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ function AdminSission() {
158158
getSessionRefetch();
159159
})
160160
.catch((error) => {});
161-
// console.log("------",id)
162161
}
163162
};
164163

src/pages/Attendance.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const TraineeAttendance = React.lazy(
1010
function Attendance() {
1111
return (
1212
<>
13-
<CheckRole roles={['coordinator']}>
13+
<CheckRole roles={['coordinator', 'ttl']}>
1414
<TraineeAttendanceTracker />
1515
</CheckRole>
1616
<CheckRole roles={['trainee']}>

src/pages/Organization/Orglogin.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ function Orglogin() {
8888
}
8989
};
9090
const names = name.replace(/ /gi, '').toLowerCase();
91-
// console.log(names)
9291
const completeOrgUrl = `${names}`;
9392

9493
return (

0 commit comments

Comments
 (0)