diff --git a/hackens_orga/frontend/static/js/budget_list.js b/hackens_orga/frontend/static/js/budget_list.js new file mode 100644 index 0000000..6f231f0 --- /dev/null +++ b/hackens_orga/frontend/static/js/budget_list.js @@ -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)); + } + } + }); + }); + +});