"use client"; import { TrashIcon } from "@heroicons/react/24/outline"; import { FC, useState } from "react"; import { BillingLocation, YearMonth } 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 } | { /** location which should be edited */ location?: undefined, /** year adn month at a new billing location should be assigned */ yearMonth: YearMonth } export const LocationEditForm: FC = ({ location, yearMonth }) => { 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", 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" ? ( <>
{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}

))}
{t("tenant--payment-instructions-note")} ) : // ELSE include hidden inputs to preserve existing values <> }
{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)} />
{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")}
) }