From 7590e5439208b4627d803507d814a96eb0871f49 Mon Sep 17 00:00:00 2001 From: christian Date: Sun, 2 Jun 2024 07:54:09 +0200 Subject: [PATCH 1/5] updated schema so same region name can exist in differentcountries --- src/server/db/schema.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/server/db/schema.ts b/src/server/db/schema.ts index cba41e4..06eec43 100644 --- a/src/server/db/schema.ts +++ b/src/server/db/schema.ts @@ -7,6 +7,7 @@ import { text, timestamp, uuid, + unique, } from "drizzle-orm/pg-core"; import { relations } from "drizzle-orm"; @@ -95,11 +96,17 @@ export const subRegionsRelations = relations(subRegions, ({ many, one }) => ({ }), })); -export const regions = createTable("region", { - id: uuid("id").primaryKey().notNull().defaultRandom(), - name: text("name").notNull().unique(), - countryId: uuid("countryId").notNull(), -}); +export const regions = createTable( + "region", + { + id: uuid("id").primaryKey().notNull().defaultRandom(), + name: text("name").notNull(), + countryId: uuid("countryId").notNull(), + }, + (t) => ({ + uniqueRegion: unique().on(t.name, t.countryId), + }), +); export const regionsRelations = relations(regions, ({ many, one }) => ({ wines: many(wines), From 2c5fcbcbdbd983214494885a092385fb928025d8 Mon Sep 17 00:00:00 2001 From: christian Date: Sun, 2 Jun 2024 07:56:39 +0200 Subject: [PATCH 2/5] fixed conflict handling --- src/server/actions/addRegion.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/actions/addRegion.ts b/src/server/actions/addRegion.ts index 005d168..e9b146d 100644 --- a/src/server/actions/addRegion.ts +++ b/src/server/actions/addRegion.ts @@ -25,7 +25,7 @@ export const addRegion = async (prevstate: any, formData: FormData) => { const confirmedRegion = await db .insert(regions) .values(newRegion.data) - .onConflictDoNothing({ target: regions.name }) + .onConflictDoNothing() .returning({ name: regions.name }); if (!confirmedRegion[0]?.name) { return { From 14b14f9f260e3e07e09a0539f7f2550c59dce1da Mon Sep 17 00:00:00 2001 From: christian Date: Sun, 2 Jun 2024 08:14:30 +0200 Subject: [PATCH 3/5] updated create region form --- src/app/_components/AllRegions.tsx | 10 +++++++++- src/app/_components/admin/CreateRegionForm.tsx | 1 - 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/app/_components/AllRegions.tsx b/src/app/_components/AllRegions.tsx index 9c3f008..4eb73b8 100644 --- a/src/app/_components/AllRegions.tsx +++ b/src/app/_components/AllRegions.tsx @@ -2,6 +2,7 @@ import { db } from "~/server/db"; export default async function AllRegions() { const allRegions = await db.query.regions.findMany(); + const allCountries = await db.query.countries.findMany(); return (

All Regions:

@@ -9,7 +10,14 @@ export default async function AllRegions() { <>
    {allRegions.map((region) => ( -
  • {region.name}
  • +
  • + {region.name} -{" "} + { + allCountries.find( + (country) => country.id === region.countryId, + )?.name + } +
  • ))}
diff --git a/src/app/_components/admin/CreateRegionForm.tsx b/src/app/_components/admin/CreateRegionForm.tsx index b7f6a89..4951d7b 100644 --- a/src/app/_components/admin/CreateRegionForm.tsx +++ b/src/app/_components/admin/CreateRegionForm.tsx @@ -11,7 +11,6 @@ import { SelectValue, } from "~/components/ui/select"; import { useFormState } from "react-dom"; -import { init } from "next/dist/compiled/webpack/webpack"; interface Country { id: string; From c90eedcabd1e1b3c2f0fa2e58de98092efd63715 Mon Sep 17 00:00:00 2001 From: christian Date: Sun, 2 Jun 2024 09:27:40 +0200 Subject: [PATCH 4/5] made subregions unique to regions --- src/server/db/schema.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/server/db/schema.ts b/src/server/db/schema.ts index 06eec43..fe29446 100644 --- a/src/server/db/schema.ts +++ b/src/server/db/schema.ts @@ -82,11 +82,17 @@ export const winesRelations = relations(wines, ({ one }) => ({ }), })); -export const subRegions = createTable("subRegion", { - id: uuid("id").primaryKey().notNull().defaultRandom(), - name: text("name").notNull().unique(), - regionId: uuid("regionId").notNull(), -}); +export const subRegions = createTable( + "subRegion", + { + id: uuid("id").primaryKey().notNull().defaultRandom(), + name: text("name").notNull().unique(), + regionId: uuid("regionId").notNull(), + }, + (t) => ({ + uniqueSubRegion: unique().on(t.name, t.regionId), + }), +); export const subRegionsRelations = relations(subRegions, ({ many, one }) => ({ wines: many(wines), From 067c31d745545d9dc3e0b8bc63eac2cac5b80c96 Mon Sep 17 00:00:00 2001 From: christian Date: Sun, 2 Jun 2024 11:41:55 +0200 Subject: [PATCH 5/5] fixing forms --- .../_components/admin/CreateCountryForm.tsx | 18 +++----- .../_components/admin/CreateRegionForm.tsx | 7 ++- src/server/actions/addCountry.ts | 46 +++++++++++++++---- src/server/actions/addRegion.ts | 17 ++++--- 4 files changed, 53 insertions(+), 35 deletions(-) diff --git a/src/app/_components/admin/CreateCountryForm.tsx b/src/app/_components/admin/CreateCountryForm.tsx index bd79a74..45ab5bf 100644 --- a/src/app/_components/admin/CreateCountryForm.tsx +++ b/src/app/_components/admin/CreateCountryForm.tsx @@ -2,22 +2,16 @@ import { Input } from "~/components/ui/input"; import { addCountry } from "~/server/actions/addCountry"; import SubmitButton from "../SubmitButton"; -import { useRef } from "react"; - -const initialState = { - name: "" as string, -}; +import { useFormState } from "react-dom"; export default function CreateCountryForm() { - const ref = useRef(null); - const handleSubmit = async (formData: FormData) => { - ref.current?.reset(); - await addCountry(formData); - }; + const [state, formAction] = useFormState(addCountry, null); + return ( -
- + + + {state?.message} ); } diff --git a/src/app/_components/admin/CreateRegionForm.tsx b/src/app/_components/admin/CreateRegionForm.tsx index 4951d7b..2c3ba9b 100644 --- a/src/app/_components/admin/CreateRegionForm.tsx +++ b/src/app/_components/admin/CreateRegionForm.tsx @@ -1,7 +1,7 @@ "use client"; import { Input } from "~/components/ui/input"; import SubmitButton from "../SubmitButton"; -import { useRef } from "react"; +import { useActionState } from "react"; import { addRegion } from "~/server/actions/addRegion"; import { Select, @@ -20,10 +20,9 @@ interface Country { export default function CreateRegionForm(props: { countries: Country[] }) { const [state, formAction] = useFormState(addRegion, null); const { countries } = props; - const ref = useRef(null); return ( -
- + +