form action redirects user to tjhe appropriate year

This commit is contained in:
2024-01-17 15:47:55 +01:00
parent 119d64344f
commit 0eb11e7d02
18 changed files with 158 additions and 86 deletions

View File

@@ -1,13 +1,12 @@
'use server';
import { z } from 'zod';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import clientPromise, { getDbClient } from '../dbClient';
import { BillAttachment, BillingLocation } from '../db-types';
import { getDbClient } from '../dbClient';
import { Bill, BillAttachment, BillingLocation, YearMonth } from '../db-types';
import { ObjectId } from 'mongodb';
import { withUser } from '@/app/lib/auth';
import { AuthenticatedUser } from '../types/next-auth';
import { gotoHome } from './navigationActions';
export type State = {
errors?: {
@@ -110,7 +109,7 @@ const serializeAttachment = async (billAttachment: File | null) => {
* @param formData form data
* @returns
*/
export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationId: string, billId:string|undefined, prevState:State, formData: FormData) => {
export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationId: string, billId:string|undefined, billYear:number|undefined, prevState:State, formData: FormData) => {
const { id: userId } = user;
@@ -192,18 +191,9 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
}
});
}
// clear the cache for the path
revalidatePath('/');
// go to the bill list
redirect('/');
await gotoHome(billYear ? `/?year=${billYear}` : undefined);
})
export async function gotoHome() {
revalidatePath('/');
redirect('/');
}
export const fetchBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string) => {
const { id: userId } = user;
@@ -226,10 +216,10 @@ export const fetchBillById = withUser(async (user:AuthenticatedUser, locationID:
return(null);
}
return(bill);
return([billLocation, bill] as [BillingLocation, Bill]);
})
export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string) => {
export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID:string, billID:string, year:number) => {
const { id: userId } = user;
@@ -250,5 +240,6 @@ export const deleteBillById = withUser(async (user:AuthenticatedUser, locationID
}
});
await gotoHome(`/?year=${year}`);
return(post.modifiedCount);
});