forked from fahrPool/fahrPool
92 lines
2.6 KiB
TypeScript
92 lines
2.6 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useTransition } from "react";
|
|
import { setzePasswort } from "@/app/nutzerverwaltung/actions";
|
|
import { MIN_PASSWORT_LAENGE } from "@/lib/auth/passwort";
|
|
import { Alert } from "@/components/ui/Alert";
|
|
import { Button } from "@/components/ui/Button";
|
|
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
|
|
|
|
export function PasswortSetzen({ nutzerId, istEigenerAccount }: { nutzerId: string; istEigenerAccount: boolean }) {
|
|
const [offen, setOffen] = useState(false);
|
|
const [fehler, setFehler] = useState<string | null>(null);
|
|
const [gesetzt, setGesetzt] = useState(false);
|
|
const [isPending, startTransition] = useTransition();
|
|
|
|
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
|
event.preventDefault();
|
|
const formData = new FormData(event.currentTarget);
|
|
startTransition(async () => {
|
|
const ergebnis = await setzePasswort(nutzerId, formData);
|
|
if (!ergebnis.success) {
|
|
setFehler(ergebnis.error);
|
|
return;
|
|
}
|
|
setFehler(null);
|
|
setGesetzt(true);
|
|
setOffen(false);
|
|
});
|
|
}
|
|
|
|
if (!offen) {
|
|
return (
|
|
<div className="flex flex-col gap-2">
|
|
<div>
|
|
<Button
|
|
type="button"
|
|
variant="secondary"
|
|
fullWidth={false}
|
|
disabled={istEigenerAccount}
|
|
onClick={() => {
|
|
setGesetzt(false);
|
|
setFehler(null);
|
|
setOffen(true);
|
|
}}
|
|
>
|
|
Passwort setzen
|
|
</Button>
|
|
</div>
|
|
{gesetzt && (
|
|
<p role="status" className="text-status-success">
|
|
Passwort gesetzt. Der Nutzer muss es beim nächsten Login ändern.
|
|
</p>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit} className="flex flex-col gap-3">
|
|
{fehler && <Alert>{fehler}</Alert>}
|
|
<FormField label="Neues Passwort" htmlFor={`passwort-${nutzerId}`}>
|
|
<input
|
|
id={`passwort-${nutzerId}`}
|
|
name="passwort"
|
|
type="password"
|
|
required
|
|
minLength={MIN_PASSWORT_LAENGE}
|
|
autoComplete="new-password"
|
|
className={FIELD_CLASSES}
|
|
/>
|
|
</FormField>
|
|
<div className="flex flex-wrap items-center gap-3">
|
|
<Button type="submit" fullWidth={false} disabled={isPending}>
|
|
Passwort speichern
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
variant="secondary"
|
|
fullWidth={false}
|
|
disabled={isPending}
|
|
onClick={() => {
|
|
setOffen(false);
|
|
setFehler(null);
|
|
}}
|
|
>
|
|
Abbrechen
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|