|
Line 0
Link Here
|
|
|
1 |
(() => { |
| 2 |
document |
| 3 |
.getElementById("quotaModal") |
| 4 |
?.addEventListener("show.bs.modal", handleShowQuotaModal); |
| 5 |
document |
| 6 |
.getElementById("quotaForm") |
| 7 |
?.addEventListener("submit", handleQuotaSubmit); |
| 8 |
document |
| 9 |
.getElementById("deleteQuotaModal") |
| 10 |
?.addEventListener("show.bs.modal", handleShowDeleteModal); |
| 11 |
document |
| 12 |
.getElementById("deleteForm") |
| 13 |
?.addEventListener("submit", handleDeleteSubmit); |
| 14 |
|
| 15 |
async function handleQuotaSubmit(e) { |
| 16 |
e.preventDefault(); |
| 17 |
|
| 18 |
const target = e.target; |
| 19 |
if (!(target instanceof HTMLFormElement)) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
const formData = new FormData(target); |
| 24 |
const quotaId = formData.get("quota_id"); |
| 25 |
const patronId = formData.get("patron_id"); |
| 26 |
const description = formData.get("description"); |
| 27 |
const startDate = formData.get("start_date"); |
| 28 |
const endDate = formData.get("end_date"); |
| 29 |
const allocation = formData.get("allocation"); |
| 30 |
|
| 31 |
const quotaUrl = quotaId ? `/api/v1/patrons/${patronId}/quotas/${quotaId}` : `/api/v1/patrons/${patronId}/quotas`; |
| 32 |
let [error, response] = await catchError( |
| 33 |
fetch(quotaUrl, { |
| 34 |
method: quotaId ? "PUT" : "POST", |
| 35 |
body: JSON.stringify({ |
| 36 |
patron_id: patronId, |
| 37 |
description: description, |
| 38 |
start_date: startDate, |
| 39 |
end_date: endDate, |
| 40 |
allocation: allocation |
| 41 |
}), |
| 42 |
headers: { |
| 43 |
"Content-Type": "application/json", |
| 44 |
}, |
| 45 |
}) |
| 46 |
); |
| 47 |
if (error || !response.ok) { |
| 48 |
const alertContainer = document.getElementById( |
| 49 |
"quota_result" |
| 50 |
); |
| 51 |
alertContainer.outerHTML = ` |
| 52 |
<div id="quota_result" class="alert alert-danger"> |
| 53 |
${__("Failure")} |
| 54 |
</div> |
| 55 |
`; |
| 56 |
|
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
quota_success = true; |
| 61 |
quota_table?.api().ajax.reload(); |
| 62 |
|
| 63 |
$("#quotaModal").modal("hide"); |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
function handleShowQuotaModal(e) { |
| 68 |
const button = e.relatedTarget; |
| 69 |
if (!button) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
const quotaModalLabel = document.getElementById("quotaLabel"); |
| 74 |
const quotaModalSubmit = document.getElementById("quotaFormSubmit"); |
| 75 |
|
| 76 |
const quotaIdInput = document.getElementById("quota_id"); |
| 77 |
const quotaPatronInput = document.getElementById("patron_id"); |
| 78 |
const quotaDescriptionInput = document.getElementById("quota_description"); |
| 79 |
const quotaStartDateInput = document.getElementById("quota_from"); |
| 80 |
const quotaEndDateInput = document.getElementById("quota_to"); |
| 81 |
const quotaAllocationInput = document.getElementById("quota_allocation"); |
| 82 |
|
| 83 |
const quota = button.dataset.quota; |
| 84 |
if (quota) { |
| 85 |
quotaIdInput.value = quota; |
| 86 |
quotaModalLabel.textContent = _("Edit quota"); |
| 87 |
quotaModalSubmit.innerHTML = "<i class=\"fa fa-check\"></i> " + _("Update"); |
| 88 |
quotaDescriptionInput.value = button.dataset.description; |
| 89 |
quotaStartDateInput._flatpickr.setDate(button.dataset.start_date, 1); |
| 90 |
quotaEndDateInput._flatpickr.setDate(button.dataset.end_date, 1); |
| 91 |
quotaAllocationInput.value = button.dataset.allocation; |
| 92 |
} else { |
| 93 |
quotaIdInput.value = null; |
| 94 |
quotaModalLabel.textContent = _("Add quota"); |
| 95 |
quotaModalSubmit.innerHTML = "<i class=\"fa fa-fw fa-plus\"></i> " + _("Add"); |
| 96 |
quotaDescriptionInput.value = null; |
| 97 |
quotaStartDateInput.value = null; |
| 98 |
quotaEndDateInput.value = null; |
| 99 |
quotaAllocationInput.value = null; |
| 100 |
} |
| 101 |
|
| 102 |
const patron = button.dataset.patron; |
| 103 |
quotaPatronInput.value = patron; |
| 104 |
|
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
async function handleDeleteSubmit(e) { |
| 109 |
e.preventDefault(); |
| 110 |
|
| 111 |
const target = e.target; |
| 112 |
if (!(target instanceof HTMLFormElement)) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
const formData = new FormData(target); |
| 117 |
const quotaId = formData.get("quota_id"); |
| 118 |
const patronId = formData.get("patron_id"); |
| 119 |
|
| 120 |
const quotaUrl = `/api/v1/patrons/${patronId}/quotas/${quotaId}`; |
| 121 |
let [error, response] = await catchError( |
| 122 |
fetch(quotaUrl, { |
| 123 |
method: "DELETE", |
| 124 |
headers: { |
| 125 |
"Content-Type": "application/json", |
| 126 |
}, |
| 127 |
}) |
| 128 |
); |
| 129 |
if (error || !response.ok) { |
| 130 |
const alertContainer = document.getElementById( |
| 131 |
"quota_result" |
| 132 |
); |
| 133 |
alertContainer.outerHTML = ` |
| 134 |
<div id="quota_result" class="alert alert-danger"> |
| 135 |
${__("Failure")} |
| 136 |
</div> |
| 137 |
`; |
| 138 |
|
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
quota_table?.api().ajax.reload(); |
| 143 |
|
| 144 |
$("#deleteQuotaModal").modal("hide"); |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
function handleShowDeleteModal(e) { |
| 149 |
const button = e.relatedTarget; |
| 150 |
if (!button) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
const quotaIdInput = document.getElementById("quota_id_delete"); |
| 155 |
const quotaPatronInput = document.getElementById("patron_id_delete"); |
| 156 |
|
| 157 |
const quota = button.dataset.quota; |
| 158 |
quotaIdInput.value = quota; |
| 159 |
const patron = button.dataset.patron; |
| 160 |
quotaPatronInput.value = patron; |
| 161 |
|
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
function catchError(promise) { |
| 166 |
return promise.then(data => [undefined, data]).catch(error => [error]); |
| 167 |
} |
| 168 |
})(); |