Lines 8-28
let bookable_items,
Link Here
|
8 |
booking_patron, |
8 |
booking_patron, |
9 |
booking_itemtype_id; |
9 |
booking_itemtype_id; |
10 |
|
10 |
|
|
|
11 |
/** |
12 |
* @param {number[]} integers1 - The first array of integers to check. |
13 |
* @param {number[]} integers2 - The second array of integers to be checked against. |
14 |
* @returns {boolean} - Returns true if any element from integers1 is found in integers2, otherwise false. |
15 |
*/ |
11 |
function containsAny(integers1, integers2) { |
16 |
function containsAny(integers1, integers2) { |
12 |
// Create a hash set to store integers from the second array |
17 |
return integers1.some(integer => new Set(integers2).has(integer)); |
13 |
let integerSet = {}; |
|
|
14 |
for (let i = 0; i < integers2.length; i++) { |
15 |
integerSet[integers2[i]] = true; |
16 |
} |
17 |
|
18 |
// Check if any integer from the first array exists in the hash set |
19 |
for (let i = 0; i < integers1.length; i++) { |
20 |
if (integerSet[integers1[i]]) { |
21 |
return true; // Found a match, return true |
22 |
} |
23 |
} |
24 |
|
25 |
return false; // No match found |
26 |
} |
18 |
} |
27 |
|
19 |
|
28 |
$("#placeBookingModal").on("show.bs.modal", function (e) { |
20 |
$("#placeBookingModal").on("show.bs.modal", function (e) { |
Lines 189-229
$("#placeBookingModal").on("show.bs.modal", function (e) {
Link Here
|
189 |
allowClear: false, |
181 |
allowClear: false, |
190 |
placeholder: __("Pickup location"), |
182 |
placeholder: __("Pickup location"), |
191 |
}); |
183 |
}); |
192 |
function setLocationsPicker(response) { |
184 |
function setLocationsPicker(response, booking_patron) { |
193 |
let $pickupSelect = $("#pickup_library_id"); |
185 |
let $pickupSelect = $("#pickup_library_id"); |
194 |
let bookableItemnumbers = bookable_items.map(function (object) { |
186 |
let bookableItemnumbers = bookable_items.map(function (object) { |
195 |
return object.item_id; |
187 |
return object.item_id; |
196 |
}); |
188 |
}); |
197 |
$pickupSelect.empty(); |
189 |
$pickupSelect.empty(); |
198 |
|
190 |
|
199 |
$.each(response, function (index, pickup_location) { |
191 |
const filtered_pickup_locations = response.filter(({ pickup_items }) => |
200 |
if ( |
192 |
containsAny(pickup_items, bookableItemnumbers) |
201 |
containsAny(pickup_location.pickup_items, bookableItemnumbers) |
193 |
); |
202 |
) { |
194 |
$.each(filtered_pickup_locations, function (index, pickup_location) { |
203 |
let option = $( |
195 |
let option = $( |
204 |
'<option value="' + |
196 |
'<option value="' + |
205 |
pickup_location.library_id + |
197 |
pickup_location.library_id + |
206 |
'">' + |
198 |
'">' + |
207 |
pickup_location.name + |
199 |
pickup_location.name + |
208 |
"</option>" |
200 |
"</option>" |
209 |
); |
201 |
); |
210 |
|
202 |
|
211 |
option.attr( |
203 |
option.attr("data-needs_override", pickup_location.needs_override); |
212 |
"data-needs_override", |
204 |
option.attr( |
213 |
pickup_location.needs_override |
205 |
"data-pickup_items", |
214 |
); |
206 |
pickup_location.pickup_items.join(",") |
215 |
option.attr( |
207 |
); |
216 |
"data-pickup_items", |
|
|
217 |
pickup_location.pickup_items.join(",") |
218 |
); |
219 |
|
208 |
|
220 |
$pickupSelect.append(option); |
209 |
$pickupSelect.append(option); |
221 |
} |
|
|
222 |
}); |
210 |
}); |
223 |
|
211 |
|
224 |
$pickupSelect.prop("disabled", false); |
212 |
$pickupSelect.prop("disabled", false); |
225 |
|
213 |
|
226 |
// If pickup_library already exists, pre-select |
214 |
// If filtered_pickup_locations contain the booking_patron's home library, use it as the default for pickup_library_id |
|
|
215 |
pickup_library_id ??= filtered_pickup_locations.find( |
216 |
pickup_location => |
217 |
pickup_location.library_id === |
218 |
booking_patron?.library.library_id |
219 |
)?.library_id; |
220 |
|
227 |
if (pickup_library_id) { |
221 |
if (pickup_library_id) { |
228 |
$pickupSelect.val(pickup_library_id).trigger("change"); |
222 |
$pickupSelect.val(pickup_library_id).trigger("change"); |
229 |
} else { |
223 |
} else { |
Lines 266-277
$("#placeBookingModal").on("show.bs.modal", function (e) {
Link Here
|
266 |
}, |
260 |
}, |
267 |
success: function (response) { |
261 |
success: function (response) { |
268 |
if (dataFetched === true) { |
262 |
if (dataFetched === true) { |
269 |
setLocationsPicker(response); |
263 |
setLocationsPicker(response, booking_patron); |
270 |
} else { |
264 |
} else { |
271 |
var interval = setInterval(function () { |
265 |
var interval = setInterval(function () { |
272 |
if (dataFetched === true) { |
266 |
if (dataFetched === true) { |
273 |
// Data is fetched, execute the callback and stop the interval |
267 |
// Data is fetched, execute the callback and stop the interval |
274 |
setLocationsPicker(response); |
268 |
setLocationsPicker(response, booking_patron); |
275 |
clearInterval(interval); |
269 |
clearInterval(interval); |
276 |
} |
270 |
} |
277 |
}, 100); |
271 |
}, 100); |
Lines 646-651
$("#placeBookingModal").on("show.bs.modal", function (e) {
Link Here
|
646 |
}); |
640 |
}); |
647 |
$("#pickup_library_id").trigger("change.select2"); |
641 |
$("#pickup_library_id").trigger("change.select2"); |
648 |
|
642 |
|
|
|
643 |
// Set item's home library as default if patron's home library hasn't already populated pickup_library_id |
644 |
let pickup_library_id = $("#pickup_library_id").val(); |
645 |
const booking_patron_id = $("#booking_patron_id").val(); |
646 |
if (!pickup_library_id && booking_patron_id) { |
647 |
$.ajax({ |
648 |
url: |
649 |
"/api/v1/patrons?patron_id=" + |
650 |
booking_patron_id, |
651 |
dataType: "json", |
652 |
type: "GET", |
653 |
}).then(response => { |
654 |
const [booking_patron] = response; |
655 |
let is_home; |
656 |
pickup_library_id = bookable_items.find( |
657 |
({ home_library_id, holding_library_id }) => { |
658 |
is_home = |
659 |
holding_library_id === home_library_id; |
660 |
if (is_home) { |
661 |
return ( |
662 |
home_library_id === |
663 |
booking_patron.library_id |
664 |
); |
665 |
} |
666 |
|
667 |
return ( |
668 |
holding_library_id === |
669 |
booking_patron.library_id |
670 |
); |
671 |
} |
672 |
)?.[ |
673 |
is_home |
674 |
? "home_library_id" |
675 |
: "holding_library_id" |
676 |
]; |
677 |
if (!pickup_library_id) { |
678 |
return; |
679 |
} |
680 |
|
681 |
$("#pickup_library_id") |
682 |
.val(pickup_library_id) |
683 |
.trigger("change.select2"); |
684 |
}); |
685 |
} |
686 |
|
649 |
// Disable patron selection change |
687 |
// Disable patron selection change |
650 |
$("#booking_patron_id").prop("disabled", true); |
688 |
$("#booking_patron_id").prop("disabled", true); |
651 |
|
689 |
|
652 |
- |
|
|