Merge branch 'main' of https://gitea.rannes.dev/christian/wine-shop
All checks were successful
Vercel Production Deployment / Deploy-Production (push) Successful in 1m46s

This commit is contained in:
christian 2024-05-24 17:29:01 +02:00
commit b2d31d96ab
8 changed files with 77 additions and 45 deletions

View File

@ -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",

View File

@ -1,26 +1,56 @@
"use client";
import { ChangeEvent, FormEvent, useEffect, useState } from "react";
import { addWine } from "../server/actions/addWine";
import { getProducers } from "../server/actions/allProducers";
import { FormEvent } from "react";
import { useFormState } from "react-dom";
import { allProducers, insertWine } from "~/server/db";
interface Producer {
id: string;
name: string;
createdAt: Date;
country: string;
region: string;
email: string;
}
const initialState = {
message: null,
};
export default function CreateWine() {
const [name, setName] = useState("");
const [producer, setProducer] = useState("");
const [allProducers, setAllProducers] = useState<Producer[]>([]);
export default function CreateWineForm() {
const [state, formAction] = useFormState(insertWine, { message: "" });
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<HTMLInputElement>) => {
setName(event.target.value);
};
const handleProducer = (event: ChangeEvent<HTMLSelectElement>) => {
setProducer(event.target.value);
};
const handleAdd = async (event: FormEvent) => {
event.preventDefault();
addWine(producer, name);
setName("");
};
return (
<form action={}>
<select name="producer" id="producer">
{allProducers.map((producer) => (
<option key={producer.id} value={producer.id}>
{producer.name}
<div>
<input type="text" onChange={handleName} value={name} />
<select name="producer" onSelect={handleProducer}>
{allProducers.map((item) => (
<option key={item.id} value={item.id}>
{item.name}
</option>
))}
</select>
<input type="text" name="name" id="name" />
<input type="datetime-local" name="createdAt" id="createdAt" />
</form>
<button onClick={handleAdd}>Submit wine</button>
</div>
);
}

View File

@ -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() {
<main className="">
<div className="container ">
<h1>Yes hello</h1>
{/* <CreateWine /> */}
<CreateWine />
<WineCards />
</div>
</main>

View File

@ -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();
};

View File

@ -0,0 +1,7 @@
'use server'
import { db } from "../db/index";
export async function getProducers(){
return db.query.producers.findMany();
}

View File

@ -0,0 +1,7 @@
'use server'
import { db } from "../db/index";
export async function getWines(){
return db.query.wines.findMany();
}

View File

@ -4,25 +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 = {
name: string;
producer: string;
};
export type Wine = {
id: string;
name: string;
producer: string;
createdAt: Date;
updatedAt: Date | null;
};
export const insertWine = async (wine: NewWine) => {
return db.insert(schema.wines).values(wine);
}
export const allProducers = await db.query.producers.findMany();

View File

@ -1,5 +1,3 @@
import { drizzle } from 'drizzle-orm/vercel-postgres';
import { sql } from "@vercel/postgres";
import {
pgTableCreator,
text,