103 lines
4.8 KiB
TypeScript
103 lines
4.8 KiB
TypeScript
"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<BillEditFormProps> = ({ 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(
|
|
<div className="card card-compact card-bordered max-w-sm bg-base-100 shadow-s my-1">
|
|
<div className="card-body">
|
|
<form action={ dispatch }>
|
|
<TrashIcon className="h-[1em] w-[1em] absolute cursor-pointer text-error bottom-5 right-4 text-2xl" />
|
|
|
|
<input id="billName" name="billName" type="text" placeholder="Bill name" className="input input-bordered w-full" defaultValue={name} required />
|
|
<div id="status-error" aria-live="polite" aria-atomic="true">
|
|
{state.errors?.billName &&
|
|
state.errors.billName.map((error: string) => (
|
|
<p className="mt-2 text-sm text-red-500" key={error}>
|
|
{error}
|
|
</p>
|
|
))}
|
|
</div>
|
|
{
|
|
// <textarea className="textarea textarea-bordered my-1 w-full max-w-sm block" placeholder="Opis" value="Pričuva, Voda, Smeće"></textarea>
|
|
|
|
attachment ?
|
|
<a href={`/attachment/${invoiceID}-${id}/${attachment.fileName}`} target="_blank" className='text-center block max-w-[24em] text-nowrap truncate inline-block mt-4'>
|
|
<DocumentIcon className="h-[1em] w-[1em] text-2xl inline-block mr-1" />
|
|
{decodeURIComponent(attachment.fileName)}
|
|
</a>
|
|
: null
|
|
}
|
|
<input id="billAttachment" name="billAttachment" type="file" className="file-input file-input-bordered w-full max-w-sm file-input-xs my-2" />
|
|
<div id="status-error" aria-live="polite" aria-atomic="true">
|
|
{state.errors?.billAttachment &&
|
|
state.errors.billAttachment.map((error: string) => (
|
|
<p className="mt-2 text-sm text-red-500" key={error}>
|
|
{error}
|
|
</p>
|
|
))}
|
|
</div>
|
|
|
|
<div className="form-control w-32 p-1">
|
|
<label className="cursor-pointer label p-0">
|
|
<span className="label-text">Paid</span>
|
|
<input id="billPaid" name="billPaid" type="checkbox" className="toggle toggle-success" defaultChecked={paid} />
|
|
</label>
|
|
</div>
|
|
|
|
<textarea id="billNotes" name="billNotes" className="textarea textarea-bordered my-2 w-full max-w-sm block" placeholder="Note" defaultValue={notes ?? ''}></textarea>
|
|
<div id="status-error" aria-live="polite" aria-atomic="true">
|
|
{state.errors?.billNotes &&
|
|
state.errors.billNotes.map((error: string) => (
|
|
<p className="mt-2 text-sm text-red-500" key={error}>
|
|
{error}
|
|
</p>
|
|
))}
|
|
</div>
|
|
|
|
<div id="status-error" aria-live="polite" aria-atomic="true">
|
|
{state.message &&
|
|
<p className="mt-2 text-sm text-red-500">
|
|
{state.message}
|
|
</p>
|
|
}
|
|
</div>
|
|
<button type="submit" className="btn btn-primary">Save</button>
|
|
<button type="button" className="btn btn-neutral ml-3" onClick={handleCancel}>Cancel</button>
|
|
</form>
|
|
</div>
|
|
</div>);
|
|
} |