controlled create dialogs

This commit is contained in:
andrzej 2024-09-25 18:48:03 +02:00
parent 81b36d0c8c
commit 23dbda7135
6 changed files with 28 additions and 42 deletions

Binary file not shown.

View File

@ -6,12 +6,18 @@ import { Genre } from "@prisma/client";
import { createPub } from "app/lib/create"; import { createPub } from "app/lib/create";
import PubForm from "app/ui/forms/pub"; import PubForm from "app/ui/forms/pub";
import { Plus } from "lucide-react"; import { Plus } from "lucide-react";
import { useState } from "react";
export default function CreatePubDialog({ genres }: ComponentProps<"div"> & { genres: Genre[] }) { export default function CreatePubDialog({ genres }: ComponentProps<"div"> & { genres: Genre[] }) {
const [isOpen, setIsOpen] = useState(false)
function closeDialog() {
setIsOpen(false)
}
return ( return (
<Dialog> <Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild> <DialogTrigger asChild>
<Button> <Button>
<span className="hidden md:block">Create new publication</span> <span className="hidden md:block">Create new publication</span>
@ -23,11 +29,9 @@ export default function CreatePubDialog({ genres }: ComponentProps<"div"> & { ge
<DialogTitle>New publication</DialogTitle> <DialogTitle>New publication</DialogTitle>
<DialogDescription>Create an entry for a new publication i.e. a place you intend to submit stories to.</DialogDescription> <DialogDescription>Create an entry for a new publication i.e. a place you intend to submit stories to.</DialogDescription>
</DialogHeader> </DialogHeader>
<PubForm createPub={createPub} genres={genres} /> <PubForm createPub={createPub} genres={genres} closeDialog={closeDialog} />
<DialogFooter> <DialogFooter>
<DialogClose >
<Button form="pubform">Submit</Button> <Button form="pubform">Submit</Button>
</DialogClose>
</DialogFooter> </DialogFooter>
</DialogContent> </DialogContent>

View File

@ -11,6 +11,9 @@ import { Plus } from "lucide-react";
export default function CreateStoryDialog({ genres }: ComponentProps<"div"> & { genres: Genre[] }) { export default function CreateStoryDialog({ genres }: ComponentProps<"div"> & { genres: Genre[] }) {
const [isOpen, setIsOpen] = useState(false) const [isOpen, setIsOpen] = useState(false)
function closeDialog() {
setIsOpen(false)
}
return ( return (
<Dialog open={isOpen} onOpenChange={setIsOpen}> <Dialog open={isOpen} onOpenChange={setIsOpen}>
@ -25,12 +28,9 @@ export default function CreateStoryDialog({ genres }: ComponentProps<"div"> & {
<DialogTitle>New story</DialogTitle> <DialogTitle>New story</DialogTitle>
<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} className="" /> <StoryForm createStory={createStory} genres={genres} className="" closeDialog={closeDialog} />
<DialogFooter> <DialogFooter>
<DialogClose>
{/* TODO: pass setIsOpen to form as prop, to be handled post-verification */}
<Button form="storyform">Submit</Button> <Button form="storyform">Submit</Button>
</DialogClose>
</DialogFooter> </DialogFooter>
</DialogContent> </DialogContent>

View File

@ -1097,10 +1097,6 @@ body {
justify-content: space-between; justify-content: space-between;
} }
.justify-around {
justify-content: space-around;
}
.gap-1 { .gap-1 {
gap: 0.25rem; gap: 0.25rem;
} }
@ -1202,6 +1198,10 @@ body {
white-space: nowrap; white-space: nowrap;
} }
.rounded-3xl {
border-radius: 1.5rem;
}
.rounded-full { .rounded-full {
border-radius: 9999px; border-radius: 9999px;
} }
@ -1214,25 +1214,11 @@ body {
border-radius: calc(var(--radius) - 4px); border-radius: calc(var(--radius) - 4px);
} }
.rounded-3xl {
border-radius: 1.5rem;
}
.rounded-l-3xl {
border-top-left-radius: 1.5rem;
border-bottom-left-radius: 1.5rem;
}
.rounded-t-3xl { .rounded-t-3xl {
border-top-left-radius: 1.5rem; border-top-left-radius: 1.5rem;
border-top-right-radius: 1.5rem; border-top-right-radius: 1.5rem;
} }
.rounded-l {
border-top-left-radius: 0.25rem;
border-bottom-left-radius: 0.25rem;
}
.rounded-tl-3xl { .rounded-tl-3xl {
border-top-left-radius: 1.5rem; border-top-left-radius: 1.5rem;
} }

View File

@ -21,8 +21,9 @@ import { Genre } from "@prisma/client"
import GenrePicker from "./genrePicker" import GenrePicker from "./genrePicker"
import { pubSchema } from "./schemas" import { pubSchema } from "./schemas"
import { useRouter } from "next/navigation" import { useRouter } from "next/navigation"
import { Ban } from "lucide-react"
export default function PubForm({ genres, createPub, className }: ComponentProps<"div"> & { genres: Array<Genre>, createPub: (data: any) => void }) { export default function PubForm({ genres, createPub, className, closeDialog }: ComponentProps<"div"> & { genres: Array<Genre>, createPub: (data: any) => void, closeDialog: () => void }) {
const form = useForm<z.infer<typeof pubSchema>>({ const form = useForm<z.infer<typeof pubSchema>>({
resolver: zodResolver(pubSchema), resolver: zodResolver(pubSchema),
defaultValues: { defaultValues: {
@ -41,6 +42,7 @@ export default function PubForm({ genres, createPub, className }: ComponentProps
if (!res) throw new Error("something went wrong") if (!res) throw new Error("something went wrong")
toast({ title: "Successfully submitted:", description: values.title }) toast({ title: "Successfully submitted:", description: values.title })
router.refresh() router.refresh()
closeDialog()
} catch (error) { } catch (error) {
toast({ toast({
title: "Oh dear... ", title: "Oh dear... ",
@ -51,11 +53,8 @@ export default function PubForm({ genres, createPub, className }: ComponentProps
function onErrors(errors) { function onErrors(errors) {
toast({ toast({
title: "You have errors",
description: ( description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4"> <Ban />
<code className="text-white">{JSON.stringify(errors, null, 2)}</code>
</pre>
), ),
}) })
console.log(JSON.stringify(errors)) console.log(JSON.stringify(errors))

View File

@ -15,11 +15,12 @@ import {
import { Input } from "@/components/ui/input" import { Input } from "@/components/ui/input"
import { toast } from "@/components/ui/use-toast" import { toast } from "@/components/ui/use-toast"
import { ComponentProps } from "react" import { ComponentProps, SetStateAction } from "react"
import { Genre, Story } from "@prisma/client" import { Genre, Story } from "@prisma/client"
import { randomStoryTitle } from "app/lib/shortStoryTitleGenerator" import { randomStoryTitle } from "app/lib/shortStoryTitleGenerator"
import GenrePicker from "./genrePicker" import GenrePicker from "./genrePicker"
import { useRouter } from "next/navigation" import { useRouter } from "next/navigation"
import { Ban, Cross } from "lucide-react"
export const formSchema = z.object({ export const formSchema = z.object({
id: z.number().optional(), id: z.number().optional(),
@ -28,7 +29,7 @@ export const formSchema = z.object({
genres: z.array(z.number()) genres: z.array(z.number())
}) })
export default function StoryForm({ genres, createStory, className }: ComponentProps<"div"> & { genres: Array<Genre>, createStory: (data: any) => void, className: string }) { export default function StoryForm({ genres, createStory, className, closeDialog }: ComponentProps<"div"> & { genres: Array<Genre>, createStory: (data: any) => void, className: string, closeDialog: () => void }) {
const form = useForm<z.infer<typeof formSchema>>({ const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema), resolver: zodResolver(formSchema),
defaultValues: { defaultValues: {
@ -46,6 +47,7 @@ export default function StoryForm({ genres, createStory, className }: ComponentP
if (!res) throw new Error("something went wrong") if (!res) throw new Error("something went wrong")
toast({ title: "Sucessfully submitted:", description: values.title }) toast({ title: "Sucessfully submitted:", description: values.title })
router.refresh() router.refresh()
closeDialog()
} catch (error) { } catch (error) {
toast({ toast({
title: "Oh dear... ", title: "Oh dear... ",
@ -58,12 +60,7 @@ export default function StoryForm({ genres, createStory, className }: ComponentP
function onErrors(errors) { function onErrors(errors) {
toast({ toast({
title: "You have errors", description: (<Ban />)
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(errors, null, 2)}</code>
</pre>
),
}) })
console.log(JSON.stringify(errors)) console.log(JSON.stringify(errors))
} }
@ -100,7 +97,7 @@ export default function StoryForm({ genres, createStory, className }: ComponentP
<FormItem className="flex flex-col"> <FormItem className="flex flex-col">
<FormLabel className="h-5">Word count</FormLabel> <FormLabel className="h-5">Word count</FormLabel>
<FormControl> <FormControl>
<Input className=" w-24" type="number" step={500} min={1} {...field}></Input> <Input className=" w-24" type="number" step={500} {...field}></Input>
</FormControl> </FormControl>
<FormMessage /> <FormMessage />
</FormItem> </FormItem>