import { Button } from "@minmon/ui/components/button"; import { useForm } from "@tanstack/react-form"; import { useMutation } from "@tanstack/react-query"; import { createFileRoute, useNavigate } from "@tanstack/react-router"; import { toast } from "sonner"; import { FormCard, PageSection, TextAreaField, TextField } from "@/components/dashboard-ui"; import { queryClient, trpcClient } from "@/utils/trpc"; export const Route = createFileRoute("/dashboard/servers/new")({ component: RouteComponent, }); type ServerFormValues = { name: string; primaryIpAddress: string; secondaryIpAddress: string; operatingSystem: string; location: string; provider: string; description: string; status: "active" | "maintenance" | "inactive"; }; function RouteComponent() { const navigate = useNavigate(); const createServer = useMutation({ mutationFn: async (values: ServerFormValues) => trpcClient.servers.create.mutate({ ...values, secondaryIpAddress: values.secondaryIpAddress || null, operatingSystem: values.operatingSystem || null, location: values.location || null, provider: values.provider || null, description: values.description || null, }), onSuccess: async (created) => { toast.success("Server created"); await queryClient.invalidateQueries(); navigate({ to: "/dashboard/servers/$serverId", params: { serverId: created.id } }); }, onError: (error) => { toast.error(error.message); }, }); const defaultValues: ServerFormValues = { name: "", primaryIpAddress: "", secondaryIpAddress: "", operatingSystem: "", location: "", provider: "", description: "", status: "active", }; const form = useForm({ defaultValues, onSubmit: async ({ value }) => { await createServer.mutateAsync(value); }, }); return (
{ e.preventDefault(); e.stopPropagation(); form.handleSubmit(); }} > ({ isSubmitting: state.isSubmitting })}> {({ isSubmitting }) => } } >
{(field) => ( field.handleChange(e.target.value)} placeholder="Production VPS" /> )} {(field) => ( field.handleChange(e.target.value)} placeholder="203.0.113.10" /> )} {(field) => ( field.handleChange(e.target.value)} placeholder="203.0.113.11" /> )} {(field) => ( field.handleChange(e.target.value)} placeholder="Ubuntu 24.04 LTS" /> )} {(field) => ( field.handleChange(e.target.value)} placeholder="Singapore" /> )} {(field) => ( field.handleChange(e.target.value)} placeholder="DigitalOcean" /> )} {(field) => ( field.handleChange(e.target.value as "active" | "maintenance" | "inactive")} placeholder="active" /> )}
{(field) => ( field.handleChange(e.target.value)} placeholder="Describe the purpose of this server." /> )}
); }