// The admin's screen: the accounts the app knows, with their roles; give a role to any email (even one that never signed in) or take it away. import { api } from "./api.js"; import { esc } from "./math.js"; import { ROLE_NAMES } from "./roles.js"; const GRANTABLE = ["student", "teacher", "manager", "admin"]; let draw; async function showAccounts(message = "", error = "") { const accounts = await api("/api/accounts"); draw(`

תפקידים

מתן תפקיד

אפשר להקליד גם כתובת שעוד לא נכנסה לאפליקציה: התפקיד יחול מהכניסה הראשונה שלה. שינוי תפקיד נקלט תוך כחצי דקה.

${error ? `` : ""}${message ? `

${esc(message)}

` : ""}

${accounts.map(a => ``).join("")}
חשבוןתפקידים שניתנומההגדרות
${esc(a.email)} ${a.roles.map(r => `${ROLE_NAMES[r]} `).join(" ") || '—'} ${a.from_settings.map(r => `${ROLE_NAMES[r]}`).join(" ") || '—'}
`); document.getElementById("give").addEventListener("submit", async event => { event.preventDefault(); const email = document.getElementById("g-email").value.trim(), role = document.getElementById("g-role").value; try { await api(`/api/accounts/${encodeURIComponent(email)}/roles/${role}`, { method: "POST" }); await showAccounts(`${email}: ניתן התפקיד ${ROLE_NAMES[role]}.`); } catch (e) { await showAccounts("", e.message); } }); document.querySelector("#accounts tbody").addEventListener("click", async event => { const role = event.target.closest("[data-take]")?.dataset.take; if (!role) return; const email = event.target.closest("tr").dataset.email; try { await api(`/api/accounts/${encodeURIComponent(email)}/roles/${role}`, { method: "DELETE" }); await showAccounts(`${email}: התפקיד ${ROLE_NAMES[role]} הוסר.`); } catch (e) { await showAccounts("", e.message); } }); } export async function show(me, view) { draw = view; await showAccounts(); }