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