diff --git a/components/modals/create-server-modal.tsx b/components/modals/create-server-modal.tsx new file mode 100644 index 0000000..2796950 --- /dev/null +++ b/components/modals/create-server-modal.tsx @@ -0,0 +1,138 @@ +"use client"; + +import axios from "axios"; +import * as z from "zod"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useForm } from "react-hook-form"; + +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import{ + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { Button } from "@/components/ui/button"; +import { FileUpload } from "@/components/file-upload"; +import { useRouter } from "next/navigation"; +import { useModal } from "@/hooks/use-modal-store"; + +const formSchema = z.object({ + name: z.string().min(1, { + message: "Server name is required", + }), + imageUrl: z.string().min(1, { + message: "Server image is required", + }), +}); + +export const CreateServerModal = () => { + const { isOpen, onClose, type } = useModal(); + const router = useRouter(); + + const isModalOpen = isOpen && type === "createServer"; + + const form = useForm({ + resolver: zodResolver(formSchema), + defaultValues: { + name: "", + imageUrl: "", + } + }); + + const isLoading = form.formState.isSubmitting; + + const onSubmit = async (values: z.infer) => { + try { + await axios.post("/api/servers", values); + + form.reset(); + router.refresh(); + onClose(); + } catch (error) { + console.error(error); + } + } + + const handleClose = () => { + form.reset(); + onClose(); + } + + return ( + + + + + Customize your server + + + Give your server a personality with a name and an image. You can always change it later. + + +
+ +
+
+ ( + + + + + + )} + /> +
+ ( + + + Server name + + + + + + + )} + /> +
+ + + +
+ +
+
+ ) +} \ No newline at end of file