|
| 1 | +'use client'; |
| 2 | +import { Button } from "@/components/ui/button"; |
| 3 | +import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"; |
| 4 | +import { DropdownMenuItem } from "@/components/ui/dropdown-menu"; |
| 5 | +import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; |
| 6 | +import { Input } from "@/components/ui/input"; |
| 7 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 8 | +import { PlusCircledIcon } from "@radix-ui/react-icons"; |
| 9 | +import { useForm } from "react-hook-form"; |
| 10 | +import { z } from "zod"; |
| 11 | + |
| 12 | +const formSchema = z.object({ |
| 13 | + name: z.string().min(2).max(40), |
| 14 | +}); |
| 15 | + |
| 16 | + |
| 17 | +interface OrgCreationDialogProps { |
| 18 | + onSubmit: (data: z.infer<typeof formSchema>) => void; |
| 19 | + isOpen: boolean; |
| 20 | + onOpenChange: (isOpen: boolean) => void; |
| 21 | +} |
| 22 | + |
| 23 | +export const OrgCreationDialog = ({ |
| 24 | + onSubmit, |
| 25 | + isOpen, |
| 26 | + onOpenChange, |
| 27 | +}: OrgCreationDialogProps) => { |
| 28 | + const form = useForm<z.infer<typeof formSchema>>({ |
| 29 | + resolver: zodResolver(formSchema), |
| 30 | + defaultValues: { |
| 31 | + name: "", |
| 32 | + }, |
| 33 | + }); |
| 34 | + |
| 35 | + return ( |
| 36 | + <Dialog |
| 37 | + open={isOpen} |
| 38 | + onOpenChange={onOpenChange} |
| 39 | + > |
| 40 | + <DialogTrigger asChild> |
| 41 | + <DropdownMenuItem |
| 42 | + onSelect={(e) => e.preventDefault()} |
| 43 | + > |
| 44 | + <Button |
| 45 | + variant="ghost" |
| 46 | + size="default" |
| 47 | + className="w-full justify-start gap-1.5 p-0" |
| 48 | + > |
| 49 | + <PlusCircledIcon className="h-5 w-5 text-muted-foreground" /> |
| 50 | + Create organization |
| 51 | + </Button> |
| 52 | + </DropdownMenuItem> |
| 53 | + </DialogTrigger> |
| 54 | + <DialogContent> |
| 55 | + <DialogHeader> |
| 56 | + <DialogTitle>Create an organization</DialogTitle> |
| 57 | + <DialogDescription>Collaborate with</DialogDescription> |
| 58 | + </DialogHeader> |
| 59 | + <Form {...form}> |
| 60 | + <form onSubmit={form.handleSubmit(onSubmit)}> |
| 61 | + <FormField |
| 62 | + control={form.control} |
| 63 | + name="name" |
| 64 | + render={({ field }) => ( |
| 65 | + <FormItem> |
| 66 | + <FormLabel>Organization name</FormLabel> |
| 67 | + <FormControl> |
| 68 | + <Input {...field} /> |
| 69 | + </FormControl> |
| 70 | + <FormMessage /> |
| 71 | + </FormItem> |
| 72 | + )} |
| 73 | + /> |
| 74 | + <Button className="mt-5" type="submit">Submit</Button> |
| 75 | + </form> |
| 76 | + </Form> |
| 77 | + </DialogContent> |
| 78 | + </Dialog> |
| 79 | + ) |
| 80 | +} |
0 commit comments