forked from fahrPool/fahrPool
feat: add Nutzerverwaltung page
This commit is contained in:
parent
3f78e4f65a
commit
8ed4c86813
|
|
@ -0,0 +1,111 @@
|
|||
import { redirect } from "next/navigation";
|
||||
import { loadProfile } from "@/lib/auth/profile";
|
||||
import { canManageUsers } from "@/lib/auth/roles";
|
||||
import { listNutzer } from "@/lib/nutzerverwaltung/queries";
|
||||
import { erstelleNutzer, aendereRolle, sperreNutzer, entsperreNutzer } from "./actions";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { Alert } from "@/components/ui/Alert";
|
||||
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
|
||||
|
||||
const ROLLEN_OPTIONEN = ["admin", "disponent", "fahrer", "readonly"] as const;
|
||||
|
||||
export default async function NutzerverwaltungPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<{ error?: string }>;
|
||||
}) {
|
||||
const profileResult = await loadProfile();
|
||||
if (profileResult.status !== "ok" || !canManageUsers(profileResult.profile.role)) {
|
||||
redirect(`/auftraege?error=${encodeURIComponent("Nur Admin darf die Nutzerverwaltung nutzen.")}`);
|
||||
}
|
||||
|
||||
const { error } = await searchParams;
|
||||
const nutzerListe = await listNutzer();
|
||||
|
||||
return (
|
||||
<div className="mx-auto max-w-2xl p-4 md:p-8">
|
||||
<h1 className="mb-6 text-xl font-semibold text-asphalt-grey">Nutzerverwaltung</h1>
|
||||
|
||||
{error && (
|
||||
<div className="mb-6">
|
||||
<Alert>{error}</Alert>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<section className="mb-8">
|
||||
<ul className="grid grid-cols-1 gap-3">
|
||||
{nutzerListe.map((nutzer) => {
|
||||
const istEigenerAccount = nutzer.id === profileResult.profile.id;
|
||||
const aendereRolleAction = aendereRolle.bind(null, nutzer.id);
|
||||
const sperrenAction = sperreNutzer.bind(null, nutzer.id);
|
||||
const entsperrenAction = entsperreNutzer.bind(null, nutzer.id);
|
||||
|
||||
return (
|
||||
<li key={nutzer.id}>
|
||||
<Card>
|
||||
<p className="text-asphalt-grey">
|
||||
{nutzer.name} {nutzer.email ? `(${nutzer.email})` : ""}
|
||||
{nutzer.gesperrt && <span className="ml-2 text-status-danger">Gesperrt</span>}
|
||||
</p>
|
||||
<form action={aendereRolleAction} className="mt-2 flex flex-wrap items-center gap-3">
|
||||
<select
|
||||
name="rolle"
|
||||
defaultValue={nutzer.role}
|
||||
disabled={istEigenerAccount}
|
||||
className={FIELD_CLASSES}
|
||||
>
|
||||
{ROLLEN_OPTIONEN.map((rolle) => (
|
||||
<option key={rolle} value={rolle}>
|
||||
{rolle}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<Button type="submit" fullWidth={false} disabled={istEigenerAccount}>
|
||||
Rolle ändern
|
||||
</Button>
|
||||
</form>
|
||||
<form action={nutzer.gesperrt ? entsperrenAction : sperrenAction} className="mt-2">
|
||||
<Button
|
||||
type="submit"
|
||||
fullWidth={false}
|
||||
variant={nutzer.gesperrt ? "secondary" : "destructive"}
|
||||
disabled={istEigenerAccount}
|
||||
>
|
||||
{nutzer.gesperrt ? "Entsperren" : "Sperren"}
|
||||
</Button>
|
||||
</form>
|
||||
</Card>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<Card>
|
||||
<form action={erstelleNutzer}>
|
||||
<h2 className="mb-4 text-base font-semibold text-asphalt-grey">Neuen Nutzer anlegen</h2>
|
||||
<FormField label="Name" htmlFor="name">
|
||||
<input id="name" name="name" type="text" required className={FIELD_CLASSES} />
|
||||
</FormField>
|
||||
<FormField label="E-Mail" htmlFor="email">
|
||||
<input id="email" name="email" type="email" required className={FIELD_CLASSES} />
|
||||
</FormField>
|
||||
<FormField label="Passwort" htmlFor="passwort">
|
||||
<input id="passwort" name="passwort" type="password" required minLength={8} className={FIELD_CLASSES} />
|
||||
</FormField>
|
||||
<FormField label="Rolle" htmlFor="rolle">
|
||||
<select id="rolle" name="rolle" defaultValue="readonly" className={FIELD_CLASSES}>
|
||||
{ROLLEN_OPTIONEN.map((rolle) => (
|
||||
<option key={rolle} value={rolle}>
|
||||
{rolle}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</FormField>
|
||||
<Button type="submit">Nutzer anlegen</Button>
|
||||
</form>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
Reference in New Issue