some ajax

This commit is contained in:
sinavir 2023-02-16 19:07:43 +01:00
parent 3a1885d551
commit 9c0da4c17e

View file

@ -0,0 +1,35 @@
document.addEventListener('DOMContentLoaded', () => {
const $deletebudgetline = Array.prototype.slice.call(document.querySelectorAll('.delete-budgetline'), 0);
// Add a click event on each of them
$deletebudgetline.forEach(el => {
el.addEventListener('click', async () => {
// Get the target from the "data-target" attribute
if ('lineid' in el.dataset) {
const target = el.dataset.lineid;
const tableLine = document.getElementById(`tableline-${target}`);
if (confirm("Are you sure to delete this item ?")) {
// Get csrf token
const cookieValue = document.cookie
.split('; ')
.find((row) => row.startsWith('csrftoken='))
?.split('=')[1];
const url = `/api/budget/budgetline/${target}/`;
await fetch(url, {
method: 'DELETE',
headers: {
'X-CSRFToken': cookieValue
}
}).then((resp) => {
if(resp.ok) {
tableLine.remove();
//TODO: Add a message (code a helper for that) for succes or error
}
}).catch((e) => console.log(e));
}
}
});
});
});