implemented location edit

This commit is contained in:
2024-01-05 14:52:32 +01:00
parent f26aae7d66
commit 3f7b681ab9
5 changed files with 200 additions and 12 deletions

View File

@@ -15,6 +15,7 @@ export interface BillingLocation {
/** the value is encoded as yyyymm (i.e. 202301) */
yearMonth: number;
bills: Bill[];
notes: string|null;
};
/** Bill basic data */

107
app/lib/locationActions.ts Normal file
View File

@@ -0,0 +1,107 @@
'use server';
import { z } from 'zod';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import clientPromise from './mongodb';
import { BillingLocation } from './db-types';
import { ObjectId } from 'mongodb';
export type State = {
errors?: {
locationName?: string[];
locationNotes?: string[],
};
message?:string | null;
}
const FormSchema = z.object({
_id: z.string(),
locationName: z.coerce.string().min(1, "Location Name is required."),
locationNotes: z.string(),
});
const UpdateLocation = FormSchema.omit({ _id: true });
/**
* Server-side action which adds or updates a bill
* @param locationId location of the bill
* @param prevState previous state of the form
* @param formData form data
* @returns
*/
export async function updateOrAddLocation(locationId?: string, prevState:State, formData: FormData) {
const validatedFields = UpdateLocation.safeParse({
locationName: formData.get('locationName'),
locationNotes: formData.get('locationNotes'),
});
// If form validation fails, return errors early. Otherwise, continue...
if(!validatedFields.success) {
return({
errors: validatedFields.error.flatten().fieldErrors,
message: "Missing Fields",
});
}
const {
locationName,
locationNotes,
} = validatedFields.data;
// update the bill in the mongodb
const client = await clientPromise;
const db = client.db("rezije");
if(locationId) {
await db.collection<BillingLocation>("lokacije").updateOne(
{
_id: locationId // find a location with the given locationID
},
{
$set: {
name: locationName,
notes: locationNotes,
}
});
} else {
await db.collection<BillingLocation>("lokacije").insertOne({
_id: (new ObjectId()).toHexString(),
name: locationName,
notes: locationNotes,
yearMonth: 202101, // ToDo: get the current year and month
bills: [],
});
}
// clear the cache for the path
revalidatePath('/');
// go to the bill list
redirect('/');
}
export const fetchLocationById = async (locationID:string) => {
const client = await clientPromise;
const db = client.db("rezije");
// find a location with the given locationID
const billLocation = await db.collection<BillingLocation>("lokacije").findOne({ _id: locationID });
if(!billLocation) {
console.log(`Location ${locationID} not found`);
return(null);
}
return(billLocation);
}
export const deleteLocationById = async (locationID:string) => {
const client = await clientPromise;
const db = client.db("rezije");
// find a location with the given locationID
const post = await db.collection<BillingLocation>("lokacije").deleteOne({ _id: locationID });
return(post.deletedCount);
}