forked from fahrPool/fahrPool
feat: Seite /profil, Nutzer pflegt eigene Details
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
bd3e5b3d4d
commit
2cacd33372
|
|
@ -0,0 +1,30 @@
|
|||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { createClient } from "@/lib/supabase/server";
|
||||
import { loadProfile } from "@/lib/auth/profile";
|
||||
import { validateNutzerDetails, leseDetailsAusFormData } from "@/lib/nutzerverwaltung/details";
|
||||
import { speichereDetails } from "@/lib/nutzerverwaltung/details-queries";
|
||||
|
||||
export type ActionResult = { success: true } | { success: false; error: string };
|
||||
|
||||
export async function speichereMeineDaten(formData: FormData): Promise<ActionResult> {
|
||||
const profileResult = await loadProfile();
|
||||
if (profileResult.status !== "ok") {
|
||||
return { success: false, error: "Bitte neu anmelden." };
|
||||
}
|
||||
|
||||
const validation = validateNutzerDetails(leseDetailsAusFormData(formData));
|
||||
if (!validation.valid) {
|
||||
return { success: false, error: validation.error };
|
||||
}
|
||||
|
||||
const supabase = await createClient();
|
||||
const gespeichert = await speichereDetails(supabase, profileResult.profile.id, validation.details);
|
||||
if (!gespeichert) {
|
||||
return { success: false, error: "Speichern fehlgeschlagen." };
|
||||
}
|
||||
|
||||
revalidatePath("/profil");
|
||||
return { success: true };
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
import { redirect } from "next/navigation";
|
||||
import { loadProfile } from "@/lib/auth/profile";
|
||||
import { ladeDetails } from "@/lib/nutzerverwaltung/details-queries";
|
||||
import { ProfilForm } from "@/components/profil/ProfilForm";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
|
||||
export default async function ProfilPage() {
|
||||
const profileResult = await loadProfile();
|
||||
if (profileResult.status !== "ok") {
|
||||
redirect("/login");
|
||||
}
|
||||
|
||||
const details = await ladeDetails(profileResult.profile.id);
|
||||
|
||||
return (
|
||||
<div className="mx-auto max-w-2xl p-4 md:p-8">
|
||||
<h1 className="mb-1 text-xl font-semibold text-asphalt-grey">Mein Profil</h1>
|
||||
<p className="mb-6 text-asphalt-grey">{profileResult.profile.name}</p>
|
||||
<Card>
|
||||
<ProfilForm details={details} />
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -64,6 +64,12 @@ export function NavLinks({
|
|||
Nutzerverwaltung
|
||||
</Link>
|
||||
)}
|
||||
<Link
|
||||
href="/profil"
|
||||
className="flex min-h-11 items-center text-asphalt-grey hover:text-performance-blue focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-performance-blue"
|
||||
>
|
||||
Mein Profil
|
||||
</Link>
|
||||
</nav>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
"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>
|
||||
);
|
||||
}
|
||||
Loading…
Reference in New Issue