@@ -93,83 +93,90 @@ body.on('click', '.djangoAppt_btn-request-next-slot', function () {
93
93
} )
94
94
95
95
body . on ( 'click' , '.btn-submit-appointment' , function ( e ) {
96
- e . preventDefault ( ) ; // Prevent default form submission
96
+ e . preventDefault ( ) ;
97
97
98
- const selectedSlot = $ ( '.djangoAppt_appointment-slot.selected' ) . text ( ) ;
99
- const selectedDate = $ ( '.djangoAppt_date_chosen' ) . text ( ) ;
98
+ const $slot = $ ( '.djangoAppt_appointment-slot.selected' ) ;
99
+ const $date = $ ( '.djangoAppt_date_chosen' ) ;
100
+ const selectedSlot = $slot . text ( ) . trim ( ) ;
101
+ const selectedDate = $date . text ( ) . trim ( ) ;
100
102
101
- // Validate that both slot and date are selected
103
+ // Validate selection
102
104
if ( ! selectedSlot || ! selectedDate ) {
103
105
alert ( selectDateAndTimeAlertTxt ) ;
104
106
return ;
105
107
}
106
108
107
- // Additional validation - check if we actually have meaningful data
109
+ // Additional validation
110
+ const $warning = $ ( '.warning-message' ) ;
108
111
if ( ! selectedSlot . trim ( ) || ! selectedDate . trim ( ) ) {
109
- const warningContainer = $ ( '.warning-message' ) ;
110
- if ( warningContainer . find ( '.submit-warning' ) . length === 0 ) {
111
- warningContainer . append ( '<p class="submit-warning">' + selectTimeSlotWarningTxt + '</p>' ) ;
112
+ if ( $warning . find ( '.submit-warning' ) . length === 0 ) {
113
+ $warning . append ( `<p class="submit-warning">${ selectTimeSlotWarningTxt } </p>` ) ;
112
114
}
113
115
return ;
114
116
}
115
117
118
+ // Extract time and date
116
119
const startTime = convertTo24Hour ( selectedSlot ) ;
117
- const dateParts = selectedDate . split ( ', ' ) ;
118
- const monthDayYear = dateParts [ 1 ] + "," + dateParts [ 2 ] ;
119
- const formattedDate = new Date ( monthDayYear + " " + startTime ) ;
120
- const date = formattedDate . toISOString ( ) . slice ( 0 , 10 ) ;
121
- const endTimeDate = new Date ( formattedDate . getTime ( ) + serviceDuration * 60000 ) ;
122
- const endTime = formatTime ( endTimeDate ) ;
123
120
124
- // Get the staff member value
125
- const staffMember = $ ( '#staff_id' ) . val ( ) ;
121
+ const dateParts = selectedDate . split ( ', ' ) ; // e.g. ["Tuesday", "June 11", "2025"]
122
+ if ( dateParts . length < 3 ) {
123
+ alert ( 'Invalid date format. Please select a valid appointment date.' ) ;
124
+ return ;
125
+ }
126
126
127
- // Check if it's a recurring appointment
128
- const isRecurring = $ ( '#id_is_recurring' ) . is ( ':checked' ) ;
127
+ const monthDayYear = `${ dateParts [ 1 ] } ${ dateParts [ 2 ] } ` ; // "June 11 2025"
128
+ const formattedDate = new Date ( `${ monthDayYear } ${ startTime } ` ) ;
129
+ if ( isNaN ( formattedDate ) ) {
130
+ console . error ( 'Invalid formatted date:' , `${ monthDayYear } ${ startTime } ` ) ;
131
+ alert ( 'Invalid date/time. Please verify your selections.' ) ;
132
+ return ;
133
+ }
129
134
130
- if ( isRecurring ) {
131
- // Use the recurring form
132
- const recurringForm = $ ( '.appointment-form' ) ;
135
+ const date = formattedDate . toISOString ( ) . slice ( 0 , 10 ) ;
136
+ const endTime = formatTime ( new Date ( formattedDate . getTime ( ) + serviceDuration * 60000 ) ) ;
133
137
134
- // Populate hidden fields for recurring appointments
135
- $ ( '#hidden_staff_member' ) . val ( staffMember ) ;
136
- $ ( '#hidden_selected_date' ) . val ( date ) ;
137
- $ ( '#hidden_selected_time' ) . val ( startTime ) ;
138
+ const staffMember = $ ( '#staff_id' ) . val ( ) ;
139
+ const isRecurring = $ ( '#id_is_recurring' ) . is ( ':checked' ) ;
140
+ const $form = $ ( '.appointment-form' ) ;
141
+
142
+ // Prepare hidden fields
143
+ const hiddenFields = [
144
+ { name : 'date' , value : date } ,
145
+ { name : 'start_time' , value : startTime } ,
146
+ { name : 'end_time' , value : endTime } ,
147
+ { name : 'service' , value : serviceId } ,
148
+ { name : 'staff_member' , value : staffMember }
149
+ ] ;
150
+
151
+ if ( rescheduledDate ) {
152
+ hiddenFields . push ( {
153
+ name : 'reason_for_rescheduling' ,
154
+ value : $ ( '#reason_for_rescheduling' ) . val ( )
155
+ } ) ;
156
+ }
138
157
139
- if ( rescheduledDate ) {
140
- const reasonForRescheduling = $ ( '#reason_for_rescheduling' ) . val ( ) ;
141
- $ ( '#hidden_reason_for_rescheduling' ) . val ( reasonForRescheduling ) ;
142
- }
158
+ if ( ! isRecurring ) {
159
+ const actionUrl = rescheduledDate ? appointmentRescheduleURL : appointmentRequestSubmitURL ;
160
+ $form . attr ( 'action' , actionUrl ) ;
143
161
144
- // Submit the form with recurring data
145
- recurringForm . submit ( ) ;
146
- } else {
147
- // Use the original form for single appointments
148
- const form = $ ( '.appointment-form' ) ;
149
- let formAction = rescheduledDate ? appointmentRescheduleURL : appointmentRequestSubmitURL ;
150
- form . attr ( 'action' , formAction ) ;
151
-
152
- // Add hidden fields to original form
153
- if ( ! form . find ( 'input[name="appointment_request_id"]' ) . length && rescheduledDate ) {
154
- form . append ( $ ( '<input>' , {
155
- type : 'hidden' ,
162
+ if ( rescheduledDate && ! $form . find ( 'input[name="appointment_request_id"]' ) . length ) {
163
+ hiddenFields . push ( {
156
164
name : 'appointment_request_id' ,
157
165
value : appointmentRequestId
158
- } ) ) ;
159
- }
160
-
161
- form . append ( $ ( '<input>' , { type : 'hidden' , name : 'date' , value : date } ) ) ;
162
- form . append ( $ ( '<input>' , { type : 'hidden' , name : 'start_time' , value : startTime } ) ) ;
163
- form . append ( $ ( '<input>' , { type : 'hidden' , name : 'end_time' , value : endTime } ) ) ;
164
- form . append ( $ ( '<input>' , { type : 'hidden' , name : 'service' , value : serviceId } ) ) ;
165
-
166
- if ( rescheduledDate ) {
167
- const reasonForRescheduling = $ ( '#reason_for_rescheduling' ) . val ( ) ;
168
- form . append ( $ ( '<input>' , { type : 'hidden' , name : 'reason_for_rescheduling' , value : reasonForRescheduling } ) ) ;
166
+ } ) ;
169
167
}
168
+ }
170
169
171
- form . submit ( ) ;
170
+ // Append fields and submit
171
+ for ( const field of hiddenFields ) {
172
+ $form . append ( $ ( '<input>' , {
173
+ type : 'hidden' ,
174
+ name : field . name ,
175
+ value : field . value
176
+ } ) ) ;
172
177
}
178
+
179
+ $form . submit ( ) ;
173
180
} ) ;
174
181
175
182
$ ( '#staff_id' ) . on ( 'change' , function ( ) {
0 commit comments