From f52d6d60040bad937b9b0f207d79e97010d7b415 Mon Sep 17 00:00:00 2001 From: christian Date: Sun, 2 Jun 2024 16:05:45 +0200 Subject: [PATCH 1/3] fixed duplicate check --- src/server/actions/addCountry.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/server/actions/addCountry.ts b/src/server/actions/addCountry.ts index 810e745..dbc6ee9 100644 --- a/src/server/actions/addCountry.ts +++ b/src/server/actions/addCountry.ts @@ -6,16 +6,19 @@ import { countries } from "../db/schema"; import { ZodError, z } from "zod"; import { eq } from "drizzle-orm"; -const schema = z.object({ - name: z.string().min(1, { message: "Name is required" }), -}); - export async function addCountry(prevstate: any, formData: FormData) { const name = formData.get("name") as string; const exists = await db .select({ name: countries.name }) .from(countries) .where(eq(countries.name, name)); + + const schema = z.object({ + name: z + .string() + .min(1, { message: "Name is required" }) + .refine(() => !exists[0], { message: "Country already exists" }), + }); try { schema.parse({ name, From 72156c0778317d91b6ef10b9fa431fcf7a59895e Mon Sep 17 00:00:00 2001 From: christian Date: Sun, 2 Jun 2024 16:12:23 +0200 Subject: [PATCH 2/3] updated error msg for duplicate country --- src/server/actions/addCountry.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/actions/addCountry.ts b/src/server/actions/addCountry.ts index dbc6ee9..7de3b00 100644 --- a/src/server/actions/addCountry.ts +++ b/src/server/actions/addCountry.ts @@ -17,7 +17,7 @@ export async function addCountry(prevstate: any, formData: FormData) { name: z .string() .min(1, { message: "Name is required" }) - .refine(() => !exists[0], { message: "Country already exists" }), + .refine(() => !exists[0], { message: `${name} already exists` }), }); try { schema.parse({ From 753f09da32278f7ef9e1bffaa3fa3a1c81cf4561 Mon Sep 17 00:00:00 2001 From: christian Date: Sun, 2 Jun 2024 16:32:17 +0200 Subject: [PATCH 3/3] fixed to lowercase --- .../_components/admin/CreateCountryForm.tsx | 12 ++++++++++-- src/server/actions/addCountry.ts | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/app/_components/admin/CreateCountryForm.tsx b/src/app/_components/admin/CreateCountryForm.tsx index f7d5e21..2b72471 100644 --- a/src/app/_components/admin/CreateCountryForm.tsx +++ b/src/app/_components/admin/CreateCountryForm.tsx @@ -5,6 +5,7 @@ import SubmitButton from "../SubmitButton"; import { useFormState } from "react-dom"; import { useEffect, useRef } from "react"; import clsx from "clsx"; +import { Check, CircleX } from "lucide-react"; export default function CreateCountryForm() { const [formState, formAction] = useFormState(addCountry, { @@ -21,14 +22,21 @@ export default function CreateCountryForm() { } }, [formState.message]); return ( -
+ + {formState.message === "success" ? ( + + ) : ( +
+ + {formState.errors?.name}{" "} +
+ )} - {formState.errors?.name} ); } diff --git a/src/server/actions/addCountry.ts b/src/server/actions/addCountry.ts index 7de3b00..2bb4b66 100644 --- a/src/server/actions/addCountry.ts +++ b/src/server/actions/addCountry.ts @@ -7,29 +7,31 @@ import { ZodError, z } from "zod"; import { eq } from "drizzle-orm"; export async function addCountry(prevstate: any, formData: FormData) { - const name = formData.get("name") as string; + const name = (formData.get("name") as string).toLowerCase(); const exists = await db .select({ name: countries.name }) .from(countries) .where(eq(countries.name, name)); + //Define the schema for the form data const schema = z.object({ name: z .string() .min(1, { message: "Name is required" }) .refine(() => !exists[0], { message: `${name} already exists` }), }); + //Parse the form data using the schema for validation, and check if the name already exists try { schema.parse({ name, }); - if (!exists[0]) { - await db - .insert(countries) - .values({ name }) - .returning({ name: countries.name }); - revalidatePath("/"); - } + //If the name doesn't exist, add the country to the database abd revalidate the page + await db + .insert(countries) + .values({ name }) + .returning({ name: countries.name }); + revalidatePath("/"); + //Return a success message return { message: "success", errors: undefined,