"use client"; import { TrashIcon, ExclamationTriangleIcon, ClockIcon, EnvelopeIcon, CheckCircleIcon } from "@heroicons/react/24/outline"; import { FC, useState } from "react"; import { BillingLocation, UserSettings, YearMonth, EmailStatus } from "../lib/db-types"; import { updateOrAddLocation } from "../lib/actions/locationActions"; import { useFormState } from "react-dom"; import Link from "next/link"; import { useLocale, useTranslations } from "next-intl"; import { InfoBox } from "./InfoBox"; export type LocationEditFormProps = { /** location which should be edited */ location: BillingLocation, /** year adn month at a new billing location should be assigned */ yearMonth?: undefined, /** user settings for payment configuration */ userSettings: UserSettings | null } | { /** location which should be edited */ location?: undefined, /** year adn month at a new billing location should be assigned */ yearMonth: YearMonth, /** user settings for payment configuration */ userSettings: UserSettings | null } export const LocationEditForm: FC = ({ location, yearMonth, userSettings }) => { const initialState = { message: null, errors: {} }; const handleAction = updateOrAddLocation.bind(null, location?._id, location?.yearMonth ?? yearMonth); const [state, dispatch] = useFormState(handleAction, initialState); const t = useTranslations("location-edit-form"); const locale = useLocale(); // Track tenant field values for real-time validation const [formValues, setFormValues] = useState({ locationName: location?.name ?? "", tenantName: location?.tenantName ?? "", tenantStreet: location?.tenantStreet ?? "", tenantTown: location?.tenantTown ?? "", tenantEmail: location?.tenantEmail ?? "", tenantPaymentMethod: location?.tenantPaymentMethod ?? "none", proofOfPaymentType: location?.proofOfPaymentType ?? "none", autoBillFwd: location?.autoBillFwd ?? false, billFwdStrategy: location?.billFwdStrategy ?? "when-payed", rentDueNotification: location?.rentDueNotification ?? false, rentAmount: location?.rentAmount ?? "", rentDueDay: location?.rentDueDay ?? 1, }); const handleInputChange = (field: keyof typeof formValues, value: string | boolean | number) => { setFormValues(prev => ({ ...prev, [field]: value })); }; let { year, month } = location ? location.yearMonth : yearMonth; return (
{ location && }
{t("location-name-legend")}
handleInputChange("locationName", e.target.value)} />
{state.errors?.locationName && state.errors.locationName.map((error: string) => (

{error}

))}
{t("tenant-payment-instructions-legend")} {t("tenant-payment-instructions-code-info")}
{t("tenant-payment-instructions-method--legend")}
{formValues.tenantPaymentMethod === "iban" && userSettings?.enableIbanPayment ? (
{t("iban-payment--form-title")}
handleInputChange("tenantName", e.target.value)} />
{state.errors?.tenantName && state.errors.tenantName.map((error: string) => (

{error}

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

{error}

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

{error}

))}
) : // ELSE include hidden inputs to preserve existing values <> }
{t("proof-of-payment-attachment-type--legend")} {t("proof-of-payment-attachment-type--info")}
{t("proof-of-payment-attachment-type--option--label")} { formValues.tenantPaymentMethod === "none" && formValues.proofOfPaymentType === "combined" ?

{ t.rich("proof-of-payment-attachment-type--option--combined--hint", { strong: (children: React.ReactNode) => {children} } ) }

:

{ formValues.proofOfPaymentType === "combined" ? t("proof-of-payment-attachment-type--option--combined--tooltip") : t("proof-of-payment-attachment-type--option--per-bill--tooltip") }

}
{t("auto-utility-bill-forwarding-legend")} {t("auto-utility-bill-forwarding-info")}
{formValues.autoBillFwd && (
{t("utility-bill-forwarding-strategy-label")}
)}
{t("auto-rent-notification-legend")} {t("auto-rent-notification-info")}
{formValues.rentDueNotification && (
{t("rent-due-day-label")}
{t("rent-amount-label")} handleInputChange("rentAmount", parseFloat(e.target.value))} />
{state.errors?.rentAmount && state.errors.rentAmount.map((error: string) => (

{error}

))}
)}
{(formValues.autoBillFwd || formValues.rentDueNotification) && (
{t("tenant-email-legend")} handleInputChange("tenantEmail", e.target.value)} /> {location?.tenantEmail && location?.tenantEmail === formValues.tenantEmail && location?.tenantEmailStatus ? (
{location.tenantEmailStatus === EmailStatus.Unverified && ( <> {t("email-status.unverified")} )} {location.tenantEmailStatus === EmailStatus.VerificationPending && ( <> {t("email-status.verification-pending")} )} {location.tenantEmailStatus === EmailStatus.Verified && ( <> {t("email-status.verified")} )} {location.tenantEmailStatus === EmailStatus.Unsubscribed && ( <> {t("email-status.unsubscribed")} )}
):(
{t("email-status.unverified")}
)}
{state.errors?.tenantEmail && state.errors.tenantEmail.map((error: string) => (

{error}

))}
)}
{t("scope-legend")} {!location ? (
) : ( <> {t("update-scope-info")}
{t("update-scope-legend")}
)}
{ state.message &&

{state.message}

}
{t("cancel-button")}
) } export const LocationEditFormSkeleton: FC = () => { const t = useTranslations("location-edit-form"); return (
{t("location-name-legend")}
) }