"use client"; import { DocumentIcon, TrashIcon } from "@heroicons/react/24/outline"; import { PlainBill } from "../lib/db-types"; import { FC } from "react"; import { useFormState } from "react-dom"; import { gotoHome, updateBill } from "../lib/actions"; import { redirect } from 'next/navigation'; // Next.js does not encode an utf-8 file name correctly when sending a form with a file attachment // This is a workaround for that const updateBill2 = (locationId: string, billId:string, prevState:any, formData: FormData) => { console.log('updateBill2', formData.get('billAttachment')); // URL encode the file name of the attachment so it is correctly sent to the server const billAttachment = formData.get('billAttachment') as File; formData.set('billAttachment', billAttachment, encodeURIComponent(billAttachment.name)); return updateBill(locationId, billId, prevState, formData); } export interface BillEditFormProps { invoiceID: string, bill: PlainBill } export const BillEditForm:FC = ({ invoiceID, bill: { id, name, paid, attachment, notes } }) => { const initialState = { message: null, errors: {} }; const updateBillWithId = updateBill2.bind(null, invoiceID, id); const [ state, dispatch ] = useFormState(updateBillWithId, initialState); // redirect to the main page const handleCancel = () => { console.log('handleCancel'); gotoHome(); }; return(
{state.errors?.billName && state.errors.billName.map((error: string) => (

{error}

))}
{ // attachment ? {attachment.fileName} : null }
{state.errors?.billAttachment && state.errors.billAttachment.map((error: string) => (

{error}

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

{error}

))}
{state.message &&

{state.message}

}
); }