diff --git a/web/app/profil/actions.ts b/web/app/profil/actions.ts new file mode 100644 index 0000000..6539ff7 --- /dev/null +++ b/web/app/profil/actions.ts @@ -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 { + 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 }; +} diff --git a/web/app/profil/page.tsx b/web/app/profil/page.tsx new file mode 100644 index 0000000..d7a1f0a --- /dev/null +++ b/web/app/profil/page.tsx @@ -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 ( +
+

Mein Profil

+

{profileResult.profile.name}

+ + + +
+ ); +} diff --git a/web/components/NavLinks.tsx b/web/components/NavLinks.tsx index f83e12d..19eabf6 100644 --- a/web/components/NavLinks.tsx +++ b/web/components/NavLinks.tsx @@ -64,6 +64,12 @@ export function NavLinks({ Nutzerverwaltung )} + + Mein Profil + ); } diff --git a/web/components/profil/ProfilForm.tsx b/web/components/profil/ProfilForm.tsx new file mode 100644 index 0000000..aadc8df --- /dev/null +++ b/web/components/profil/ProfilForm.tsx @@ -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(null); + const [gespeichert, setGespeichert] = useState(false); + const [isPending, startTransition] = useTransition(); + + function handleSubmit(event: React.FormEvent) { + 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 ( +
+ {fehler && ( +
+ {fehler} +
+ )} + + {gespeichert && ( +

+ Gespeichert. +

+ )} + + + ); +}