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);
});

View File

@@ -1,14 +1,12 @@
'use server';
import { z } from 'zod';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import clientPromise, { getDbClient } from '../dbClient';
import { getDbClient } from '../dbClient';
import { BillingLocation, YearMonth } from '../db-types';
import { ObjectId } from 'mongodb';
import { auth, withUser } from '@/app/lib/auth';
import { withUser } from '@/app/lib/auth';
import { AuthenticatedUser } from '../types/next-auth';
import { NormalizedRouteManifest } from 'next/dist/server/base-server';
import { gotoHome } from './navigationActions';
export type State = {
errors?: {
@@ -82,10 +80,7 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
});
}
// clear the cache for the path
revalidatePath('/');
// go to the bill list
redirect('/');
await gotoHome(yearMonth ? `/?year=${yearMonth?.year}` : undefined)
});
@@ -128,7 +123,7 @@ export const fetchLocationById = withUser(async (user:AuthenticatedUser, locatio
return(billLocation);
})
export const deleteLocationById = withUser(async (user:AuthenticatedUser, locationID:string) => {
export const deleteLocationById = withUser(async (user:AuthenticatedUser, locationID:string, yearMonth:YearMonth) => {
const dbClient = await getDbClient();
@@ -137,5 +132,5 @@ export const deleteLocationById = withUser(async (user:AuthenticatedUser, locati
// find a location with the given locationID
const post = await dbClient.collection<BillingLocation>("lokacije").deleteOne({ _id: locationID, userId });
return(post.deletedCount);
await gotoHome(`/?year=${yearMonth?.year}`)
})

View File

@@ -1,10 +1,8 @@
'use server';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import clientPromise, { getDbClient } from '../dbClient';
import { getDbClient } from '../dbClient';
import { ObjectId } from 'mongodb';
import { BillingLocation, YearMonth } from '../db-types';
import { Bill, BillingLocation, YearMonth } from '../db-types';
import { AuthenticatedUser } from '../types/next-auth';
import { withUser } from '../auth';
@@ -22,7 +20,6 @@ export const addMonth = withUser(async (user:AuthenticatedUser, { year, month }:
// update the bill in the mongodb
const dbClient = await getDbClient();
const prevYear = month === 1 ? year - 1 : year;
const prevMonth = month === 1 ? 12 : month - 1;
@@ -52,24 +49,16 @@ export const addMonth = withUser(async (user:AuthenticatedUser, { year, month }:
paid: false,
attachment: null,
notes: null,
}
payedAmount: null
} as Bill
})
} as BillingLocation);
});
const newMonthLocations = await newMonthLocationsCursor.toArray()
await dbClient.collection<BillingLocation>("lokacije").insertMany(newMonthLocations);
// clear the cache for the path
revalidatePath('/');
// go to the bill list
redirect('/');
});
export async function gotoHome() {
redirect('/');
}
export const fetchAvailableYears = withUser(async (user:AuthenticatedUser) => {
const { id: userId } = user;

View File

@@ -0,0 +1,9 @@
'use server';
import { revalidatePath } from "next/cache";
import { redirect } from 'next/navigation';
export async function gotoHome(path: string = '/') {
revalidatePath(path, "page");
redirect(path);
}