2023-10-08 02:33:48 -07:00
|
|
|
"use client";
|
|
|
|
|
2023-10-08 18:18:17 -07:00
|
|
|
import axios from "axios";
|
2023-10-08 02:33:48 -07:00
|
|
|
import * as z from "zod";
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
import { useForm } from "react-hook-form";
|
2023-10-08 17:25:08 -07:00
|
|
|
import { useEffect, useState } from "react";
|
2023-10-08 02:33:48 -07:00
|
|
|
|
|
|
|
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";
|
2023-10-08 17:25:08 -07:00
|
|
|
import { FileUpload } from "@/components/file-upload";
|
2023-10-08 18:18:17 -07:00
|
|
|
import { useRouter } from "next/navigation";
|
2023-10-08 02:33:48 -07:00
|
|
|
|
|
|
|
const formSchema = z.object({
|
|
|
|
name: z.string().min(1, {
|
|
|
|
message: "Server name is required",
|
|
|
|
}),
|
|
|
|
imageUrl: z.string().min(1, {
|
|
|
|
message: "Server image is required",
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2023-10-08 17:30:03 -07:00
|
|
|
export const InitialModal = () => {
|
2023-10-08 02:33:48 -07:00
|
|
|
const [isMounted, setIsMounted] = useState(false);
|
|
|
|
|
2023-10-08 18:18:17 -07:00
|
|
|
const router = useRouter();
|
|
|
|
|
2023-10-08 02:33:48 -07:00
|
|
|
useEffect(() => {
|
|
|
|
setIsMounted(true);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const form = useForm({
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
defaultValues: {
|
|
|
|
name: "",
|
|
|
|
imageUrl: "",
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const isLoading = form.formState.isSubmitting;
|
|
|
|
|
|
|
|
const onSubmit = async (values: z.infer<typeof formSchema>) => {
|
2023-10-08 18:18:17 -07:00
|
|
|
try {
|
|
|
|
await axios.post("/api/servers", values);
|
|
|
|
|
|
|
|
form.reset();
|
|
|
|
router.refresh();
|
|
|
|
window.location.reload();
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
2023-10-08 02:33:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isMounted) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-10-08 17:25:08 -07:00
|
|
|
|
2023-10-08 02:33:48 -07:00
|
|
|
return (
|
|
|
|
<Dialog open>
|
|
|
|
<DialogContent className="bg-white text-black p-0 overflow-hidden">
|
|
|
|
<DialogHeader className="pt-8 px-6">
|
|
|
|
<DialogTitle className="text-2xl text-center">
|
|
|
|
Customize your server
|
|
|
|
</DialogTitle>
|
|
|
|
<DialogDescription className="text-center text-zinc-500">
|
|
|
|
Give your server a personality with a name and an image. You can always change it later.
|
|
|
|
</DialogDescription>
|
|
|
|
</DialogHeader>
|
|
|
|
<Form {...form}>
|
|
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
|
|
|
|
<div className="space-y-8 px-6">
|
|
|
|
<div className="flex items-center justify-center text-center">
|
2023-10-08 17:25:08 -07:00
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="imageUrl"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem>
|
|
|
|
<FormControl>
|
|
|
|
<FileUpload
|
|
|
|
endpoint="serverImage"
|
|
|
|
value={field.value}
|
|
|
|
onChange={field.onChange}
|
|
|
|
/>
|
|
|
|
</FormControl>
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
2023-10-08 02:33:48 -07:00
|
|
|
</div>
|
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="name"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem>
|
|
|
|
<FormLabel
|
|
|
|
className="uppercase text-xs font-bold text-zinc-500 dark:text-secondary/70"
|
|
|
|
>
|
|
|
|
Server name
|
|
|
|
</FormLabel>
|
|
|
|
<FormControl>
|
|
|
|
<Input
|
|
|
|
disabled={isLoading}
|
|
|
|
className="bg-zinc-300/50 border-0
|
|
|
|
focus-visible:ring-0 text-black
|
|
|
|
focus-visible:ring-offset-0"
|
|
|
|
placeholder="Enter server name"
|
|
|
|
{...field}
|
|
|
|
/>
|
|
|
|
</FormControl>
|
|
|
|
<FormMessage/>
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<DialogFooter className="bg-gray-100 px-6 py-4">
|
|
|
|
<Button variant={"primary"} disabled={isLoading}>
|
|
|
|
Create
|
|
|
|
</Button>
|
|
|
|
</DialogFooter>
|
|
|
|
</form>
|
|
|
|
</Form>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
)
|
|
|
|
}
|