add create server actions
This commit is contained in:
parent
0d25299ba6
commit
f1422b5b24
|
@ -1,6 +1,25 @@
|
|||
import PubForm from "app/ui/forms/pub";
|
||||
import { getGenres } from "app/lib/get";
|
||||
import prisma from "app/lib/db";
|
||||
export default async function Page() {
|
||||
const genres = await getGenres()
|
||||
return <PubForm genres={genres} />
|
||||
async function createPub(data) {
|
||||
"use server"
|
||||
const genresArray = data.genres.map(e => { return { id: e } })
|
||||
const res = await prisma.pub.create({
|
||||
data: {
|
||||
title: data.title,
|
||||
link: data.link,
|
||||
query_after_days: data.query_after_days
|
||||
}
|
||||
})
|
||||
console.log(res)
|
||||
const genresRes = await prisma.pub.update({
|
||||
where: { id: res.id },
|
||||
data:
|
||||
{ genres: { set: genresArray } }
|
||||
})
|
||||
console.log(genresRes)
|
||||
}
|
||||
const genres = await getGenres()
|
||||
return <PubForm genres={genres} createPub={createPub} />
|
||||
}
|
||||
|
|
|
@ -2,10 +2,16 @@
|
|||
import { getPubs, getResponses, getStories } from "app/lib/get";
|
||||
import SubmissionForm from "app/ui/forms/sub";
|
||||
import { SelectForm } from "app/ui/forms/selectDemo";
|
||||
import prisma from "app/lib/db";
|
||||
|
||||
export default async function Page() {
|
||||
const stories = await getStories()
|
||||
const pubs = await getPubs()
|
||||
const responses = await getResponses()
|
||||
return <SubmissionForm stories={stories} pubs={pubs} responses={responses} />
|
||||
async function createSub(data) {
|
||||
"use server"
|
||||
const res = await prisma.sub.create({ data })
|
||||
console.log(res)
|
||||
}
|
||||
return <SubmissionForm stories={stories} pubs={pubs} responses={responses} createSub={createSub} />
|
||||
}
|
||||
|
|
|
@ -815,15 +815,15 @@ body {
|
|||
width: auto;
|
||||
}
|
||||
|
||||
.w-full {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.w-fit {
|
||||
width: -moz-fit-content;
|
||||
width: fit-content;
|
||||
}
|
||||
|
||||
.w-full {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.min-w-\[8rem\] {
|
||||
min-width: 8rem;
|
||||
}
|
||||
|
@ -837,19 +837,14 @@ body {
|
|||
min-width: fit-content;
|
||||
}
|
||||
|
||||
.max-w-sm {
|
||||
max-width: 24rem;
|
||||
}
|
||||
|
||||
.max-w-fit {
|
||||
max-width: -moz-fit-content;
|
||||
max-width: fit-content;
|
||||
}
|
||||
|
||||
.max-w-screen-sm {
|
||||
max-width: 640px;
|
||||
}
|
||||
|
||||
.max-w-sm {
|
||||
max-width: 24rem;
|
||||
}
|
||||
|
||||
.shrink-0 {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
@ -862,6 +857,10 @@ body {
|
|||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
.transform {
|
||||
transform: translate(var(--tw-translate-x), var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y));
|
||||
}
|
||||
|
||||
.cursor-default {
|
||||
cursor: default;
|
||||
}
|
||||
|
@ -988,10 +987,6 @@ body {
|
|||
border-radius: calc(var(--radius) - 4px);
|
||||
}
|
||||
|
||||
.rounded-full {
|
||||
border-radius: 9999px;
|
||||
}
|
||||
|
||||
.border {
|
||||
border-width: 1px;
|
||||
}
|
||||
|
@ -1016,10 +1011,6 @@ body {
|
|||
border-color: hsl(var(--primary));
|
||||
}
|
||||
|
||||
.border-transparent {
|
||||
border-color: transparent;
|
||||
}
|
||||
|
||||
.bg-accent {
|
||||
background-color: hsl(var(--accent));
|
||||
}
|
||||
|
@ -1130,21 +1121,6 @@ body {
|
|||
padding-bottom: 1rem;
|
||||
}
|
||||
|
||||
.px-2\.5 {
|
||||
padding-left: 0.625rem;
|
||||
padding-right: 0.625rem;
|
||||
}
|
||||
|
||||
.py-0 {
|
||||
padding-top: 0px;
|
||||
padding-bottom: 0px;
|
||||
}
|
||||
|
||||
.py-0\.5 {
|
||||
padding-top: 0.125rem;
|
||||
padding-bottom: 0.125rem;
|
||||
}
|
||||
|
||||
.pl-3 {
|
||||
padding-left: 0.75rem;
|
||||
}
|
||||
|
@ -1424,14 +1400,6 @@ body {
|
|||
background-color: hsl(var(--secondary) / 0.8);
|
||||
}
|
||||
|
||||
.hover\:bg-destructive\/80:hover {
|
||||
background-color: hsl(var(--destructive) / 0.8);
|
||||
}
|
||||
|
||||
.hover\:bg-primary\/80:hover {
|
||||
background-color: hsl(var(--primary) / 0.8);
|
||||
}
|
||||
|
||||
.hover\:text-accent-foreground:hover {
|
||||
color: hsl(var(--accent-foreground));
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ const formSchema = z.object({
|
|||
genres: z.array(z.number()),
|
||||
})
|
||||
|
||||
export default function PubForm({ genres }) {
|
||||
export default function PubForm({ genres, createPub }) {
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: {
|
||||
|
@ -52,7 +52,7 @@ export default function PubForm({ genres }) {
|
|||
</pre>
|
||||
),
|
||||
})
|
||||
|
||||
createPub(values)
|
||||
console.log(values)
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,9 @@ import {
|
|||
} from "@/components/ui/popover"
|
||||
import GenresTrigger from "./genresTrigger"
|
||||
import GenreCheckbox from "./genreCheckbox"
|
||||
import { PopoverTrigger } from "@radix-ui/react-popover"
|
||||
import { GenrePicker } from "./genrePicker"
|
||||
import { useRef, useImperativeHandle } from "react"
|
||||
|
||||
const formSchema = z.object({
|
||||
title: z.string().min(2).max(50),
|
||||
|
@ -69,7 +72,9 @@ export default function StoryForm({ genres, createStory }) {
|
|||
console.log(JSON.stringify(errors))
|
||||
}
|
||||
|
||||
|
||||
const genrePickerRef = useRef<HTMLInputElement>(null)
|
||||
const { ref, ...rest } = form.register("genres")
|
||||
useImperativeHandle(ref, () => genrePickerRef.current)
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit, onErrors)} className="space-y-8">
|
||||
|
|
|
@ -36,13 +36,13 @@ import { useState } from "react"
|
|||
const FormSchema = z.object({
|
||||
storyId: z.coerce.number(),
|
||||
pubId: z.coerce.number(),
|
||||
submitted: z.date(),
|
||||
responded: z.date().optional(),
|
||||
submitted: z.date().transform((date) => date.toString()),
|
||||
responded: z.date().transform((date) => date.toString()).optional(),
|
||||
responseId: z.number()
|
||||
})
|
||||
|
||||
|
||||
export default function SubmissionForm({ stories, pubs, responses }) {
|
||||
export default function SubmissionForm({ stories, pubs, responses, createSub }) {
|
||||
const form = useForm<z.infer<typeof FormSchema>>({
|
||||
resolver: zodResolver(FormSchema),
|
||||
defaultValues: {
|
||||
|
@ -81,6 +81,7 @@ export default function SubmissionForm({ stories, pubs, responses }) {
|
|||
</pre>
|
||||
),
|
||||
})
|
||||
createSub(values)
|
||||
console.log(values)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue