From 0bf474a33822470ba1ceaf7e9e77061bc6c71572 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 10 Oct 2023 15:25:20 +0100 Subject: [PATCH] Bug 29002: Fix patron selection and clarify variable names Signed-off-by: Martin Renvoize Signed-off-by: Janet McGowan Signed-off-by: Caroline Cyr La Rose Signed-off-by: Laurence Rault Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- .../prog/js/place_booking_modal.js | 233 +++++++++--------- 1 file changed, 118 insertions(+), 115 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/place_booking_modal.js b/koha-tmpl/intranet-tmpl/prog/js/place_booking_modal.js index 467872fdb2..01f1041bf7 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/place_booking_modal.js +++ b/koha-tmpl/intranet-tmpl/prog/js/place_booking_modal.js @@ -1,5 +1,5 @@ let dataFetched = false; -let items, bookings, booking_id, itemnumber; +let bookable_items, bookings, booking_id, booking_item_id, booking_patron; $('#placeBookingModal').on('show.bs.modal', function(e) { @@ -9,7 +9,7 @@ $('#placeBookingModal').on('show.bs.modal', function(e) { $('#booking_biblio_id').val(biblionumber); let patron_id = button.data('patron') || 0; - itemnumber = button.data('itemnumber'); + booking_item_id = button.data('itemnumber'); let start_date = button.data('start_date'); let end_date = button.data('end_date'); @@ -24,6 +24,102 @@ $('#placeBookingModal').on('show.bs.modal', function(e) { $('#booking_id').val(''); } + // Patron select2 + $("#booking_patron_id").kohaSelect({ + dropdownParent: $(".modal-content", "#placeBookingModal"), + width: '50%', + dropdownAutoWidth: true, + allowClear: true, + minimumInputLength: 3, + ajax: { + url: '/api/v1/patrons', + delay: 250, + dataType: 'json', + headers: { + "x-koha-embed": "library" + }, + data: function(params) { + let search_term = (params.term === undefined) ? '' : params.term; + let query = { + 'q': JSON.stringify({ + "-or": [{ + "firstname": { + "-like": search_term + '%' + } + }, + { + "surname": { + "-like": search_term + '%' + } + }, + { + "cardnumber": { + "-like": search_term + '%' + } + } + ] + }), + '_order_by': '+me.surname,+me.firstname', + '_page': params.page, + }; + return query; + }, + processResults: function(data, params) { + let results = []; + data.results.forEach(function(patron) { + patron.id = patron.patron_id; + results.push(patron); + }); + return { + "results": results, "pagination": { "more": data.pagination.more } + }; + }, + }, + templateResult: function (patron) { + if (patron.library_id == loggedInLibrary) { + loggedInClass = "ac-currentlibrary"; + } else { + loggedInClass = ""; + } + + let $patron = $("") + .append( + "" + + (patron.surname + ? escape_str(patron.surname) + ", " + : "") + + (patron.firstname + ? escape_str(patron.firstname) + " " + : "") + + (patron.cardnumber + ? " (" + escape_str(patron.cardnumber) + ") " + : "") + + (patron.date_of_birth + ? '' + + $get_age(patron.date_of_birth) + + " " + + __("years") + + "" + : "") + ) + .addClass(loggedInClass); + return $patron; + }, + templateSelection: function (patron) { + if (!patron.surname) { + return patron.text; + } + return ( + escape_str(patron.surname) + ", " + escape_str(patron.firstname) + ); + }, + placeholder: "Search for a patron" + }); + + $('#booking_patron_id').on('select2:select', function (e) { + booking_patron = e.params.data; + }); + // Adopt periodPicker let periodPicker = $("#period").get(0)._flatpickr; @@ -48,7 +144,7 @@ $('#placeBookingModal').on('show.bs.modal', function(e) { function(itemsFetch,bookingsFetch){ // Set variables - items = itemsFetch[0]; + bookable_items = itemsFetch[0]; bookings = bookingsFetch[0]; // Item select2 @@ -66,7 +162,7 @@ $('#placeBookingModal').on('show.bs.modal', function(e) { // Total bookable items let bookable = 0; - for (item of items) { + for (item of bookable_items) { bookable++; // Populate item select (NOTE: Do we still need this check for pre-existing select option here?) if (!($('#booking_item_id').find("option[value='" + item.item_id + "']").length)) { @@ -159,7 +255,7 @@ $('#placeBookingModal').on('show.bs.modal', function(e) { // check that there are available items // available = all bookable items - booked items - booked biblios - let total_available = items.length - unavailable_items.length - biblio_bookings.length; + let total_available = bookable_items.length - unavailable_items.length - biblio_bookings.length; if (total_available === 0) { return true; } @@ -169,7 +265,7 @@ $('#placeBookingModal').on('show.bs.modal', function(e) { else if (date <= end_date && date >= start_date) { // same item, disable date - if (booking.item_id && booking.item_id == itemnumber) { + if (booking.item_id && booking.item_id == booking_item_id) { return true; } @@ -197,7 +293,7 @@ $('#placeBookingModal').on('show.bs.modal', function(e) { // Setup listener for item select2 $('#booking_item_id').on('select2:select', function(e) { - itemnumber = e.params.data.id ? e.params.data.id : null; + booking_item_id = e.params.data.id ? e.params.data.id : null; // redraw pariodPicker taking selected item into account periodPicker.redraw(); @@ -232,7 +328,7 @@ $('#placeBookingModal').on('show.bs.modal', function(e) { }); $("#booking_item_id > option").each(function() { let option = $(this); - if ( itemnumber && itemnumber == option.val() ) { + if ( booking_item_id && booking_item_id == option.val() ) { option.prop('disabled',false); } else if ( booked_items.some(function(booked_item){ return option.val() == booked_item.item_id; @@ -266,116 +362,22 @@ $('#placeBookingModal').on('show.bs.modal', function(e) { dataFetched = true; // Set form values - setFormValues(patron_id,itemnumber,start_date,end_date,periodPicker); + setFormValues(patron_id,booking_item_id,start_date,end_date,periodPicker); }, function(jqXHR, textStatus, errorThrown){ console.log("Fetch failed"); } ); } else { - setFormValues(patron_id,itemnumber,start_date,end_date,periodPicker); + setFormValues(patron_id,booking_item_id,start_date,end_date,periodPicker); }; - - // Setup forms - - // Patron select2 - $("#booking_patron_id").kohaSelect({ - dropdownParent: $(".modal-content", "#placeBookingModal"), - width: '50%', - dropdownAutoWidth: true, - allowClear: true, - minimumInputLength: 3, - ajax: { - url: '/api/v1/patrons', - delay: 250, - dataType: 'json', - headers: { - "x-koha-embed": "library" - }, - data: function(params) { - let search_term = (params.term === undefined) ? '' : params.term; - let query = { - 'q': JSON.stringify({ - "-or": [{ - "firstname": { - "-like": search_term + '%' - } - }, - { - "surname": { - "-like": search_term + '%' - } - }, - { - "cardnumber": { - "-like": search_term + '%' - } - } - ] - }), - '_order_by': '+me.surname,+me.firstname', - '_page': params.page, - }; - return query; - }, - processResults: function(data, params) { - let results = []; - data.results.forEach(function(patron) { - patron.id = patron.patron_id; - results.push(patron); - }); - return { - "results": results, "pagination": { "more": data.pagination.more } - }; - }, - }, - templateResult: function (patron) { - if (patron.library_id == loggedInLibrary) { - loggedInClass = "ac-currentlibrary"; - } else { - loggedInClass = ""; - } - - let $patron = $("") - .append( - "" + - (patron.surname - ? escape_str(patron.surname) + ", " - : "") + - (patron.firstname - ? escape_str(patron.firstname) + " " - : "") + - (patron.cardnumber - ? " (" + escape_str(patron.cardnumber) + ") " - : "") + - (patron.date_of_birth - ? '' + - $get_age(patron.date_of_birth) + - " " + - __("years") + - "" - : "") - ) - .addClass(loggedInClass); - return $patron; - }, - templateSelection: function (patron) { - if (!patron.surname) { - return patron.text; - } - return ( - escape_str(patron.surname) + ", " + escape_str(patron.firstname) - ); - }, - placeholder: "Search for a patron" - }); - }); -function setFormValues(patron_id,itemnumber,start_date,end_date,periodPicker){ +function setFormValues(patron_id,booking_item_id,start_date,end_date,periodPicker){ // If passed patron, pre-select if (patron_id) { + let patronSelect = $('#booking_patron_id'); let patron = $.ajax({ url: '/api/v1/patrons/' + patron_id, dataType: 'json', @@ -384,14 +386,17 @@ function setFormValues(patron_id,itemnumber,start_date,end_date,periodPicker){ $.when(patron).done( function(patron){ - let newOption = new Option(escape_str(patron.surname) + ", " + escape_str(patron.firstname), patron.patron_id, true, true); - $('#booking_patron_id').append(newOption).trigger('change'); // clone patron_id to id (select2 expects an id field) patron.id = patron.patron_id; + patron.text = escape_str(patron.surname) + ", " + escape_str(patron.firstname); + + // Add and select new option + let newOption = new Option(patron.text, patron.id, true, true); + patronSelect.append(newOption).trigger('change'); // manually trigger the `select2:select` event - $('#booking_patron_id').trigger({ + patronSelect.trigger({ type: 'select2:select', params: { data: patron @@ -418,8 +423,8 @@ function setFormValues(patron_id,itemnumber,start_date,end_date,periodPicker){ }; // If passed an itemnumber, pre-select - if (itemnumber) { - $('#booking_item_id').val(itemnumber).trigger('change'); + if (booking_item_id) { + $('#booking_item_id').val(booking_item_id).trigger('change'); } } @@ -450,14 +455,13 @@ $("#placeBookingForm").on('submit', function(e) { bookings_table.api().ajax.reload(); } if (typeof timeline !== 'undefined' && timeline !== null) { - let selected_patron = $("#booking_patron_id").select2('data')[0]; timeline.itemsData.add({ id: data.booking_id, booking: data.booking_id, patron: data.patron_id, start: dayjs(data.start_date).toDate(), end: dayjs(data.end_date).toDate(), - content: $patron_to_html(selected_patron, { + content: $patron_to_html(booking_patron, { display_cardnumber: true, url: false }), @@ -501,14 +505,13 @@ $("#placeBookingForm").on('submit', function(e) { bookings_table.api().ajax.reload(); } if (typeof timeline !== 'undefined' && timeline !== null) { - let selected_patron = $("#booking_patron_id").select2('data')[0]; timeline.itemsData.update({ id: data.booking_id, booking: data.booking_id, patron: data.patron_id, start: dayjs(data.start_date).toDate(), end: dayjs(data.end_date).toDate(), - content: $patron_to_html(selected_patron, { + content: $patron_to_html(booking_patron, { display_cardnumber: true, url: false }), -- 2.39.5