"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 whether to generate 2D code for tenant (use persisted value from database) const [generateTenantCode, setGenerateTenantCode] = useState( location?.generateTenantCode ?? false ); // Track whether to automatically notify tenant (use persisted value from database) const [autoBillFwd, setautoBillFwd] = useState( location?.autoBillFwd ?? false ); // Track whether to automatically send rent notification (use persisted value from database) const [rentDueNotification, setrentDueNotification] = useState( location?.rentDueNotification ?? false ); // Track tenant field values for real-time validation const [tenantFields, setTenantFields] = useState({ tenantFirstName: location?.tenantFirstName ?? "", tenantLastName: location?.tenantLastName ?? "", tenantEmail: location?.tenantEmail ?? "", }); const handleTenantFieldChange = (field: keyof typeof tenantFields, value: string) => { setTenantFields(prev => ({ ...prev, [field]: value })); }; let { year, month } = location ? location.yearMonth : yearMonth; return (
{ location && }
{t("location-name-legend")}
{state.errors?.locationName && state.errors.locationName.map((error: string) => (

{error}

))}
{t("tenant-2d-code-legend")} {t("tenant-2d-code-info")}
{generateTenantCode && ( <>
handleTenantFieldChange("tenantFirstName", e.target.value)} />
{state.errors?.tenantFirstName && state.errors.tenantFirstName.map((error: string) => (

{error}

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

{error}

))}
)}
{t("auto-utility-bill-forwarding-legend")} {t("auto-utility-bill-forwarding-info")}
{autoBillFwd && (
{t("utility-bill-forwarding-strategy-label")}
)}
{t("auto-rent-notification-legend")} {t("auto-rent-notification-info")}
{rentDueNotification && (
{t("rent-due-day-label")}
)}
{(autoBillFwd || rentDueNotification) && (
{t("tenant-email-legend")} handleTenantFieldChange("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")}
) }