@@ -180,9 +180,28 @@ function formatTime(date) {
180
180
}
181
181
182
182
function getAvailableSlots ( selectedDate , staffId = null ) {
183
- // Send an AJAX request to get the available slots for the selected date
184
- if ( staffId === null ) {
185
- return ;
183
+ // Update the slot list with the available slots for the selected date
184
+ const slotList = $ ( '#slot-list' ) ;
185
+ const slotContainer = $ ( '.slot-container' ) ;
186
+ const errorMessageContainer = $ ( '.error-message' ) ;
187
+
188
+ // Clear previous error messages and slots
189
+ slotList . empty ( ) ;
190
+ errorMessageContainer . find ( '.djangoAppt_no-availability-text' ) . remove ( ) ;
191
+
192
+ // Remove the "Next available date" message
193
+ nextAvailableDateSelector = $ ( '.djangoAppt_next-available-date' ) ; // Update the selector
194
+ nextAvailableDateSelector . remove ( ) ;
195
+
196
+ // Correctly check if staffId is 'none', null, or undefined and exit the function if true
197
+ // Check if staffId is 'none', null, or undefined and display an error message
198
+ if ( staffId === 'none' || staffId === null || staffId === undefined ) {
199
+ console . log ( 'No staff ID provided, displaying error message.' ) ;
200
+ const errorMessage = $ ( '<p class="djangoAppt_no-availability-text">' + noStaffMemberSelected + '</p>' ) ;
201
+ errorMessageContainer . append ( errorMessage ) ;
202
+ // Optionally disable the submit button here
203
+ $ ( '.btn-submit-appointment' ) . attr ( 'disabled' , 'disabled' ) ;
204
+ return ; // Exit the function early
186
205
}
187
206
188
207
let ajaxData = {
@@ -200,28 +219,20 @@ function getAvailableSlots(selectedDate, staffId = null) {
200
219
console . error ( 'Failed to get non-working days:' , nonWorkingDays ) ;
201
220
}
202
221
} ) ;
222
+
223
+ // Send an AJAX request to get the available slots for the selected date
203
224
$ . ajax ( {
204
225
url : availableSlotsAjaxURL ,
205
226
data : ajaxData ,
206
227
dataType : 'json' ,
207
228
success : function ( data ) {
208
- // Update the slot list with the available slots for the selected date
209
- const slotList = $ ( '#slot-list' ) ;
210
- slotList . empty ( ) ;
211
- const slotContainer = $ ( '.slot-container' ) ;
212
- // Remove the "Next available date" message
213
- nextAvailableDateSelector = $ ( '.djangoAppt_next-available-date' ) ; // Update the selector
214
- nextAvailableDateSelector . remove ( ) ;
215
- const errorMessageContainer = $ ( '.error-message' ) ;
216
-
217
229
if ( data . available_slots . length === 0 ) {
218
230
const selectedDateObj = moment . tz ( selectedDate , timezone ) ;
219
231
const selectedD = selectedDateObj . toDate ( ) ;
220
232
const today = new Date ( ) ;
221
233
today . setHours ( 0 , 0 , 0 , 0 ) ;
222
234
223
235
if ( selectedD < today ) {
224
- errorMessageContainer . find ( '.djangoAppt_no-availability-text' ) . remove ( ) ;
225
236
// Show an error message
226
237
errorMessageContainer . append ( '<p class="djangoAppt_no-availability-text">Date is in the past.</p>' ) ;
227
238
if ( slotContainer . find ( '.djangoAppt_btn-request-next-slot' ) . length === 0 ) {
0 commit comments