frontend (AJAX, hide button if no permission)

This commit is contained in:
sinavir 2023-03-06 11:55:19 +01:00
parent 4811fbeada
commit 28f65de0c5
3 changed files with 29 additions and 11 deletions

View file

@ -8,7 +8,9 @@ document.addEventListener('DOMContentLoaded', () => {
// Get the target from the "data-target" attribute
if ('lineid' in el.dataset) {
const target = el.dataset.lineid;
const group = el.dataset.groupid;
const tableLine = document.getElementById(`tableline-${target}`);
const budgetAmount = document.getElementById(`budget-amount-${group}`);
if (confirm("Are you sure to delete this item ?")) {
// Get csrf token
const cookieValue = document.cookie
@ -16,6 +18,7 @@ document.addEventListener('DOMContentLoaded', () => {
.find((row) => row.startsWith('csrftoken='))
?.split('=')[1];
const url = `/api/budget/budgetline/${target}/`;
tableLine.classList.add("tr-disabled");
await fetch(url, {
method: 'DELETE',
headers: {
@ -24,8 +27,16 @@ document.addEventListener('DOMContentLoaded', () => {
}).then((resp) => {
if(resp.ok) {
tableLine.remove();
//TODO: Add a message (code a helper for that) for succes or error
const url = `/api/budget/budgetgroup/${group}`;
budgetAmount.innerHTML = "---";
return fetch(url);
} else {
tableLine.classList.remove("tr-disabled");
}
}).then((resp) =>
resp.json()
).then((data) => {
budgetAmount.innerHTML = data.total;
}).catch((e) => console.log(e));
}
}

View file

@ -42,6 +42,16 @@
</div>
<div class="navbar-end">
{% if user.is_authenticated %}
<div class="navbar-item">
<span class="icon-text">
<span class="icon">
<i class="fas fa-user"></i>
</span>
<span>{{ user.username }}</span>
</span>
</div>
{% endif %}
<div class="navbar-item">
<div class="buttons">
{% if user.is_authenticated %}

View file

@ -4,6 +4,7 @@
<section class="section pt-2 content">
<h1 class="title is-3">Budget d'hackens</h1>
{% if perms.budget.add_budgetgroup %}
<div class="buttons">
<a
class="button is-dark"
@ -15,11 +16,13 @@
<span>Nouveau budget</span>
</a>
</div>
{% endif %}
{% for budgetGroup in object_list %}
{% with all_lines=budgetGroup.get_all_lines %}
<div class="level is-mobile">
<div class="level-left">
<h2 class="m-0 title is-4 level-item">
Budget {{ budgetGroup.name }}: {{ budgetGroup.get_total }}
Budget {{ budgetGroup.name }}&nbsp;:&nbsp; <span id="budget-amount-{{ budgetGroup.id }}">{{ all_lines.1 }}</span>
</h2>
</div>
<div class="level-right">
@ -43,7 +46,6 @@
>
<span class="icon"><i class="fa-regular fa-pen-to-square"></i></span>
</a>
<button class="button" disabled><span class="icon"><i class="fa-solid fa-box-archive"></i></span></button>
{% endif %}
</div>
</div>
@ -53,7 +55,7 @@
<strong>Description:</strong> {{ budgetGroup.description.strip }}
</p>
{% endif %}
{% for line in budgetGroup.budgetline_set.all|dictsort:"date" %}
{% for line in all_lines.0|dictsort:"date" %}
{% if forloop.first %}
<table class="table">
<thead>
@ -62,9 +64,6 @@
<th>Libellé</th>
<th>Montant</th>
<th>Ajouté par</th>
{% if perms.budget.can_view_facture %}
<th>Facture</th>
{% endif %}
{% if perms.budget.change_budgetline %}
<th></th>
{% endif %}
@ -77,9 +76,6 @@
<td>{{ line.title }}</td>
<td>{{ line.amount }}€</td>
<td>{{ line.author.name }}</td>
{% if perms.budget.can_view_facture %}
<td>{{ line.facture }}</td>
{% endif %}
{% if perms.budget.change_budgetline %}
<td>
<div class="buttons">
@ -87,7 +83,7 @@
<span class="icon"><i class="fa-solid fa-pen-to-square"></i></span>
</a>
{% if perms.budget.delete_budgetline %}
<button data-lineid="{{ line.id }}" class="delete-budgetline button">
<button data-lineid="{{ line.id }}" data-groupid="{{ budgetGroup.id }}" class="delete-budgetline button">
<span class="icon"><i class="fa-solid fa-trash"></i></span>
</button>
{% endif %}
@ -103,6 +99,7 @@
<p class="has-text-centered">Ce budget est vide</p>
{% endfor %}
<div class="divider"></div>
{% endwith %}
{% endfor %}
</section>