66 UseFormRegister ,
77 useFieldArray ,
88 useFormContext ,
9+ useWatch ,
910} from "react-hook-form" ;
1011
1112import { 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