From 17c0c0e853ea7790f2aa4e7869c28773e8e18bfb Mon Sep 17 00:00:00 2001 From: christian Date: Fri, 24 May 2024 12:01:58 +0200 Subject: [PATCH 1/2] CreateWine is sort of working. It's not pushing producer id though --- drizzle.config.ts | 1 - src/app/CreateWine.tsx | 63 +++++++++++++++++++++++------- src/app/page.tsx | 10 +++-- src/server/actions/addWine.ts | 11 ++++++ src/server/actions/allProducers.ts | 7 ++++ src/server/actions/allWines.ts | 7 ++++ src/server/db/schema.ts | 2 - 7 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 src/server/actions/addWine.ts create mode 100644 src/server/actions/allProducers.ts create mode 100644 src/server/actions/allWines.ts diff --git a/drizzle.config.ts b/drizzle.config.ts index ec6d8ac..bf2ea94 100644 --- a/drizzle.config.ts +++ b/drizzle.config.ts @@ -1,7 +1,6 @@ import { type Config } from "drizzle-kit"; import "./src/server/db/envConfig" - export default { schema: "./src/server/db/schema.ts", dialect: "postgresql", diff --git a/src/app/CreateWine.tsx b/src/app/CreateWine.tsx index 5b1ad5f..782ca19 100644 --- a/src/app/CreateWine.tsx +++ b/src/app/CreateWine.tsx @@ -1,24 +1,59 @@ -"use client"; -import { FormEvent } from "react"; -import { allProducers, insertWine } from "~/server/db"; +'use client' +import { ChangeEvent, FormEvent, useEffect, useState } from "react"; +import { addWine } from "../server/actions/addWine"; +import { getProducers } from "../server/actions/allProducers"; -export default function CreateWine() { - function handleSubmit(event: FormEvent) { - event.preventDefault(); +interface Producer { + id: string; + name: string; + createdAt: Date; + country: string; + region: string; + email: string; +} - const formData = new FormData(event.currentTarget); - const response = insertWine(formData); + + +export default function CreateWine(){ + const [name, setName] = useState(''); + const [producer, setProducer] = useState(''); + const [allProducers, setAllProducers] = useState([]); + + useEffect(() => { + const fetchProducers = async () => { + try { + const producers = await getProducers(); + setAllProducers(producers); + } catch (error) { + console.error("Failed to fetch producers:", error); + } + }; + fetchProducers(); + }, []); + + const handleName = (event: ChangeEvent) => { + setName(event.target.value); } + const handleProducer = (event: ChangeEvent) => { + setProducer(event.target.value); + } + const handleAdd = async (event: FormEvent) => { + event.preventDefault(); + addWine(producer, name); + setName(''); + }; return ( -
- + -
+ + ); } diff --git a/src/app/page.tsx b/src/app/page.tsx index a8d7ed2..157fd6f 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,9 +1,11 @@ -import { FormEvent } from "react"; -import { allWines, insertWine, allProducers } from "~/server/db"; +import { getProducers } from "~/server/actions/allProducers"; +import { getWines } from "../server/actions/allWines"; import CreateWine from "./CreateWine"; - export const dynamic = "force-dynamic"; +const allWines = await getWines(); +const allProducers = await getProducers(); + function WineCards() { return ( <> @@ -21,7 +23,7 @@ export default async function HomePage() {

Yes hello

- {/* */} +
diff --git a/src/server/actions/addWine.ts b/src/server/actions/addWine.ts new file mode 100644 index 0000000..0fa1113 --- /dev/null +++ b/src/server/actions/addWine.ts @@ -0,0 +1,11 @@ +'use server' +import { db } from '../db/index' +import { wines } from '../db/schema' + +export const addWine = async (producer: string, name: string) => { + await db.insert(wines).values({ + producer: producer, + name: name, + }) + .returning(); + }; \ No newline at end of file diff --git a/src/server/actions/allProducers.ts b/src/server/actions/allProducers.ts new file mode 100644 index 0000000..4838261 --- /dev/null +++ b/src/server/actions/allProducers.ts @@ -0,0 +1,7 @@ +'use server' +import { db } from "../db/index"; + + +export async function getProducers(){ + return db.query.producers.findMany(); +} \ No newline at end of file diff --git a/src/server/actions/allWines.ts b/src/server/actions/allWines.ts new file mode 100644 index 0000000..45bb29c --- /dev/null +++ b/src/server/actions/allWines.ts @@ -0,0 +1,7 @@ +'use server' +import { db } from "../db/index"; + + +export async function getWines(){ + return db.query.wines.findMany(); +} \ No newline at end of file diff --git a/src/server/db/schema.ts b/src/server/db/schema.ts index 15ed081..7fac292 100644 --- a/src/server/db/schema.ts +++ b/src/server/db/schema.ts @@ -1,5 +1,3 @@ -import { drizzle } from 'drizzle-orm/vercel-postgres'; -import { sql } from "@vercel/postgres"; import { pgTableCreator, text, From ec752e0367bdbc60a15766b0d2f724db8f1e085f Mon Sep 17 00:00:00 2001 From: christian Date: Fri, 24 May 2024 12:11:23 +0200 Subject: [PATCH 2/2] updated index.ts --- src/server/db/index.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/server/db/index.ts b/src/server/db/index.ts index 8c70d51..108c0bc 100644 --- a/src/server/db/index.ts +++ b/src/server/db/index.ts @@ -4,12 +4,3 @@ import { sql } from '@vercel/postgres'; import * as schema from './schema'; export const db = drizzle(sql, { schema }); - -export const allWines = await db.query.wines.findMany(); - -type NewWine = typeof schema.wines.$inferInsert; -export const insertWine = async (wine: NewWine) => { - return db.insert(schema.wines).values(wine).returning(); -} - -export const allProducers = await db.query.producers.findMany();