forked from fahrPool/fahrPool
49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useTransition } from "react";
|
|
import { speichereMeineDaten } from "@/app/profil/actions";
|
|
import { NutzerDetailsFelder } from "@/components/nutzerverwaltung/NutzerDetailsFelder";
|
|
import { Alert } from "@/components/ui/Alert";
|
|
import { Button } from "@/components/ui/Button";
|
|
import type { NutzerDetails } from "@/lib/nutzerverwaltung/details";
|
|
|
|
export function ProfilForm({ details }: { details: NutzerDetails }) {
|
|
const [fehler, setFehler] = useState<string | null>(null);
|
|
const [gespeichert, setGespeichert] = 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 speichereMeineDaten(formData);
|
|
if (!ergebnis.success) {
|
|
setGespeichert(false);
|
|
setFehler(ergebnis.error);
|
|
return;
|
|
}
|
|
setFehler(null);
|
|
setGespeichert(true);
|
|
});
|
|
}
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit}>
|
|
{fehler && (
|
|
<div className="mb-4">
|
|
<Alert>{fehler}</Alert>
|
|
</div>
|
|
)}
|
|
<NutzerDetailsFelder idPrefix="profil" details={details} />
|
|
{gespeichert && (
|
|
<p role="status" className="mb-4 text-status-success">
|
|
Gespeichert.
|
|
</p>
|
|
)}
|
|
<Button type="submit" disabled={isPending}>
|
|
Speichern
|
|
</Button>
|
|
</form>
|
|
);
|
|
}
|