fosrl.pangolin/src/app/[orgId]/settings/resources/CreateResourceForm.tsx

321 lines
14 KiB
TypeScript
Raw Normal View History

2024-11-11 00:00:16 -05:00
"use client";
import { Button, buttonVariants } from "@app/components/ui/button";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage
2024-11-11 00:00:16 -05:00
} from "@app/components/ui/form";
import { Input } from "@app/components/ui/input";
import { useToast } from "@app/hooks/useToast";
import { zodResolver } from "@hookform/resolvers/zod";
import { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";
import {
Credenza,
CredenzaBody,
CredenzaClose,
CredenzaContent,
CredenzaDescription,
CredenzaFooter,
CredenzaHeader,
CredenzaTitle
2024-11-11 00:00:16 -05:00
} from "@app/components/Credenza";
import { useParams, useRouter } from "next/navigation";
import { ListSitesResponse } from "@server/routers/site";
2025-01-01 21:41:31 -05:00
import { formatAxiosError } from "@app/lib/api";
2024-11-11 00:00:16 -05:00
import { CheckIcon } from "lucide-react";
import {
Popover,
PopoverContent,
PopoverTrigger
2024-11-11 00:00:16 -05:00
} from "@app/components/ui/popover";
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList
2024-11-11 00:00:16 -05:00
} from "@app/components/ui/command";
import { CaretSortIcon } from "@radix-ui/react-icons";
2025-01-01 21:41:31 -05:00
import CustomDomainInput from "./[resourceId]/CustomDomainInput";
2024-11-11 00:00:16 -05:00
import { Axios, AxiosResponse } from "axios";
import { Resource } from "@server/db/schema";
import { useOrgContext } from "@app/hooks/useOrgContext";
import { subdomainSchema } from "@server/schemas/subdomainSchema";
2025-01-01 21:41:31 -05:00
import { createApiClient } from "@app/lib/api";
import { useEnvContext } from "@app/hooks/useEnvContext";
2025-01-01 21:41:31 -05:00
import { cn } from "@app/lib/cn";
2024-11-11 00:00:16 -05:00
const accountFormSchema = z.object({
subdomain: subdomainSchema,
2024-11-11 00:00:16 -05:00
name: z.string(),
siteId: z.number()
2024-11-11 00:00:16 -05:00
});
type AccountFormValues = z.infer<typeof accountFormSchema>;
type CreateResourceFormProps = {
open: boolean;
setOpen: (open: boolean) => void;
};
export default function CreateResourceForm({
open,
setOpen
2024-11-11 00:00:16 -05:00
}: CreateResourceFormProps) {
const { toast } = useToast();
const api = createApiClient(useEnvContext());
2024-11-11 00:00:16 -05:00
const [loading, setLoading] = useState(false);
const params = useParams();
const orgId = params.orgId;
const router = useRouter();
const { org } = useOrgContext();
2024-11-11 00:00:16 -05:00
const [sites, setSites] = useState<ListSitesResponse["sites"]>([]);
const [domainSuffix, setDomainSuffix] = useState<string>(org.org.domain);
2024-11-11 00:00:16 -05:00
const form = useForm<AccountFormValues>({
resolver: zodResolver(accountFormSchema),
defaultValues: {
subdomain: "",
name: "My Resource"
}
2024-11-11 00:00:16 -05:00
});
useEffect(() => {
if (!open) {
return;
}
const fetchSites = async () => {
const res = await api.get<AxiosResponse<ListSitesResponse>>(
`/org/${orgId}/sites/`
2024-11-11 00:00:16 -05:00
);
setSites(res.data.data.sites);
if (res.data.data.sites.length > 0) {
form.setValue("siteId", res.data.data.sites[0].siteId);
}
2024-11-11 00:00:16 -05:00
};
fetchSites();
}, [open]);
async function onSubmit(data: AccountFormValues) {
console.log(data);
const res = await api
.put<AxiosResponse<Resource>>(
`/org/${orgId}/site/${data.siteId}/resource/`,
{
name: data.name,
subdomain: data.subdomain
2024-11-11 00:00:16 -05:00
// subdomain: data.subdomain,
}
2024-11-11 00:00:16 -05:00
)
.catch((e) => {
toast({
variant: "destructive",
2024-11-11 00:00:16 -05:00
title: "Error creating resource",
description: formatAxiosError(
e,
"An error occurred when creating the resource"
)
2024-11-11 00:00:16 -05:00
});
});
if (res && res.status === 201) {
const id = res.data.data.resourceId;
// navigate to the resource page
router.push(`/${orgId}/settings/resources/${id}`);
}
}
return (
<>
<Credenza
open={open}
onOpenChange={(val) => {
setOpen(val);
setLoading(false);
// reset all values
form.reset();
}}
>
<CredenzaContent>
<CredenzaHeader>
<CredenzaTitle>Create Resource</CredenzaTitle>
<CredenzaDescription>
Create a new resource to proxy requests to your app
</CredenzaDescription>
</CredenzaHeader>
<CredenzaBody>
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="space-y-4"
id="create-resource-form"
>
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input
placeholder="Your name"
{...field}
/>
</FormControl>
<FormDescription>
This is the name that will be
displayed for this resource.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="subdomain"
render={({ field }) => (
<FormItem>
<FormLabel>Subdomain</FormLabel>
<FormControl>
<CustomDomainInput
value={field.value}
2024-11-11 00:00:16 -05:00
domainSuffix={domainSuffix}
placeholder="Enter subdomain"
onChange={(value) =>
form.setValue(
"subdomain",
value
)
}
2024-11-11 00:00:16 -05:00
/>
</FormControl>
<FormDescription>
This is the fully qualified
domain name that will be used to
access the resource.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="siteId"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel>Site</FormLabel>
<Popover>
<PopoverTrigger asChild>
<FormControl>
<Button
variant="outline"
role="combobox"
className={cn(
2024-12-20 22:24:44 -05:00
"justify-between",
2024-11-11 00:00:16 -05:00
!field.value &&
"text-muted-foreground"
2024-11-11 00:00:16 -05:00
)}
>
{field.value
? sites.find(
(site) =>
site.siteId ===
field.value
2024-11-11 00:00:16 -05:00
)?.name
: "Select site"}
<CaretSortIcon className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
2024-12-20 22:24:44 -05:00
<PopoverContent className="p-0">
2024-11-11 00:00:16 -05:00
<Command>
<CommandInput placeholder="Search site..." />
<CommandList>
<CommandEmpty>
No site found.
</CommandEmpty>
<CommandGroup>
{sites.map(
(site) => (
<CommandItem
value={
2025-01-23 21:26:59 -05:00
site.niceId
2024-11-11 00:00:16 -05:00
}
key={
site.siteId
}
onSelect={() => {
form.setValue(
"siteId",
site.siteId
2024-11-11 00:00:16 -05:00
);
}}
>
<CheckIcon
className={cn(
"mr-2 h-4 w-4",
site.siteId ===
field.value
? "opacity-100"
: "opacity-0"
2024-11-11 00:00:16 -05:00
)}
/>
{
site.name
}
</CommandItem>
)
2024-11-11 00:00:16 -05:00
)}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
<FormDescription>
This is the site that will be
used in the dashboard.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
</CredenzaBody>
<CredenzaFooter>
<Button
type="submit"
form="create-resource-form"
loading={loading}
disabled={loading}
>
Create Resource
</Button>
<CredenzaClose asChild>
<Button variant="outline">Close</Button>
</CredenzaClose>
</CredenzaFooter>
</CredenzaContent>
</Credenza>
</>
);
}