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,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}`)
})