Skip to content

Commit 32232eb

Browse files
authored
No non-necessary ajax request sent when no staff member selected (#93)
1 parent f0f5b3f commit 32232eb

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

appointment/static/js/appointments.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,28 @@ function formatTime(date) {
180180
}
181181

182182
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
186205
}
187206

188207
let ajaxData = {
@@ -200,28 +219,20 @@ function getAvailableSlots(selectedDate, staffId = null) {
200219
console.error('Failed to get non-working days:', nonWorkingDays);
201220
}
202221
});
222+
223+
// Send an AJAX request to get the available slots for the selected date
203224
$.ajax({
204225
url: availableSlotsAjaxURL,
205226
data: ajaxData,
206227
dataType: 'json',
207228
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-
217229
if (data.available_slots.length === 0) {
218230
const selectedDateObj = moment.tz(selectedDate, timezone);
219231
const selectedD = selectedDateObj.toDate();
220232
const today = new Date();
221233
today.setHours(0, 0, 0, 0);
222234

223235
if (selectedD < today) {
224-
errorMessageContainer.find('.djangoAppt_no-availability-text').remove();
225236
// Show an error message
226237
errorMessageContainer.append('<p class="djangoAppt_no-availability-text">Date is in the past.</p>');
227238
if (slotContainer.find('.djangoAppt_btn-request-next-slot').length === 0) {

appointment/templates/appointment/appointments.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ <h1 class="page-title">{{ service.name }}</h1>
111111
</script>
112112
<script>
113113
const requestNonAvailableSlotBtnTxt = "{% trans 'Request next available slot' %}";
114+
const noStaffMemberSelected = "{% trans 'No staff member selected.' %}"
114115
</script>
115116
<script src="{% static 'js/appointments.js' %}"></script>
116117
<script src="{% static 'js/js-utils.js' %}"></script>

appointment/tests/test_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_get_available_slots_ajax(self):
7676
url = reverse('appointment:available_slots_ajax')
7777
response = self.client.get(url, {'selected_date': date.today().isoformat()},
7878
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
79-
self.assertEqual(response.status_code, 200)
79+
self.assertEqual(response.status_code, 403)
8080
response_data = response.json()
8181
self.assertIn('date_chosen', response_data)
8282
self.assertIn('available_slots', response_data)

appointment/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def get_available_slots_ajax(request):
6363
custom_data['error'] = False
6464
message = _('No staff member selected')
6565
return json_response(message=message, custom_data=custom_data, success=False,
66-
error_code=ErrorCode.STAFF_ID_REQUIRED)
66+
error_code=ErrorCode.STAFF_ID_REQUIRED, status=403)
6767

6868
sm = get_object_or_404(StaffMember, pk=staff_id)
6969
custom_data['staff_member'] = sm.get_staff_member_name()

0 commit comments

Comments
 (0)