maybe fixed query. now to fix form

This commit is contained in:
christian 2024-05-23 23:45:09 +02:00
parent 52a39c9ede
commit 3bdb6c7622
2 changed files with 24 additions and 9 deletions

View File

@ -1,17 +1,17 @@
"use client"; "use client";
import { FormEvent } from "react"; import { FormEvent } from "react";
import { useFormState } from "react-dom";
import { allProducers, insertWine } from "~/server/db"; import { allProducers, insertWine } from "~/server/db";
export default function CreateWine() { const initialState = {
function handleSubmit(event: FormEvent<HTMLFormElement>) { message: null,
event.preventDefault(); };
const formData = new FormData(event.currentTarget); export default function CreateWineForm() {
const response = insertWine(formData); const [state, formAction] = useFormState(insertWine, { message: "" });
}
return ( return (
<form onSubmit={handleSubmit}> <form action={}>
<select name="producer" id="producer"> <select name="producer" id="producer">
{allProducers.map((producer) => ( {allProducers.map((producer) => (
<option key={producer.id} value={producer.id}> <option key={producer.id} value={producer.id}>
@ -19,6 +19,8 @@ export default function CreateWine() {
</option> </option>
))} ))}
</select> </select>
<input type="text" name="name" id="name" />
<input type="datetime-local" name="createdAt" id="createdAt" />
</form> </form>
); );
} }

View File

@ -7,9 +7,22 @@ export const db = drizzle(sql, { schema });
export const allWines = await db.query.wines.findMany(); export const allWines = await db.query.wines.findMany();
type NewWine = typeof schema.wines.$inferInsert; 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) => { export const insertWine = async (wine: NewWine) => {
return db.insert(schema.wines).values(wine).returning(); return db.insert(schema.wines).values(wine);
} }
export const allProducers = await db.query.producers.findMany(); export const allProducers = await db.query.producers.findMany();