Skip to content

Commit 49a4909

Browse files
committed
fix hours left field
1 parent 627f4cc commit 49a4909

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

backend/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Firebase Service Account Key
22
src/firebase/ServiceAccountKey.json
33

4-
.env
4+
*.env
55
node_modules/
66
.eslintcache
77
ServiceAccountKey.json

frontend/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ yarn-error.log*
3636
next-env.d.ts
3737

3838
# env
39-
.env
39+
*.env

frontend/src/components/StudentForm/EnrollmentsEdit.tsx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
UseFormRegister,
77
useFieldArray,
88
useFormContext,
9+
useWatch,
910
} from "react-hook-form";
1011

1112
import { Student } from "../../api/students";
@@ -133,6 +134,17 @@ const EnrollmentFormItem = ({
133134
);
134135
}, [item.programId]);
135136

137+
// Watch for changes to startDate and renewalDate
138+
const watchedStartDate = useWatch<StudentFormData>({
139+
control: useFormContext<StudentFormData>().control,
140+
name: `${fieldName}.${index}.startDate`,
141+
});
142+
143+
const watchedRenewalDate = useWatch<StudentFormData>({
144+
control: useFormContext<StudentFormData>().control,
145+
name: `${fieldName}.${index}.renewalDate`,
146+
});
147+
136148
// these 3 useEffects keep our custom dropdown and react-hook-form in sync
137149
const calculateHoursLeft = (
138150
startDate: Date,
@@ -141,7 +153,6 @@ const EnrollmentFormItem = ({
141153
sessionStartTime: string,
142154
sessionEndTime: string,
143155
): number => {
144-
console.log(startDate, endDate, daysOfWeek, sessionStartTime, sessionEndTime);
145156
const dayMap = {
146157
Su: "Sunday",
147158
M: "Monday",
@@ -184,19 +195,25 @@ const EnrollmentFormItem = ({
184195
useEffect(() => {
185196
item.sessionTime = selectedSession;
186197
setValue(`${fieldName}.${index}.sessionTime`, selectedSession);
187-
if (!varying && selectedSession.start_time && selectedSession.end_time) {
198+
if (!varying && watchedStartDate && watchedRenewalDate && selectedSession) {
199+
if (item.schedule.length === 0) {
200+
programsMap[item.programId]?.daysOfWeek.forEach((day) => {
201+
item.schedule.push(day);
202+
});
203+
}
204+
188205
setValue(
189206
`${fieldName}.${index}.hoursLeft`,
190207
calculateHoursLeft(
191-
new Date(item.startDate),
192-
new Date(item.renewalDate),
208+
new Date(watchedStartDate as string),
209+
new Date(watchedRenewalDate as string),
193210
item.schedule,
194211
selectedSession.start_time,
195212
selectedSession.end_time,
196213
),
197214
);
198215
}
199-
}, [selectedSession]);
216+
}, [selectedSession, watchedStartDate, watchedRenewalDate]);
200217

201218
useEffect(() => {
202219
const progId = Object.keys(programsMap).find(

0 commit comments

Comments
 (0)