"use client"; import { useToast } from "@/components/ui/use-toast"; import { useRouter } from "next/navigation"; import * as z from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { PlusIcon } from "@radix-ui/react-icons"; const createWorkspaceSchema = z.object({ name: z.string().min(2, { message: "Workspace name must be at least 2 characters.", }), description: z .string() .min(2, { message: "Workspace description must be at least 2 characters.", }) .optional(), }); export function CreateWorkspace() { const { toast } = useToast(); const router = useRouter(); const form = useForm>({ resolver: zodResolver(createWorkspaceSchema), defaultValues: { name: "", description: "", }, }); async function createWorkspace( values: z.infer ) { try { const res = await fetch("/w", { method: "POST", body: JSON.stringify(values), }); if (!res.ok) { const error = await res.json(); throw new Error(error.message); } const { workspace } = await res.json(); toast({ variant: "default", title: "Workspace created successfully!", description: `Workspace ${workspace.name} was created successfully.`, }); router.push(`/w/${workspace.id}`); router.refresh(); } catch (err: any) { console.error(err); toast({ variant: "destructive", title: "Uh oh! There was an error creating the workspace.", description: err.error, }); } } return ( Create Workspace
( Name This is your workspace name. )} /> ( Description This is your workspace description. )} />
); }