"use client"; import { FC, useState } from "react"; import { UserSettings } from "../lib/db-types"; import { updateUserSettings } from "../lib/actions/userSettingsActions"; import { useFormState, useFormStatus } from "react-dom"; import { useLocale, useTranslations } from "next-intl"; import Link from "next/link"; import SettingsIcon from "@mui/icons-material/Settings"; import { formatIban } from "../lib/formatStrings"; import { InfoBox } from "./InfoBox"; import { NoteBox } from "./NoteBox"; export type UserSettingsFormProps = { userSettings: UserSettings | null; } type FormFieldsProps = { userSettings: UserSettings | null; errors: any; message: string | null; } const FormFields: FC = ({ userSettings, errors, message }) => { const { pending } = useFormStatus(); const t = useTranslations("user-settings-form"); const locale = useLocale(); // Track current form values for real-time validation const [formValues, setFormValues] = useState({ ownerName: userSettings?.ownerName ?? "", ownerStreet: userSettings?.ownerStreet ?? "", ownerTown: userSettings?.ownerTown ?? "", ownerIBAN: formatIban(userSettings?.ownerIBAN) ?? "", currency: userSettings?.currency ?? "EUR", ownerRevolutProfileName: userSettings?.ownerRevolutProfileName ?? "", showPaymentInstructions: userSettings?.showPaymentInstructionsInMonthlyStatement ?? "disabled", }); // https://revolut.me/aderezic?currency=EUR&amount=70000 const handleInputChange = (field: keyof typeof formValues, value: string) => { setFormValues(prev => ({ ...prev, [field]: value })); }; return ( <>
{t("general-settings-legend")}
{errors?.currency && errors.currency.map((error: string) => (

{error}

))}
{t("tenant-payment-instructions--legend")} {t("info-box-message")}
{formValues.showPaymentInstructions === "iban" && ( <>
Informacije za uplatu
handleInputChange("ownerName", e.target.value)} disabled={pending} />
{errors?.ownerName && errors.ownerName.map((error: string) => (

{error}

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

{error}

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

{error}

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

{error}

))}
{t("payment-additional-notes")} )} {formValues.showPaymentInstructions === "revolut" && ( <>
Informacije za uplatu
handleInputChange("ownerRevolutProfileName", e.target.value)} disabled={pending} />
{errors?.ownerRevolutProfileName && errors.ownerRevolutProfileName.map((error: string) => (

{error}

))}
{t("payment-additional-notes")} )}
{message && (

{message}

)}
{t("cancel-button")}
); }; export const UserSettingsForm: FC = ({ userSettings }) => { const initialState = { message: null, errors: {} }; const [state, dispatch] = useFormState(updateUserSettings, initialState); const t = useTranslations("user-settings-form"); return (

{t("title")}

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