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 // Get the target from the "data-target" attribute
if ('lineid' in el.dataset) { if ('lineid' in el.dataset) {
const target = el.dataset.lineid; const target = el.dataset.lineid;
const group = el.dataset.groupid;
const tableLine = document.getElementById(`tableline-${target}`); const tableLine = document.getElementById(`tableline-${target}`);
const budgetAmount = document.getElementById(`budget-amount-${group}`);
if (confirm("Are you sure to delete this item ?")) { if (confirm("Are you sure to delete this item ?")) {
// Get csrf token // Get csrf token
const cookieValue = document.cookie const cookieValue = document.cookie
@ -16,6 +18,7 @@ document.addEventListener('DOMContentLoaded', () => {
.find((row) => row.startsWith('csrftoken=')) .find((row) => row.startsWith('csrftoken='))
?.split('=')[1]; ?.split('=')[1];
const url = `/api/budget/budgetline/${target}/`; const url = `/api/budget/budgetline/${target}/`;
tableLine.classList.add("tr-disabled");
await fetch(url, { await fetch(url, {
method: 'DELETE', method: 'DELETE',
headers: { headers: {
@ -24,8 +27,16 @@ document.addEventListener('DOMContentLoaded', () => {
}).then((resp) => { }).then((resp) => {
if(resp.ok) { if(resp.ok) {
tableLine.remove(); 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)); }).catch((e) => console.log(e));
} }
} }

View file

@ -42,6 +42,16 @@
</div> </div>
<div class="navbar-end"> <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="navbar-item">
<div class="buttons"> <div class="buttons">
{% if user.is_authenticated %} {% if user.is_authenticated %}

View file

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