|
Line 0
Link Here
|
| 0 |
- |
1 |
$(document).ready(function() { |
|
|
2 |
$("#quick_add_user_button").on("click", function () { |
| 3 |
if (!$("#libraries_quick_add").children("option").length) { |
| 4 |
_GETLibraries(); |
| 5 |
} |
| 6 |
if (!$("#categorycode_entry_quick_add").children("optgroup").length) { |
| 7 |
_GETPatronCategories(); |
| 8 |
} |
| 9 |
$("#addQuickAddUserModal").modal("show"); |
| 10 |
$(".dialog.alert").remove(); |
| 11 |
}); |
| 12 |
|
| 13 |
$("#addQuickAddUserModal").on("click", "#addConfirm", function (e) { |
| 14 |
e.preventDefault(); |
| 15 |
if (!$("#quick_add_user_form").get(0).checkValidity()) { |
| 16 |
$("#quick_add_user_form").get(0).reportValidity(); |
| 17 |
} else { |
| 18 |
$("#user-submit-spinner").show(); |
| 19 |
_POSTPatron({ |
| 20 |
surname: $("#surname_quick_add").val(), |
| 21 |
cardnumber: $("#cardnumber_quick_add").val(), |
| 22 |
library_id: $("#libraries_quick_add").val(), |
| 23 |
category_id: $("#categorycode_entry_quick_add").val(), |
| 24 |
}); |
| 25 |
} |
| 26 |
}); |
| 27 |
|
| 28 |
/** |
| 29 |
* Sends a GET request to the /api/v1/patron_categories endpoint to fetch patron categories |
| 30 |
* |
| 31 |
* Upon success, adds the categories to the category dropdown |
| 32 |
*/ |
| 33 |
function _GETPatronCategories() { |
| 34 |
$.ajax({ |
| 35 |
url: "/api/v1/patron_categories", |
| 36 |
type: "GET", |
| 37 |
success: function (data) { |
| 38 |
let groupedCategories = Object.groupBy( |
| 39 |
data, |
| 40 |
({ category_type }) => category_type |
| 41 |
); |
| 42 |
|
| 43 |
// Add <optgroup> |
| 44 |
$.each(groupedCategories, function (category_code, categories) { |
| 45 |
$("#categorycode_entry_quick_add").append( |
| 46 |
$( |
| 47 |
'<optgroup id="' + |
| 48 |
category_code + |
| 49 |
'"label="' + |
| 50 |
_getCategoryTypeName(category_code) + |
| 51 |
'"></optgroup>' |
| 52 |
) |
| 53 |
); |
| 54 |
|
| 55 |
// Add <option> |
| 56 |
$.each(categories, function (i, category) { |
| 57 |
$( |
| 58 |
"#categorycode_entry_quick_add #" + category_code |
| 59 |
).append( |
| 60 |
$("<option></option>") |
| 61 |
.val(category.patron_category_id) |
| 62 |
.html(category.name) |
| 63 |
); |
| 64 |
}); |
| 65 |
}); |
| 66 |
}, |
| 67 |
error: function (data) { |
| 68 |
console.log(data); |
| 69 |
}, |
| 70 |
}); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Sends a GET request to the /api/v1/libraries endpoint to fetch libraries |
| 75 |
* |
| 76 |
* Upon success, adds the libraries to the library dropdown |
| 77 |
*/ |
| 78 |
function _GETLibraries() { |
| 79 |
$.ajax({ |
| 80 |
url: "/api/v1/libraries", |
| 81 |
type: "GET", |
| 82 |
success: function (data) { |
| 83 |
$.each(data, function (val, text) { |
| 84 |
$("#libraries_quick_add").append( |
| 85 |
$("<option></option>").val(text.library_id).html(text.name) |
| 86 |
); |
| 87 |
}); |
| 88 |
}, |
| 89 |
error: function (data) { |
| 90 |
console.log(data); |
| 91 |
}, |
| 92 |
}); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Sends a POST request to the /api/v1/patrons endpoint to add a new patron |
| 97 |
* |
| 98 |
* Upon completion, show a dialog with appropriate message |
| 99 |
* Upon success, add new patron's cardnumber to the cardnumber input |
| 100 |
* |
| 101 |
* @param {Object} params Patron's data to be posted. |
| 102 |
*/ |
| 103 |
function _POSTPatron(params) { |
| 104 |
$.ajax({ |
| 105 |
url: "/api/v1/patrons", |
| 106 |
type: "POST", |
| 107 |
headers: { "Content-Type": "application/json;charset=utf-8" }, |
| 108 |
data: JSON.stringify(params), |
| 109 |
success: function (data) { |
| 110 |
$("#user-submit-spinner").hide(); |
| 111 |
$("#addQuickAddUserModal").modal("hide"); |
| 112 |
$("#surname_quick_add").val(""); |
| 113 |
$("#cardnumber_quick_add").val(""); |
| 114 |
$("#toolbar").before( |
| 115 |
'<div class="dialog message">' + |
| 116 |
__( |
| 117 |
'Patron sucessfully created: </br> <strong><a target="_blank" href="/cgi-bin/koha/members/moremember.pl?borrowernumber=' + |
| 118 |
data.patron_id + |
| 119 |
'">' + |
| 120 |
data.surname + |
| 121 |
"(" + |
| 122 |
data.cardnumber + |
| 123 |
")" |
| 124 |
) + |
| 125 |
"</div>" |
| 126 |
); |
| 127 |
// create_form_cardnumber_input.val(data.cardnumber); |
| 128 |
}, |
| 129 |
error: function (data) { |
| 130 |
console.log(data); |
| 131 |
$("#user-submit-spinner").hide(); |
| 132 |
$("#addQuickAddUserModal").modal("hide"); |
| 133 |
$("#interlibraryloans").before( |
| 134 |
'<div class="dialog alert">' + |
| 135 |
__( |
| 136 |
"There was an error creating the patron: </br> <strong>" + |
| 137 |
(data.responseJSON.error |
| 138 |
? data.responseJSON.error |
| 139 |
: data.responseJSON.errors |
| 140 |
.map((e) => e.path + " " + e.message) |
| 141 |
.join("</br>")) + |
| 142 |
"</strong>" |
| 143 |
) + |
| 144 |
"</div>" |
| 145 |
); |
| 146 |
}, |
| 147 |
}); |
| 148 |
} |
| 149 |
}); |