i18n support added to all form validations

This commit is contained in:
2024-02-17 07:28:47 +01:00
parent 1da6479c80
commit 30b3da9c31
4 changed files with 38 additions and 30 deletions

View File

@@ -8,7 +8,8 @@ import { withUser } from '@/app/lib/auth';
import { AuthenticatedUser } from '../types/next-auth';
import { gotoHome } from './navigationActions';
import { unstable_noStore as noStore } from 'next/cache';
import { asyncTimeout } from '../asyncTimeout';
import { IntlTemplateFn } from '@/app/i18n';
import { getTranslations } from "next-intl/server";
export type State = {
errors?: {
@@ -18,13 +19,17 @@ export type State = {
message?:string | null;
};
const FormSchema = z.object({
/**
* Schema for validating location form fields
* @description this is defined as factory function so that it can be used with the next-intl library
*/
const FormSchema = (t:IntlTemplateFn) => z.object({
_id: z.string(),
locationName: z.coerce.string().min(1, "Location Name is required."),
locationName: z.coerce.string().min(1, t("location-name-required")),
locationNotes: z.string(),
});
const UpdateLocation = FormSchema.omit({ _id: true });
})
// dont include the _id field in the response
.omit({ _id: true });
/**
* Server-side action which adds or updates a bill
@@ -37,7 +42,9 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
noStore();
const validatedFields = UpdateLocation.safeParse({
const t = await getTranslations("location-edit-form.validation");
const validatedFields = FormSchema(t).safeParse({
locationName: formData.get('locationName'),
locationNotes: formData.get('locationNotes'),
});
@@ -84,8 +91,6 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
});
}
// await asyncTimeout(1000);
if(yearMonth) await gotoHome(yearMonth);
return {
@@ -124,8 +129,6 @@ export const fetchAllLocations = withUser(async (user:AuthenticatedUser, year:nu
})
.toArray();
// await asyncTimeout(1000);
return(locations)
})
@@ -154,8 +157,6 @@ export const fetchLocationById = withUser(async (user:AuthenticatedUser, locatio
return(null);
}
// await asyncTimeout(1000);
return(billLocation);
})
@@ -170,7 +171,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 });
// await asyncTimeout(1000);
await gotoHome(yearMonth)
})