Bill Edit Form: saving all the fields

This commit is contained in:
2024-01-05 09:38:27 +01:00
parent 0b55fb0f6a
commit 64c7ee4474
2 changed files with 59 additions and 14 deletions

View File

@@ -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