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