From 64c7ee4474e7197f26dbfd3a4b5cde046a2482d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Dere=C5=BEi=C4=87?= Date: Fri, 5 Jan 2024 09:38:27 +0100 Subject: [PATCH] Bill Edit Form: saving all the fields --- app/lib/actions.ts | 19 +++++++++++---- app/ui/BillEditForm.tsx | 54 ++++++++++++++++++++++++++++++++++------- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/app/lib/actions.ts b/app/lib/actions.ts index bd5b7c2..52b6278 100644 --- a/app/lib/actions.ts +++ b/app/lib/actions.ts @@ -9,15 +9,17 @@ import { ObjectId } from 'mongodb'; export type State = { errors?: { billName?: string[]; + billAttachment?: string[], + billNotes?: string[], }; message?:string | null; } const FormSchema = z.object({ _id: z.string(), - billName: z.string({ - invalid_type_error: 'Please select a bill name', - }), + billName: z.coerce.string().min(1, "Bill Name is required."), + billAttachment: z.object({ }), + billNotes: z.string(), }); const UpdateBill = FormSchema.omit({ _id: true }); @@ -26,6 +28,8 @@ export async function updateBill(locationId: string, billId:string, prevState:St const validatedFields = UpdateBill.safeParse({ billName: formData.get('billName'), + billAttachment: formData.get('billAttachment'), + billNotes: formData.get('billNotes'), }); // If form validation fails, return errors early. Otherwise, continue... @@ -39,8 +43,12 @@ export async function updateBill(locationId: string, billId:string, prevState:St const { billName, + billAttachment, + billNotes, } = validatedFields.data; + const billPaid = formData.get('billPaid') === 'on'; + // update the bill in the mongodb const client = await clientPromise; const db = client.db("rezije"); @@ -53,6 +61,9 @@ export async function updateBill(locationId: string, billId:string, prevState:St { $set: { "bills.$[elem].name": billName, + "bills.$[elem].paid": billPaid, + "bills.$[elem].attachment": billAttachment, + "bills.$[elem].notes": billNotes, } }, { arrayFilters: [ @@ -60,8 +71,6 @@ export async function updateBill(locationId: string, billId:string, prevState:St ] }); - console.log("updateBill.success", post); - // clear the cache for the path revalidatePath('/'); // go to the bill list diff --git a/app/ui/BillEditForm.tsx b/app/ui/BillEditForm.tsx index 4ff2c10..1c9e58e 100644 --- a/app/ui/BillEditForm.tsx +++ b/app/ui/BillEditForm.tsx @@ -23,7 +23,17 @@ export const BillEditForm:FC = ({ invoiceID, bill: { id, name
- + + +
+ {state.errors?.billName && + state.errors.billName.map((error: string) => ( +

+ {error} +

+ ))} +
+ { // // @@ -31,15 +41,41 @@ export const BillEditForm:FC = ({ invoiceID, bill: { id, name // 2023-22-12 document GSKG račun za 2023.pdf // } - -
- + +
+ {state.errors?.billAttachment && + state.errors.billAttachment.map((error: string) => ( +

+ {error} +

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

+ {error} +

+ ))} +
+ +
+ {state.message && +

+ {state.message} +

+ } +
+
);