113 lines
4.2 KiB
TypeScript
113 lines
4.2 KiB
TypeScript
import { CirclePlus } from "lucide-react";
|
|
import React from "react";
|
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { useForm } from "react-hook-form";
|
|
import { z } from "zod";
|
|
import { Button } from "~/components/ui/button";
|
|
import {
|
|
Dialog,
|
|
DialogClose,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
} from "~/components/ui/dialog";
|
|
import server from "~/lib/api/client/server";
|
|
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "./ui/form";
|
|
import { IconUploadField } from "./ui/icon-upload-field";
|
|
import { Input } from "./ui/input";
|
|
|
|
const schema = z.object({
|
|
name: z.string().min(1).max(32),
|
|
icon: z.instanceof(File).optional(),
|
|
});
|
|
|
|
export function CreateServerButton() {
|
|
const [open, setOpen] = React.useState(false);
|
|
|
|
let form = useForm<z.infer<typeof schema>>({
|
|
resolver: zodResolver(schema),
|
|
})
|
|
|
|
function onOpenChange(openState: boolean) {
|
|
setOpen(openState)
|
|
|
|
if (!openState) {
|
|
form.reset()
|
|
}
|
|
}
|
|
|
|
async function onSubmit(values: z.infer<typeof schema>) {
|
|
const response = await server.create(values)
|
|
|
|
onOpenChange(false)
|
|
}
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogTrigger asChild>
|
|
<Button variant="secondary" size="none">
|
|
<CirclePlus className="size-8 m-2" />
|
|
</Button>
|
|
</DialogTrigger>
|
|
<DialogContent className="sm:max-w-md">
|
|
<DialogHeader>
|
|
<DialogTitle>Create server</DialogTitle>
|
|
<DialogDescription>
|
|
Give your server a name and choose a server icon.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
|
<FormField
|
|
control={form.control}
|
|
name="icon"
|
|
render={({ field, fieldState }) => (
|
|
<FormItem>
|
|
<FormLabel required={!schema.shape.icon.isOptional()}>Icon</FormLabel>
|
|
<FormControl>
|
|
<div className="flex flex-col items-center justify-center">
|
|
<IconUploadField
|
|
field={field}
|
|
error={fieldState.error}
|
|
/>
|
|
</div>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="name"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel required={!schema.shape.name.isOptional()}>Name</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<DialogFooter className=" justify-between">
|
|
<DialogClose asChild>
|
|
<Button type="button" variant="secondary">
|
|
Close
|
|
</Button>
|
|
</DialogClose>
|
|
<Button type="submit" disabled={form.formState.isSubmitting}>
|
|
{form.formState.isSubmitting ? "Creating..." : "Create"}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</Form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
} |