"use client"; import { TrashIcon } from "@heroicons/react/24/outline"; import { FC } from "react"; import { BillingLocation, YearMonth } from "../lib/db-types"; import { updateOrAddLocation } from "../lib/actions/locationActions"; import { useFormState } from "react-dom"; import { Main } from "./Main"; import Link from "next/link"; import { gotoHome } from "../lib/actions/navigationActions"; export interface LocationEditFormProps { /** location which should be edited */ location?: BillingLocation, /** 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, yearMonth); const [ state, dispatch ] = useFormState(handleAction, initialState); // redirect to the main page const handleCancel = () => { if(location) gotoHome(location?.yearMonth); }; return(
{ location && }
{state.errors?.locationName && state.errors.locationName.map((error: string) => (

{error}

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

{error}

))}
{ state.message &&

{state.message}

}
) }