Skip to content

Commit

Permalink
fix(attendance): fixes recording attendance bug
Browse files Browse the repository at this point in the history
  • Loading branch information
aimedivin committed Oct 8, 2024
1 parent 213831b commit 953568f
Show file tree
Hide file tree
Showing 14 changed files with 1,309 additions and 1,056 deletions.
5 changes: 5 additions & 0 deletions src/Mutations/Attendance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ export const RECORD_ATTENDANCE = gql`
id
name
cohort {
id
name
phase {
name
id
}
}
}
trainees {
Expand Down
20 changes: 20 additions & 0 deletions src/Mutations/teamMutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ export const GET_ALL_TEAMS = gql`
id
}
}
members {
id
email
status {
date
reason
status
}
profile {
firstName
lastName
city
country
phoneNumber
biography
avatar
id
name
}
}
}
}
`;
Expand Down
53 changes: 46 additions & 7 deletions src/components/ModalAttendance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,41 @@ function ModalAttendance({
if (data) {
toast.success('Attendance recorded successfully.');
}
setAttendanceData((prev) => [...prev, data.recordAttendance]);

setAttendanceData((prev) => {
let isSet = false;
const result = prev.map((attendance) => {
if (
attendance.week.toString() === week.toString() &&
attendance.cohort.id === data.recordAttendance.team.cohort.id &&
attendance.phase.id === data.recordAttendance.team.cohort.phase.id
) {
isSet = true;
return {
...attendance,
teams: [data.recordAttendance],
};
}
return attendance;
});

if (!isSet && team === data.recordAttendance.team.id) {
result.push({
week: week.toString(),
id: Math.random().toString(),
cohort: {
id: data.recordAttendance.team.cohort.id,
name: data.recordAttendance.team.cohort.name,
},
phase: {
id: data.recordAttendance.team.cohort.phase.id,
name: data.recordAttendance.team.cohort.phase.id,
},
teams: [data.recordAttendance],
});
}
return result;
});
setTraineesAttendance([]);
setRecordTrainees([]);
onClose();
Expand All @@ -90,10 +124,12 @@ function ModalAttendance({
}, [recordTrainees]);

useEffect(() => {
const sanitizedTrainees = trainees.getTeamTrainees.filter(
(trainee: any) => trainee.status.status !== 'drop',
);
setAllTrainees(sanitizedTrainees);
if (trainees) {
const sanitizedTrainees = trainees.filter(
(trainee: any) => trainee.status.status !== 'drop',
);
setAllTrainees(sanitizedTrainees);
}
}, [trainees]);
useEffect(() => {
setFilteredTrainees(
Expand All @@ -113,11 +149,11 @@ function ModalAttendance({
) => {
let updatedAttendance;
const updateAttendance = traineesAttendance.find(
(attendance) => attendance.name.toLowerCase() === name.toLowerCase(),
(attendance) => attendance.id === id,
);
if (updateAttendance) {
updatedAttendance = traineesAttendance.map((attendance) => {
if (attendance.name.toLowerCase() === name.toLowerCase()) {
if (attendance.id === id) {
return { ...attendance, score };
}
return attendance;
Expand Down Expand Up @@ -255,6 +291,7 @@ function ModalAttendance({
day,
);
}}
className="cursor-pointer hover:brightness-75"
data-testid="test-score-2"
>
<AttendanceSymbols status={2} />
Expand All @@ -269,6 +306,7 @@ function ModalAttendance({
day,
);
}}
className="cursor-pointer hover:brightness-75"
data-testid="test-score-1"
>
<AttendanceSymbols status={1} />
Expand All @@ -283,6 +321,7 @@ function ModalAttendance({
day,
);
}}
className="cursor-pointer hover:brightness-75"
data-testid="test-score-0"
>
<AttendanceSymbols status={0} />
Expand Down
Loading

0 comments on commit 953568f

Please sign in to comment.