From 9c0da4c17e5faf6ad3cf1a5bc014805693cedd43 Mon Sep 17 00:00:00 2001 From: sinavir Date: Thu, 16 Feb 2023 19:07:43 +0100 Subject: [PATCH] some ajax --- .../frontend/static/js/budget_list.js | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 hackens_orga/frontend/static/js/budget_list.js 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)); + } + } + }); + }); + +});