87 lines
2.0 KiB
JavaScript
87 lines
2.0 KiB
JavaScript
/**
|
|
* Page comptes (admin uniquement) : liste et création.
|
|
*/
|
|
|
|
async function chargerComptes()
|
|
{
|
|
const comptes = await appelerApi("/api/utilisateurs");
|
|
|
|
if (!comptes)
|
|
{
|
|
return;
|
|
}
|
|
|
|
const tbody = document.getElementById("listeComptes");
|
|
tbody.innerHTML = comptes.map(function (compte)
|
|
{
|
|
return (
|
|
"<tr>" +
|
|
"<td>" + compte.login + "</td>" +
|
|
"<td>" + compte.profil + "</td>" +
|
|
"</tr>"
|
|
);
|
|
}).join("");
|
|
}
|
|
|
|
document.getElementById("boutonNouveauCompte").addEventListener("click", function ()
|
|
{
|
|
document.getElementById("formCompte").reset();
|
|
document.getElementById("erreurCompte").hidden = true;
|
|
document.getElementById("dialogueCompte").showModal();
|
|
});
|
|
|
|
document.getElementById("annulerCompte").addEventListener("click", function ()
|
|
{
|
|
document.getElementById("dialogueCompte").close();
|
|
});
|
|
|
|
document.getElementById("formCompte").addEventListener("submit", async function (event)
|
|
{
|
|
event.preventDefault();
|
|
|
|
const erreurCompte = document.getElementById("erreurCompte");
|
|
erreurCompte.hidden = true;
|
|
|
|
try
|
|
{
|
|
await appelerApi("/api/utilisateurs", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
login: document.getElementById("login").value.trim(),
|
|
motDePasse: document.getElementById("motDePasse").value,
|
|
profil: document.getElementById("profil").value
|
|
})
|
|
});
|
|
|
|
document.getElementById("dialogueCompte").close();
|
|
await chargerComptes();
|
|
}
|
|
catch (erreur)
|
|
{
|
|
erreurCompte.textContent = erreur.message;
|
|
erreurCompte.hidden = false;
|
|
}
|
|
});
|
|
|
|
async function demarrer()
|
|
{
|
|
const utilisateur = await verifierSession();
|
|
|
|
if (!utilisateur)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (utilisateur.profil !== "admin")
|
|
{
|
|
window.location.href = "/";
|
|
return;
|
|
}
|
|
|
|
brancherDeconnexion();
|
|
await chargerComptes();
|
|
}
|
|
|
|
demarrer();
|