From 61d956b9cd93b67ff7a2136cc0d7bb47793d528f Mon Sep 17 00:00:00 2001 From: andrzej Date: Wed, 26 Jun 2024 19:32:18 +0200 Subject: [PATCH] implement create pubs popup --- src/app/lib/create.ts | 20 +++++++++++ src/app/publication/create.tsx | 30 ++++++++++++++++ src/app/publication/create/page.tsx | 40 --------------------- src/app/publication/page.tsx | 10 ++++-- src/app/story/create.tsx | 7 ++-- src/app/ui/forms/genrePicker.tsx | 55 ++++++++++++++++++++--------- src/app/ui/forms/pub.tsx | 34 ++---------------- src/app/ui/forms/story.tsx | 40 +++------------------ src/app/ui/tables/data-table.tsx | 13 +++---- 9 files changed, 113 insertions(+), 136 deletions(-) create mode 100644 src/app/publication/create.tsx delete mode 100644 src/app/publication/create/page.tsx diff --git a/src/app/lib/create.ts b/src/app/lib/create.ts index a6c669c..63cf474 100644 --- a/src/app/lib/create.ts +++ b/src/app/lib/create.ts @@ -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") +} diff --git a/src/app/publication/create.tsx b/src/app/publication/create.tsx new file mode 100644 index 0000000..d3aa5a9 --- /dev/null +++ b/src/app/publication/create.tsx @@ -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 ( + + + + + + + + New publication + Create an entry for a new publication i.e. a place you intend to submit stories to. + + + + + + + + + ) +} diff --git a/src/app/publication/create/page.tsx b/src/app/publication/create/page.tsx deleted file mode 100644 index baffa05..0000000 --- a/src/app/publication/create/page.tsx +++ /dev/null @@ -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 ( - - New publication - - - Create a new entry for a publication i.e. a place you intend to submit to. - - - - - ) -} diff --git a/src/app/publication/page.tsx b/src/app/publication/page.tsx index 0b68521..d945b33 100644 --- a/src/app/publication/page.tsx +++ b/src/app/publication/page.tsx @@ -1,15 +1,21 @@ import { Genre, Pub } from "@prisma/client"; -import { getPubsWithGenres } from "app/lib/get"; +import { getGenres, getPubsWithGenres } from "app/lib/get"; import { columns } from "./columns"; import { DataTable } from "app/ui/tables/data-table"; +import CreatePubDialog from "./create"; export type PubsWithGenres = Pub & { genres: Array } + + export default async function Page() { + const genres = await getGenres() const pubs = await getPubsWithGenres() return (
- + + +
) diff --git a/src/app/story/create.tsx b/src/app/story/create.tsx index 178056e..49106d5 100644 --- a/src/app/story/create.tsx +++ b/src/app/story/create.tsx @@ -7,9 +7,8 @@ import { Genre } from "@prisma/client"; import StoryForm from "app/ui/forms/story"; -export default function CreateStoryDialog({ genres }: ComponentProps<"div"> & { genres: Array }) { +export default function CreateStoryDialog({ genres }: ComponentProps<"div"> & { genres: Genre[] }) { - console.log(genres) return ( @@ -21,7 +20,9 @@ export default function CreateStoryDialog({ genres }: ComponentProps<"div"> & { Create an entry for a new story i.e. a thing you intend to submit for publication. - + + + diff --git a/src/app/ui/forms/genrePicker.tsx b/src/app/ui/forms/genrePicker.tsx index fd3deaa..c34bcac 100644 --- a/src/app/ui/forms/genrePicker.tsx +++ b/src/app/ui/forms/genrePicker.tsx @@ -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 { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" +import { Checkbox } from "@/components/ui/checkbox" import { cn } from "@/lib/utils" import GenreCheckbox from "./genreCheckbox" export default function GenrePicker({ genres, form }) { return ( - ( - - Genres - + + ( + + Genres - - - - )} - /> + + + )} + /> + ) } diff --git a/src/app/ui/forms/pub.tsx b/src/app/ui/forms/pub.tsx index d5c8931..3709d16 100644 --- a/src/app/ui/forms/pub.tsx +++ b/src/app/ui/forms/pub.tsx @@ -25,6 +25,7 @@ import GenreCheckbox from "./genreCheckbox" import { randomPublicationTitle } from "app/lib/shortStoryTitleGenerator" import { ComponentProps } from "react" import { Genre } from "@prisma/client" +import GenrePicker from "./genrePicker" const formSchema = z.object({ title: z.string().min(2).max(50), @@ -79,7 +80,7 @@ export default function PubForm({ genres, createPub, className }: ComponentProps return (
- +
- ( - - Genres - - - - {genres.map((item) => ( - { - return ( - - ) - }} - /> - ))} - - - - - - )} - /> - +
-
diff --git a/src/app/ui/forms/story.tsx b/src/app/ui/forms/story.tsx index f6d5640..60e04cd 100644 --- a/src/app/ui/forms/story.tsx +++ b/src/app/ui/forms/story.tsx @@ -20,12 +20,11 @@ import { Popover, PopoverContent, } from "@/components/ui/popover" -import GenresTrigger from "./genresTrigger" -import GenreCheckbox from "./genreCheckbox" import { ComponentProps } from "react" import { Genre } from "@prisma/client" import { randomStoryTitle } from "app/lib/shortStoryTitleGenerator" import { usePathname } from "next/navigation" +import GenrePicker from "./genrePicker" const formSchema = z.object({ title: z.string().min(2).max(50), @@ -73,12 +72,10 @@ export default function StoryForm({ genres, createStory, className }: ComponentP console.log(JSON.stringify(errors)) } - const pathname = usePathname() - return (
- + - - ( - - Genres - - - - {genres.map((item) => ( - { - return ( - - ) - }} - /> - ))} - - - - - - )} + - diff --git a/src/app/ui/tables/data-table.tsx b/src/app/ui/tables/data-table.tsx index 7fa06bc..89929ae 100644 --- a/src/app/ui/tables/data-table.tsx +++ b/src/app/ui/tables/data-table.tsx @@ -9,7 +9,7 @@ import { DropdownMenuRadioGroup } from "@/components/ui/dropdown-menu" import { Input } from "@/components/ui/input" -import { useState } from "react" +import { Component, ComponentProps, useState } from "react" import { ColumnDef, flexRender, @@ -33,8 +33,6 @@ import { } from "@/components/ui/table" import { EyeIcon } from "lucide-react" import { usePathname } from "next/navigation" -import { useRouter } from "next/navigation" -import Link from "next/link" interface DataTableProps { columns: ColumnDef[] @@ -45,8 +43,8 @@ interface DataTableProps { export function DataTable({ columns, data, - type -}: DataTableProps & { type: "publication" | "submission" | "story" | "genre" | "response" }) { + children +}: DataTableProps & ComponentProps<"div"> & { type: "publication" | "submission" | "story" | "genre" | "response" }) { //STATE const [sorting, setSorting] = useState([]) const [columnFilters, setColumnFilters] = useState( @@ -107,9 +105,8 @@ export function DataTable({ />
- - - + {children} +