implement create pubs popup
This commit is contained in:
parent
527b0d2aac
commit
61d956b9cd
|
@ -26,4 +26,24 @@ export async function createStory(data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export 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)
|
||||||
|
revalidatePath("/publication")
|
||||||
|
redirect("/publication")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
"use client"
|
||||||
|
import { Dialog, DialogHeader, DialogTrigger, DialogContent, DialogClose, DialogTitle, DialogFooter, DialogDescription } from "@/components/ui/dialog";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { ComponentProps } from "react";
|
||||||
|
import { Genre } from "@prisma/client";
|
||||||
|
import { createPub } from "app/lib/create";
|
||||||
|
import PubForm from "app/ui/forms/pub";
|
||||||
|
|
||||||
|
export default function CreatePubDialog({ genres }: ComponentProps<"div"> & { genres: Genre[] }) {
|
||||||
|
|
||||||
|
return (
|
||||||
|
|
||||||
|
<Dialog>
|
||||||
|
<DialogTrigger asChild>
|
||||||
|
<Button>Create new Publication</Button>
|
||||||
|
</DialogTrigger>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>New publication</DialogTitle>
|
||||||
|
<DialogDescription>Create an entry for a new publication i.e. a place you intend to submit stories to.</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<PubForm createPub={createPub} genres={genres} />
|
||||||
|
<DialogFooter>
|
||||||
|
<Button form="pubform">Submit</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
)
|
||||||
|
}
|
|
@ -1,40 +0,0 @@
|
||||||
import PubForm from "app/ui/forms/pub";
|
|
||||||
import { getGenres } from "app/lib/get";
|
|
||||||
import prisma from "app/lib/db";
|
|
||||||
import { CreateContainer, CreateContainerContent, CreateContainerDescription, CreateContainerHeader } from "app/ui/createContainer";
|
|
||||||
import { revalidatePath } from "next/cache";
|
|
||||||
import { redirect } from "next/navigation";
|
|
||||||
export default async function Page() {
|
|
||||||
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)
|
|
||||||
revalidatePath("/publication")
|
|
||||||
redirect("/publication")
|
|
||||||
}
|
|
||||||
const genres = await getGenres()
|
|
||||||
return (
|
|
||||||
<CreateContainer>
|
|
||||||
<CreateContainerHeader>New publication</CreateContainerHeader>
|
|
||||||
<CreateContainerContent>
|
|
||||||
<CreateContainerDescription>
|
|
||||||
Create a new entry for a publication i.e. a place you intend to submit to.
|
|
||||||
</CreateContainerDescription>
|
|
||||||
<PubForm genres={genres} createPub={createPub} className="mt-6" />
|
|
||||||
</CreateContainerContent>
|
|
||||||
</CreateContainer>
|
|
||||||
)
|
|
||||||
}
|
|
|
@ -1,15 +1,21 @@
|
||||||
import { Genre, Pub } from "@prisma/client";
|
import { Genre, Pub } from "@prisma/client";
|
||||||
import { getPubsWithGenres } from "app/lib/get";
|
import { getGenres, getPubsWithGenres } from "app/lib/get";
|
||||||
import { columns } from "./columns";
|
import { columns } from "./columns";
|
||||||
import { DataTable } from "app/ui/tables/data-table";
|
import { DataTable } from "app/ui/tables/data-table";
|
||||||
|
import CreatePubDialog from "./create";
|
||||||
|
|
||||||
export type PubsWithGenres = Pub & { genres: Array<Genre> }
|
export type PubsWithGenres = Pub & { genres: Array<Genre> }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default async function Page() {
|
export default async function Page() {
|
||||||
|
const genres = await getGenres()
|
||||||
const pubs = await getPubsWithGenres()
|
const pubs = await getPubsWithGenres()
|
||||||
return (
|
return (
|
||||||
<div className="container mx-auto">
|
<div className="container mx-auto">
|
||||||
<DataTable data={pubs} columns={columns} type="publication" />
|
<DataTable data={pubs} columns={columns} type="publication">
|
||||||
|
<CreatePubDialog genres={genres} />
|
||||||
|
</DataTable>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
@ -7,9 +7,8 @@ import { Genre } from "@prisma/client";
|
||||||
import StoryForm from "app/ui/forms/story";
|
import StoryForm from "app/ui/forms/story";
|
||||||
|
|
||||||
|
|
||||||
export default function CreateStoryDialog({ genres }: ComponentProps<"div"> & { genres: Array<Genre> }) {
|
export default function CreateStoryDialog({ genres }: ComponentProps<"div"> & { genres: Genre[] }) {
|
||||||
|
|
||||||
console.log(genres)
|
|
||||||
return (
|
return (
|
||||||
<Dialog>
|
<Dialog>
|
||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>
|
||||||
|
@ -21,7 +20,9 @@ export default function CreateStoryDialog({ genres }: ComponentProps<"div"> & {
|
||||||
<DialogDescription>Create an entry for a new story i.e. a thing you intend to submit for publication.</DialogDescription>
|
<DialogDescription>Create an entry for a new story i.e. a thing you intend to submit for publication.</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<StoryForm createStory={createStory} genres={genres} />
|
<StoryForm createStory={createStory} genres={genres} />
|
||||||
|
<DialogFooter>
|
||||||
|
<Button form="storyform">Submit</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
|
|
|
@ -1,19 +1,20 @@
|
||||||
import { FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"
|
import { FormField, FormItem, FormLabel, FormMessage, FormControl } from "@/components/ui/form"
|
||||||
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"
|
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"
|
||||||
import { Button } from "@/components/ui/button"
|
import { Button } from "@/components/ui/button"
|
||||||
import { Badge } from "@/components/ui/badge"
|
import { Badge } from "@/components/ui/badge"
|
||||||
|
import { Checkbox } from "@/components/ui/checkbox"
|
||||||
import { cn } from "@/lib/utils"
|
import { cn } from "@/lib/utils"
|
||||||
import GenreCheckbox from "./genreCheckbox"
|
import GenreCheckbox from "./genreCheckbox"
|
||||||
|
|
||||||
export default function GenrePicker({ genres, form }) {
|
export default function GenrePicker({ genres, form }) {
|
||||||
return (
|
return (
|
||||||
|
<Popover modal={true}>
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="genres"
|
name="genres"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem className="flex flex-col">
|
<FormItem className="flex flex-col">
|
||||||
<FormLabel className="h-5">Genres</FormLabel>
|
<FormLabel className="h-5">Genres</FormLabel>
|
||||||
<Popover modal={true}>
|
|
||||||
<PopoverTrigger asChild>
|
<PopoverTrigger asChild>
|
||||||
<Button
|
<Button
|
||||||
variant={"outline"}
|
variant={"outline"}
|
||||||
|
@ -23,7 +24,7 @@ export default function GenrePicker({ genres, form }) {
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{field.value.length !== 0 ? (
|
{field.value.length !== 0 ? (
|
||||||
field.value.map((e, i) => (<Badge>{genres.find(f => e === f.id).name}</Badge>))
|
field.value.map((e, i) => (<Badge key={i}>{genres.find(f => e === f.id).name}</Badge>))
|
||||||
) : (
|
) : (
|
||||||
<p>Select</p>
|
<p>Select</p>
|
||||||
)}
|
)}
|
||||||
|
@ -34,25 +35,45 @@ export default function GenrePicker({ genres, form }) {
|
||||||
|
|
||||||
<PopoverContent align="start">
|
<PopoverContent align="start">
|
||||||
{genres.map((item) => (
|
{genres.map((item) => (
|
||||||
<FormField
|
< FormField
|
||||||
|
|
||||||
key={item.id}
|
key={item.id}
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="genres"
|
name="genres"
|
||||||
render={({ field }) => {
|
render={({ field }) => {
|
||||||
return (
|
return (
|
||||||
<GenreCheckbox field={field} item={item} />
|
<FormItem
|
||||||
|
key={item.id}
|
||||||
|
className="flex flex-row items-start space-x-3 space-y-0"
|
||||||
|
>
|
||||||
|
<FormControl>
|
||||||
|
<Checkbox
|
||||||
|
checked={field.value?.includes(item.id)}
|
||||||
|
onCheckedChange={(checked) => {
|
||||||
|
return checked
|
||||||
|
? field.onChange([...field.value, item.id])
|
||||||
|
: field.onChange(
|
||||||
|
field.value?.filter(
|
||||||
|
(value) => value !== item.id
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormLabel className="text-sm font-normal">
|
||||||
|
{item.name}
|
||||||
|
</FormLabel>
|
||||||
|
</FormItem>
|
||||||
)
|
)
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
<Button variant="link" className="p-0" onClick={() => form.setValue("genres", [])}>Clear</Button>
|
<Button variant="link" className="p-0" onClick={() => form.setValue("genres", [])}>Clear</Button>
|
||||||
</PopoverContent>
|
</PopoverContent>
|
||||||
</Popover>
|
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
</Popover>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ import GenreCheckbox from "./genreCheckbox"
|
||||||
import { randomPublicationTitle } from "app/lib/shortStoryTitleGenerator"
|
import { randomPublicationTitle } from "app/lib/shortStoryTitleGenerator"
|
||||||
import { ComponentProps } from "react"
|
import { ComponentProps } from "react"
|
||||||
import { Genre } from "@prisma/client"
|
import { Genre } from "@prisma/client"
|
||||||
|
import GenrePicker from "./genrePicker"
|
||||||
|
|
||||||
const formSchema = z.object({
|
const formSchema = z.object({
|
||||||
title: z.string().min(2).max(50),
|
title: z.string().min(2).max(50),
|
||||||
|
@ -79,7 +80,7 @@ export default function PubForm({ genres, createPub, className }: ComponentProps
|
||||||
return (
|
return (
|
||||||
<div className={className}>
|
<div className={className}>
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit, onErrors)} className="space-y-8">
|
<form id="pubform" onSubmit={form.handleSubmit(onSubmit, onErrors)} className="space-y-8">
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="title"
|
name="title"
|
||||||
|
@ -109,35 +110,7 @@ export default function PubForm({ genres, createPub, className }: ComponentProps
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="inline-flex flex-wrap w-full gap-x-16 gap-y-8 max-w-full h-fit">
|
<div className="inline-flex flex-wrap w-full gap-x-16 gap-y-8 max-w-full h-fit">
|
||||||
<FormField
|
<GenrePicker genres={genres} form={form} />
|
||||||
control={form.control}
|
|
||||||
name="genres"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem className="flex flex-col flex-wrap">
|
|
||||||
<FormLabel className="h-5">Genres</FormLabel>
|
|
||||||
<Popover>
|
|
||||||
<GenresTrigger value={field.value} genres={genres} />
|
|
||||||
<PopoverContent align="start">
|
|
||||||
{genres.map((item) => (
|
|
||||||
<FormField
|
|
||||||
key={item.id}
|
|
||||||
control={form.control}
|
|
||||||
name="genres"
|
|
||||||
render={({ field }) => {
|
|
||||||
return (
|
|
||||||
<GenreCheckbox field={field} item={item} />
|
|
||||||
)
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
<Button variant="link" className="p-0" onClick={() => form.setValue("genres", [])}>Clear</Button>
|
|
||||||
</PopoverContent>
|
|
||||||
</Popover>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
|
|
||||||
|
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
|
@ -154,7 +127,6 @@ export default function PubForm({ genres, createPub, className }: ComponentProps
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Button type="submit">Submit</Button>
|
|
||||||
</form>
|
</form>
|
||||||
</Form>
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -20,12 +20,11 @@ import {
|
||||||
Popover,
|
Popover,
|
||||||
PopoverContent,
|
PopoverContent,
|
||||||
} from "@/components/ui/popover"
|
} from "@/components/ui/popover"
|
||||||
import GenresTrigger from "./genresTrigger"
|
|
||||||
import GenreCheckbox from "./genreCheckbox"
|
|
||||||
import { ComponentProps } from "react"
|
import { ComponentProps } from "react"
|
||||||
import { Genre } from "@prisma/client"
|
import { Genre } from "@prisma/client"
|
||||||
import { randomStoryTitle } from "app/lib/shortStoryTitleGenerator"
|
import { randomStoryTitle } from "app/lib/shortStoryTitleGenerator"
|
||||||
import { usePathname } from "next/navigation"
|
import { usePathname } from "next/navigation"
|
||||||
|
import GenrePicker from "./genrePicker"
|
||||||
|
|
||||||
const formSchema = z.object({
|
const formSchema = z.object({
|
||||||
title: z.string().min(2).max(50),
|
title: z.string().min(2).max(50),
|
||||||
|
@ -73,12 +72,10 @@ export default function StoryForm({ genres, createStory, className }: ComponentP
|
||||||
console.log(JSON.stringify(errors))
|
console.log(JSON.stringify(errors))
|
||||||
}
|
}
|
||||||
|
|
||||||
const pathname = usePathname()
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={className}>
|
<div className={className}>
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit, onErrors)} className="space-y-8">
|
<form onSubmit={form.handleSubmit(onSubmit, onErrors)} className="space-y-8" id="storyform">
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="title"
|
name="title"
|
||||||
|
@ -95,35 +92,9 @@ export default function StoryForm({ genres, createStory, className }: ComponentP
|
||||||
|
|
||||||
<div className="inline-flex flex-wrap w-full gap-x-16 gap-y-8 items-baseline max-w-full">
|
<div className="inline-flex flex-wrap w-full gap-x-16 gap-y-8 items-baseline max-w-full">
|
||||||
|
|
||||||
|
<GenrePicker
|
||||||
<FormField
|
genres={genres}
|
||||||
control={form.control}
|
form={form}
|
||||||
name="genres"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem className="flex flex-col">
|
|
||||||
<FormLabel className="h-5">Genres</FormLabel>
|
|
||||||
<Popover>
|
|
||||||
<GenresTrigger value={field.value} genres={genres} />
|
|
||||||
<PopoverContent align="start">
|
|
||||||
{genres.map((item) => (
|
|
||||||
<FormField
|
|
||||||
|
|
||||||
key={item.id}
|
|
||||||
control={form.control}
|
|
||||||
name="genres"
|
|
||||||
render={({ field }) => {
|
|
||||||
return (
|
|
||||||
<GenreCheckbox field={field} item={item} />
|
|
||||||
)
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
<Button variant="link" className="p-0" onClick={() => form.setValue("genres", [])}>Clear</Button>
|
|
||||||
</PopoverContent>
|
|
||||||
</Popover>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<FormField
|
<FormField
|
||||||
|
@ -142,7 +113,6 @@ export default function StoryForm({ genres, createStory, className }: ComponentP
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Button type="submit" className="">Submit</Button>
|
|
||||||
|
|
||||||
|
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -9,7 +9,7 @@ import {
|
||||||
DropdownMenuRadioGroup
|
DropdownMenuRadioGroup
|
||||||
} from "@/components/ui/dropdown-menu"
|
} from "@/components/ui/dropdown-menu"
|
||||||
import { Input } from "@/components/ui/input"
|
import { Input } from "@/components/ui/input"
|
||||||
import { useState } from "react"
|
import { Component, ComponentProps, useState } from "react"
|
||||||
import {
|
import {
|
||||||
ColumnDef,
|
ColumnDef,
|
||||||
flexRender,
|
flexRender,
|
||||||
|
@ -33,8 +33,6 @@ import {
|
||||||
} from "@/components/ui/table"
|
} from "@/components/ui/table"
|
||||||
import { EyeIcon } from "lucide-react"
|
import { EyeIcon } from "lucide-react"
|
||||||
import { usePathname } from "next/navigation"
|
import { usePathname } from "next/navigation"
|
||||||
import { useRouter } from "next/navigation"
|
|
||||||
import Link from "next/link"
|
|
||||||
|
|
||||||
interface DataTableProps<TData, TValue> {
|
interface DataTableProps<TData, TValue> {
|
||||||
columns: ColumnDef<TData, TValue>[]
|
columns: ColumnDef<TData, TValue>[]
|
||||||
|
@ -45,8 +43,8 @@ interface DataTableProps<TData, TValue> {
|
||||||
export function DataTable<TData, TValue>({
|
export function DataTable<TData, TValue>({
|
||||||
columns,
|
columns,
|
||||||
data,
|
data,
|
||||||
type
|
children
|
||||||
}: DataTableProps<TData, TValue> & { type: "publication" | "submission" | "story" | "genre" | "response" }) {
|
}: DataTableProps<TData, TValue> & ComponentProps<"div"> & { type: "publication" | "submission" | "story" | "genre" | "response" }) {
|
||||||
//STATE
|
//STATE
|
||||||
const [sorting, setSorting] = useState<SortingState>([])
|
const [sorting, setSorting] = useState<SortingState>([])
|
||||||
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>(
|
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>(
|
||||||
|
@ -107,9 +105,8 @@ export function DataTable<TData, TValue>({
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Link href={pathname + "/create"}>
|
{children}
|
||||||
<Button>Create new {type}</Button>
|
|
||||||
</Link>
|
|
||||||
|
|
||||||
<DropdownMenu>
|
<DropdownMenu>
|
||||||
<DropdownMenuTrigger asChild>
|
<DropdownMenuTrigger asChild>
|
||||||
|
|
Loading…
Reference in New Issue