feat : fixed some page not being as described in the figma

This commit is contained in:
2026-04-27 11:21:32 +02:00
parent 56019ad372
commit 733259e317
13 changed files with 1757 additions and 235 deletions
@@ -0,0 +1,292 @@
import { useEffect, useState } from "preact/hooks";
type Enseignement = { idProf: string; idModule: string; idPromo: string };
type Module = { id: string; nom: string };
type Promo = { id: string; annee: string };
export default function AdminEnseignements() {
const [enseignements, setEnseignements] = useState<Enseignement[]>([]);
const [modules, setModules] = useState<Module[]>([]);
const [promos, setPromos] = useState<Promo[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
// Filters
const [filterPromo, setFilterPromo] = useState("");
const [filterModule, setFilterModule] = useState("");
const [filterEnseignant, setFilterEnseignant] = useState("");
// Add form
const [showAdd, setShowAdd] = useState(false);
const [addPromo, setAddPromo] = useState("");
const [addModule, setAddModule] = useState("");
const [addProf, setAddProf] = useState("");
const [adding, setAdding] = useState(false);
const [addError, setAddError] = useState<string | null>(null);
async function load() {
try {
const [eRes, mRes, pRes] = await Promise.all([
fetch("/admin/api/enseignements"),
fetch("/admin/api/modules"),
fetch("/students/api/promotions"),
]);
if (!eRes.ok) throw new Error("Impossible de charger les enseignements");
setEnseignements(await eRes.json());
if (mRes.ok) setModules(await mRes.json());
if (pRes.ok) setPromos(await pRes.json());
} catch (e) {
setError(e instanceof Error ? e.message : "Erreur");
} finally {
setLoading(false);
}
}
useEffect(() => {
load();
}, []);
async function deleteEnseignement(
idProf: string,
idModule: string,
idPromo: string,
) {
if (
!confirm(
`Supprimer l'assignation ${idProf}${idModule} / ${idPromo} ?`,
)
) return;
try {
const res = await fetch(
`/admin/api/enseignements/${encodeURIComponent(idProf)}/${
encodeURIComponent(idModule)
}/${encodeURIComponent(idPromo)}`,
{ method: "DELETE" },
);
if (!res.ok) throw new Error("Suppression échouée");
await load();
} catch (e) {
setError(e instanceof Error ? e.message : "Erreur");
}
}
async function addEnseignement() {
if (!addProf.trim() || !addModule || !addPromo) {
setAddError("Tous les champs sont requis");
return;
}
setAdding(true);
setAddError(null);
try {
const res = await fetch("/admin/api/enseignements", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
idProf: addProf.trim(),
idModule: addModule,
idPromo: addPromo,
}),
});
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.error ?? "Création échouée");
}
setAddProf("");
setAddModule("");
setAddPromo("");
setShowAdd(false);
await load();
} catch (e) {
setAddError(e instanceof Error ? e.message : "Erreur");
} finally {
setAdding(false);
}
}
const moduleMap = Object.fromEntries(modules.map((m) => [m.id, m]));
const filtered = enseignements.filter((e) => {
const matchPromo = !filterPromo || e.idPromo === filterPromo;
const matchModule = !filterModule || e.idModule === filterModule;
const matchEns = !filterEnseignant ||
e.idProf.toLowerCase().includes(filterEnseignant.toLowerCase());
return matchPromo && matchModule && matchEns;
});
return (
<div class="page-content">
<h2 class="page-title">Assignations Enseignant Module / Promo</h2>
{error && <p class="state-error">{error}</p>}
<div class="filters">
<select
class="filter-select"
value={filterPromo}
onChange={(e) =>
setFilterPromo((e.target as HTMLSelectElement).value)}
>
<option value="">Promo </option>
{promos.map((p) => <option key={p.id} value={p.id}>{p.id}</option>)}
</select>
<select
class="filter-select"
value={filterModule}
onChange={(e) =>
setFilterModule((e.target as HTMLSelectElement).value)}
>
<option value="">Module </option>
{modules.map((m) => (
<option key={m.id} value={m.id}>{m.id} {m.nom}</option>
))}
</select>
<input
class="filter-input"
placeholder="Enseignant ▾"
value={filterEnseignant}
onInput={(e) =>
setFilterEnseignant((e.target as HTMLInputElement).value)}
/>
<button
type="button"
class="btn btn-secondary"
onClick={() => {
setFilterPromo("");
setFilterModule("");
setFilterEnseignant("");
}}
>
Filtrer
</button>
<button
type="button"
class="btn btn-primary"
onClick={() => setShowAdd((v) => !v)}
style="margin-left: auto"
>
+ Assigner
</button>
</div>
{showAdd && (
<div class="form-row" style="margin-bottom: 1.25rem">
{addError && (
<span class="state-error" style="padding: 0.3rem 0.5rem">
{addError}
</span>
)}
<select
class="filter-select"
value={addPromo}
onChange={(e) => setAddPromo((e.target as HTMLSelectElement).value)}
style="min-width: 10rem"
>
<option value="">Promo</option>
{promos.map((p) => <option key={p.id} value={p.id}>{p.id}</option>)}
</select>
<select
class="filter-select"
value={addModule}
onChange={(e) =>
setAddModule((e.target as HTMLSelectElement).value)}
style="min-width: 14rem"
>
<option value="">Module</option>
{modules.map((m) => (
<option key={m.id} value={m.id}>{m.id} {m.nom}</option>
))}
</select>
<input
class="form-input"
placeholder="User ID enseignant…"
value={addProf}
onInput={(e) => setAddProf((e.target as HTMLInputElement).value)}
style="min-width: 10rem"
/>
<button
type="button"
class="btn btn-primary"
onClick={addEnseignement}
disabled={adding}
>
{adding ? "…" : "Créer"}
</button>
<button
type="button"
class="btn btn-secondary"
onClick={() => setShowAdd(false)}
>
Annuler
</button>
</div>
)}
{loading
? <p class="state-loading">Chargement</p>
: (
<div class="data-table-wrap">
<table class="data-table">
<thead>
<tr>
<th>Promo</th>
<th>Module</th>
<th>Enseignant (User.id)</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{filtered.length === 0
? (
<tr>
<td colspan={4} class="state-empty">
Aucun enseignement trouvé
</td>
</tr>
)
: filtered.map((e) => {
const mod = moduleMap[e.idModule];
return (
<tr key={`${e.idProf}-${e.idModule}-${e.idPromo}`}>
<td>
<span class="promo-chip">{e.idPromo}</span>
</td>
<td class="col-promo">
{mod ? `${mod.id} ${mod.nom}` : e.idModule}
</td>
<td>{e.idProf}</td>
<td>
<div class="col-actions">
<button
type="button"
class="btn btn-sm btn-danger"
onClick={() =>
deleteEnseignement(
e.idProf,
e.idModule,
e.idPromo,
)}
>
🗑
</button>
</div>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
)}
<div class="info-note">
<p>
Un même module peut être enseigné par plusieurs utilisateurs sur une
même promo.
</p>
<p class="info-note-dim">
Clé composite = idProf (User.Id) + idModule + idPromo
</p>
</div>
</div>
);
}
@@ -0,0 +1,107 @@
import { useEffect, useState } from "preact/hooks";
type Perm = { id: string; nom: string };
type Role = { id: number; nom: string; permissions: string[] };
export default function AdminPermissions() {
const [permissions, setPermissions] = useState<Perm[]>([]);
const [roles, setRoles] = useState<Role[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
async function load() {
try {
const [pRes, rRes] = await Promise.all([
fetch("/admin/api/permissions"),
fetch("/admin/api/roles"),
]);
if (!pRes.ok) throw new Error("Impossible de charger les permissions");
setPermissions(await pRes.json());
if (rRes.ok) setRoles(await rRes.json());
} catch (e) {
setError(e instanceof Error ? e.message : "Erreur");
} finally {
setLoading(false);
}
}
load();
}, []);
function rolesForPerm(permId: string): Role[] {
return roles.filter((r) => r.permissions.includes(permId));
}
const MAX_ROLE_CHIPS = 2;
return (
<div class="page-content">
<h2 class="page-title">Permissions</h2>
<div class="info-note" style="margin-top: 0; margin-bottom: 1.25rem">
<p>
Les permissions sont définies statiquement par le serveur.
</p>
<p class="info-note-dim">
Elles ne peuvent pas être créées ou supprimées via l'API.
</p>
</div>
{error && <p class="state-error">{error}</p>}
{loading
? <p class="state-loading">Chargement</p>
: (
<div class="data-table-wrap">
<table class="data-table">
<thead>
<tr>
<th>idPermission</th>
<th>nomPermission</th>
<th>Rôles associés</th>
</tr>
</thead>
<tbody>
{permissions.map((p) => {
const associated = rolesForPerm(p.id);
const shown = associated.slice(0, MAX_ROLE_CHIPS);
const overflow = associated.length - MAX_ROLE_CHIPS;
return (
<tr key={p.id}>
<td>
<span
class="col-promo"
style="font-family: monospace"
>
{p.id}
</span>
</td>
<td>{p.nom}</td>
<td>
<div style="display: flex; flex-wrap: wrap; align-items: center; gap: 0.1rem">
{shown.map((r) => (
<span key={r.id} class="role-chip">{r.nom}</span>
))}
{overflow > 0 && (
<span
class="col-dim"
style="font-size: 0.72rem; margin-left: 0.2rem"
>
+{overflow}
</span>
)}
{associated.length === 0 && (
<span class="col-dim"></span>
)}
</div>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
)}
</div>
);
}
+186 -115
View File
@@ -1,17 +1,23 @@
import { useEffect, useState } from "preact/hooks";
type Role = { id: number; nom: string };
type Permission = { id: string; nom: string };
type Role = { id: number; nom: string; permissions: string[] };
type Perm = { id: string; nom: string };
const MAX_CHIPS = 3;
export default function AdminRoles() {
const [roles, setRoles] = useState<Role[]>([]);
const [permissions, setPermissions] = useState<Permission[]>([]);
const [permissions, setPermissions] = useState<Perm[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [newNom, setNewNom] = useState("");
const [creating, setCreating] = useState(false);
const [editId, setEditId] = useState<number | null>(null);
const [editNom, setEditNom] = useState("");
// Manage-perms sub-view
const [managingRole, setManagingRole] = useState<Role | null>(null);
const [editPerms, setEditPerms] = useState<Set<string>>(new Set());
const [saving, setSaving] = useState(false);
const [saveError, setSaveError] = useState<string | null>(null);
async function load() {
try {
@@ -55,21 +61,6 @@ export default function AdminRoles() {
}
}
async function saveEdit(id: number) {
try {
const res = await fetch(`/admin/api/roles/${id}`, {
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify({ nom: editNom.trim() }),
});
if (!res.ok) throw new Error("Modification échouée");
setEditId(null);
await load();
} catch (e) {
setError(e instanceof Error ? e.message : "Erreur");
}
}
async function deleteRole(id: number) {
if (!confirm("Supprimer ce rôle ?")) return;
try {
@@ -81,19 +72,143 @@ export default function AdminRoles() {
}
}
function openManage(role: Role) {
setManagingRole(role);
setEditPerms(new Set(role.permissions));
setSaveError(null);
}
function togglePerm(permId: string) {
setEditPerms((prev) => {
const next = new Set(prev);
if (next.has(permId)) next.delete(permId);
else next.add(permId);
return next;
});
}
async function savePerms() {
if (!managingRole) return;
setSaving(true);
setSaveError(null);
try {
const res = await fetch(`/admin/api/roles/${managingRole.id}`, {
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify({
nom: managingRole.nom,
permissions: [...editPerms],
}),
});
if (!res.ok) throw new Error("Enregistrement échoué");
await load();
setManagingRole(null);
} catch (e) {
setSaveError(e instanceof Error ? e.message : "Erreur");
} finally {
setSaving(false);
}
}
// ---- Manage-perms view ----
if (managingRole) {
const activeCount = editPerms.size;
return (
<div class="page-content">
<a
class="back-link"
href="#"
onClick={(e) => {
e.preventDefault();
setManagingRole(null);
}}
>
Retour à la liste des rôles
</a>
<h2 class="page-title">
Permissions du rôle {managingRole.nom}
</h2>
{saveError && <p class="state-error">{saveError}</p>}
<div
class="toolbar"
style="margin-bottom: 1.25rem; align-items: center"
>
<div style="display: flex; align-items: center; gap: 0.6rem">
<span class="numEtud-chip">idRole : {managingRole.id}</span>
<span style="font-weight: var(--font-weight-bold); font-size: 0.9rem">
{managingRole.nom}
</span>
<span style="font-size: 0.8rem; color: light-dark(var(--light-foreground-dim), var(--dark-foreground-dim))">
{activeCount} permission{activeCount !== 1 ? "s" : ""} active
{activeCount !== 1 ? "s" : ""}
</span>
</div>
<button
type="button"
class="btn btn-primary"
onClick={savePerms}
disabled={saving}
>
{saving ? "…" : "Enregistrer"}
</button>
</div>
<div style="margin-bottom: 0.5rem; display: flex; justify-content: space-between">
<span style="font-size: 0.78rem; font-weight: var(--font-weight-bold)">
Permissions disponibles
</span>
<span style="font-size: 0.72rem; color: light-dark(var(--light-foreground-dim), var(--dark-foreground-dim))">
Activer = inclure dans le rôle
</span>
</div>
<div class="perm-toggle-grid">
{permissions.map((p) => {
const active = editPerms.has(p.id);
return (
<label
key={p.id}
class={`perm-toggle-card${active ? " active" : ""}`}
onClick={() => togglePerm(p.id)}
>
<div class="perm-toggle-label">
<span class="perm-toggle-id">{p.id}</span>
<span class="perm-toggle-nom">{p.nom}</span>
</div>
<label class="toggle-switch">
<input
type="checkbox"
checked={active}
onChange={() => togglePerm(p.id)}
onClick={(e) => e.stopPropagation()}
/>
<span class="toggle-slider" />
</label>
</label>
);
})}
</div>
</div>
);
}
// ---- Main list view ----
return (
<div class="page-content">
<h2 class="page-title">Gestion des Rôles</h2>
{error && <p class="state-error">{error}</p>}
<div class="form-row">
<div class="toolbar">
<input
class="form-input"
placeholder="Nom du rôle"
placeholder="Nom du rôle"
value={newNom}
onInput={(e) => setNewNom((e.target as HTMLInputElement).value)}
onKeyDown={(e) => e.key === "Enter" && createRole()}
style="min-width: 14rem"
/>
<button
type="button"
@@ -101,7 +216,7 @@ export default function AdminRoles() {
onClick={createRole}
disabled={creating}
>
+ Ajouter
+ Créer rôle
</button>
</div>
@@ -112,8 +227,9 @@ export default function AdminRoles() {
<table class="data-table">
<thead>
<tr>
<th>ID</th>
<th>Nom</th>
<th>idRole</th>
<th>Nom du rôle</th>
<th>Permissions</th>
<th>Actions</th>
</tr>
</thead>
@@ -121,105 +237,60 @@ export default function AdminRoles() {
{roles.length === 0
? (
<tr>
<td colspan={3} class="state-empty">
<td colspan={4} class="state-empty">
Aucun rôle enregistré
</td>
</tr>
)
: roles.map((r) => (
<tr key={r.id}>
<td class="col-dim">{r.id}</td>
<td>
{editId === r.id
? (
<input
class="form-input"
value={editNom}
onInput={(e) =>
setEditNom(
(e.target as HTMLInputElement).value,
)}
style="min-width: 0; width: 100%"
/>
)
: r.nom}
</td>
<td>
<div class="col-actions">
{editId === r.id
? (
<>
<button
type="button"
class="btn btn-sm btn-primary"
onClick={() => saveEdit(r.id)}
>
</button>
<button
type="button"
class="btn btn-sm btn-secondary"
onClick={() => setEditId(null)}
>
</button>
</>
)
: (
<>
<button
type="button"
class="btn btn-sm btn-secondary"
onClick={() => {
setEditId(r.id);
setEditNom(r.nom);
}}
>
</button>
<button
type="button"
class="btn btn-sm btn-danger"
onClick={() => deleteRole(r.id)}
>
🗑
</button>
</>
: roles.map((r) => {
const shown = r.permissions.slice(0, MAX_CHIPS);
const overflow = r.permissions.length - MAX_CHIPS;
return (
<tr key={r.id}>
<td class="col-dim">{r.id}</td>
<td style="font-weight: var(--font-weight-bold)">
{r.nom}
</td>
<td>
<div style="display: flex; flex-wrap: wrap; align-items: center; gap: 0.1rem">
{shown.map((p) => (
<span key={p} class="perm-chip">{p}</span>
))}
{overflow > 0 && (
<span
class="col-dim"
style="font-size: 0.72rem; margin-left: 0.2rem"
>
+{overflow}
</span>
)}
</div>
</td>
</tr>
))}
</div>
</td>
<td>
<div class="col-actions">
<button
type="button"
class="btn btn-sm btn-secondary"
onClick={() => openManage(r)}
>
Gérer perms
</button>
<button
type="button"
class="btn btn-sm btn-danger"
onClick={() => deleteRole(r.id)}
>
🗑
</button>
</div>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
)}
{permissions.length > 0 && (
<div style="margin-top: 2rem">
<h3 style="font-size: 0.9rem; font-weight: var(--font-weight-bold); margin-bottom: 0.75rem; color: light-dark(var(--light-foreground-dim), var(--dark-foreground-dim))">
Permissions disponibles
</h3>
<div class="data-table-wrap">
<table class="data-table">
<thead>
<tr>
<th>ID</th>
<th>Nom</th>
</tr>
</thead>
<tbody>
{permissions.map((p) => (
<tr key={p.id}>
<td class="col-dim">{p.id}</td>
<td>{p.nom}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
)}
</div>
);
}
+4 -2
View File
@@ -5,11 +5,13 @@ const properties: AppProperties = {
icon: "school",
pages: {
index: "Accueil",
modules: "Modules",
users: "Utilisateurs",
roles: "Rôles",
permissions: "Permissions",
modules: "Modules",
enseignements: "Enseignements",
},
adminOnly: ["modules", "users", "roles"],
adminOnly: ["users", "roles", "permissions", "modules", "enseignements"],
hint: "PolyMPR module",
};
+16
View File
@@ -17,6 +17,22 @@ const CONFLICT = new Response(
);
export const handler: Handlers<null, AuthenticatedState> = {
// GET /enseignements
async GET(
_request: Request,
context: FreshContext<AuthenticatedState>,
): Promise<Response> {
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
return new Response(JSON.stringify([]), {
headers: { "content-type": "application/json" },
});
}
const rows = await db.select().from(enseignements);
return new Response(JSON.stringify(rows), {
headers: { "content-type": "application/json" },
});
},
// #29 POST /enseignements
async POST(
request: Request,
@@ -0,0 +1,18 @@
import {
getPartialsConfig,
makePartials,
} from "$root/defaults/makePartials.tsx";
import { FreshContext } from "$fresh/server.ts";
import { State } from "$root/defaults/interfaces.ts";
import AdminEnseignements from "../(_islands)/AdminEnseignements.tsx";
// deno-lint-ignore require-await
async function Enseignements(
_request: Request,
_context: FreshContext<State>,
) {
return <AdminEnseignements />;
}
export const config = getPartialsConfig();
export default makePartials(Enseignements);
@@ -0,0 +1,18 @@
import {
getPartialsConfig,
makePartials,
} from "$root/defaults/makePartials.tsx";
import { FreshContext } from "$fresh/server.ts";
import { State } from "$root/defaults/interfaces.ts";
import AdminPermissions from "../(_islands)/AdminPermissions.tsx";
// deno-lint-ignore require-await
async function Permissions(
_request: Request,
_context: FreshContext<State>,
) {
return <AdminPermissions />;
}
export const config = getPartialsConfig();
export default makePartials(Permissions);