"use client"; import { FC, useState } from "react"; import { UserProfile } from "../lib/db-types"; import { updateUserProfile } from "../lib/actions/userProfileActions"; import { useFormState, useFormStatus } from "react-dom"; import { useLocale, useTranslations } from "next-intl"; import Link from "next/link"; import AccountCircleIcon from "@mui/icons-material/AccountCircle"; import { formatIban } from "../lib/formatStrings"; import { InfoBox } from "./InfoBox"; export type AccountFormProps = { profile: UserProfile | null; } type FormFieldsProps = { profile: UserProfile | null; errors: any; message: string | null; } const FormFields: FC = ({ profile, errors, message }) => { const { pending } = useFormStatus(); const t = useTranslations("account-form"); const locale = useLocale(); // Track current form values for real-time validation const [formValues, setFormValues] = useState({ firstName: profile?.firstName ?? "", lastName: profile?.lastName ?? "", address: profile?.address ?? "", iban: formatIban(profile?.iban) ?? "", }); const handleInputChange = (field: keyof typeof formValues, value: string) => { setFormValues(prev => ({ ...prev, [field]: value })); }; // Check if any required field is missing (clean IBAN of spaces for validation) const cleanedIban = formValues.iban.replace(/\s/g, ''); const hasMissingData = !formValues.firstName || !formValues.lastName || !formValues.address || !cleanedIban; return ( <> {t("info-box-message")}
handleInputChange("firstName", e.target.value)} disabled={pending} />
{errors?.firstName && errors.firstName.map((error: string) => (

{error}

))}
handleInputChange("lastName", e.target.value)} disabled={pending} />
{errors?.lastName && errors.lastName.map((error: string) => (

{error}

))}
{errors?.address && errors.address.map((error: string) => (

{error}

))}
handleInputChange("iban", e.target.value)} disabled={pending} />
{errors?.iban && errors.iban.map((error: string) => (

{error}

))}
{hasMissingData && (
{t("warning-missing-data")}
)}
{message && (

{message}

)}
{t("cancel-button")}
); }; export const AccountForm: FC = ({ profile }) => { const initialState = { message: null, errors: {} }; const [state, dispatch] = useFormState(updateUserProfile, initialState); const t = useTranslations("account-form"); return (

{t("title")}

); }; export const AccountFormSkeleton: FC = () => { return (
); };