|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { CredentialType } from "@prisma/client"; |
| 4 | +import { useRouter } from "next/navigation"; |
| 5 | +import { |
| 6 | + useCreateOneCredential, |
| 7 | + useUpdateCredential, |
| 8 | +} from "../hooks/useCredentials"; |
| 9 | +import { useUpgradeModal } from "@/hooks/useUpgradeModal"; |
| 10 | +import { FormProvider, useForm } from "react-hook-form"; |
| 11 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 12 | +import z from "zod"; |
| 13 | +import { |
| 14 | + Card, |
| 15 | + CardContent, |
| 16 | + CardDescription, |
| 17 | + CardHeader, |
| 18 | + CardTitle, |
| 19 | +} from "@/components/ui/card"; |
| 20 | +import { |
| 21 | + FormControl, |
| 22 | + FormField, |
| 23 | + FormItem, |
| 24 | + FormLabel, |
| 25 | + FormMessage, |
| 26 | +} from "@/components/ui/form"; |
| 27 | +import { Input } from "@/components/ui/input"; |
| 28 | +import { |
| 29 | + Select, |
| 30 | + SelectContent, |
| 31 | + SelectItem, |
| 32 | + SelectTrigger, |
| 33 | + SelectValue, |
| 34 | +} from "@/components/ui/select"; |
| 35 | +import Image from "next/image"; |
| 36 | +import { Button } from "@/components/ui/button"; |
| 37 | +import Link from "next/link"; |
| 38 | + |
| 39 | +type Props = { |
| 40 | + initialData?: { |
| 41 | + id?: string; |
| 42 | + name: string; |
| 43 | + type: CredentialType; |
| 44 | + value: string; |
| 45 | + }; |
| 46 | +}; |
| 47 | + |
| 48 | +const formSchema = z.object({ |
| 49 | + name: z.string().min(1, "Name is required"), |
| 50 | + type: z.enum(CredentialType), |
| 51 | + value: z.string().min(1, "API key is required"), |
| 52 | +}); |
| 53 | + |
| 54 | +type FormValues = z.infer<typeof formSchema>; |
| 55 | + |
| 56 | +const options = [ |
| 57 | + { |
| 58 | + value: CredentialType.GEMINI, |
| 59 | + label: "Gemini", |
| 60 | + logo: "/logos/gemini.svg", |
| 61 | + }, |
| 62 | +]; |
| 63 | + |
| 64 | +const CredentialForm = ({ initialData }: Props) => { |
| 65 | + const router = useRouter(); |
| 66 | + const createOne = useCreateOneCredential(); |
| 67 | + const update = useUpdateCredential(); |
| 68 | + const { modal, handleError } = useUpgradeModal(); |
| 69 | + |
| 70 | + const isEdit = !!initialData?.id; |
| 71 | + |
| 72 | + const form = useForm<FormValues>({ |
| 73 | + resolver: zodResolver(formSchema), |
| 74 | + defaultValues: initialData || { |
| 75 | + name: "", |
| 76 | + type: CredentialType.GEMINI, |
| 77 | + value: "", |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + const onSubmit = async (values: FormValues) => { |
| 82 | + if (isEdit && initialData?.id) { |
| 83 | + await update.mutateAsync( |
| 84 | + { id: initialData.id, ...values }, |
| 85 | + { |
| 86 | + onSuccess: () => router.push(`/credentials`), |
| 87 | + }, |
| 88 | + ); |
| 89 | + } else { |
| 90 | + await createOne.mutateAsync(values, { |
| 91 | + onError: (error) => handleError(error), |
| 92 | + onSuccess: (data) => router.push(`/credentials/${data.id}`), |
| 93 | + }); |
| 94 | + } |
| 95 | + }; |
| 96 | + return ( |
| 97 | + <> |
| 98 | + {modal} |
| 99 | + <Card> |
| 100 | + <CardHeader> |
| 101 | + <CardTitle> |
| 102 | + {isEdit ? "Edit Credentials" : "Create Credentials"} |
| 103 | + </CardTitle> |
| 104 | + <CardDescription> |
| 105 | + {isEdit |
| 106 | + ? "Update your API key or credentials details" |
| 107 | + : "Add a new API key or credential to your account"} |
| 108 | + </CardDescription> |
| 109 | + </CardHeader> |
| 110 | + <CardContent> |
| 111 | + <FormProvider {...form}> |
| 112 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6"> |
| 113 | + <FormField |
| 114 | + control={form.control} |
| 115 | + name="name" |
| 116 | + render={({ field }) => ( |
| 117 | + <FormItem> |
| 118 | + <FormLabel>Name</FormLabel> |
| 119 | + <FormControl> |
| 120 | + <Input placeholder="My API key" {...field} /> |
| 121 | + </FormControl> |
| 122 | + <FormMessage /> |
| 123 | + </FormItem> |
| 124 | + )} |
| 125 | + /> |
| 126 | + <FormField |
| 127 | + control={form.control} |
| 128 | + name="type" |
| 129 | + render={({ field }) => ( |
| 130 | + <FormItem> |
| 131 | + <FormLabel>Type</FormLabel> |
| 132 | + <Select |
| 133 | + onValueChange={field.onChange} |
| 134 | + defaultValue={field.value} |
| 135 | + > |
| 136 | + <FormControl> |
| 137 | + <SelectTrigger className="w-full"> |
| 138 | + <SelectValue /> |
| 139 | + </SelectTrigger> |
| 140 | + </FormControl> |
| 141 | + <SelectContent> |
| 142 | + {options.map((o) => ( |
| 143 | + <SelectItem key={o.value} value={o.value}> |
| 144 | + <div className="flex items-center gap-2"> |
| 145 | + <Image |
| 146 | + src={o.logo} |
| 147 | + alt={o.label} |
| 148 | + width={16} |
| 149 | + height={16} |
| 150 | + /> |
| 151 | + {o.label} |
| 152 | + </div> |
| 153 | + </SelectItem> |
| 154 | + ))} |
| 155 | + </SelectContent> |
| 156 | + </Select> |
| 157 | + <FormMessage /> |
| 158 | + </FormItem> |
| 159 | + )} |
| 160 | + /> |
| 161 | + <FormField |
| 162 | + control={form.control} |
| 163 | + name="value" |
| 164 | + render={({ field }) => ( |
| 165 | + <FormItem> |
| 166 | + <FormLabel>API Ley</FormLabel> |
| 167 | + <FormControl> |
| 168 | + <Input type="password" placeholder="sk-..." {...field} /> |
| 169 | + </FormControl> |
| 170 | + <FormMessage /> |
| 171 | + </FormItem> |
| 172 | + )} |
| 173 | + /> |
| 174 | + <div className="flex gap-4"> |
| 175 | + <Button |
| 176 | + type="submit" |
| 177 | + disabled={createOne.isPending || update.isPending} |
| 178 | + > |
| 179 | + {isEdit ? "Update" : "Create"} |
| 180 | + </Button> |
| 181 | + <Button |
| 182 | + type="button" |
| 183 | + variant={"outline"} |
| 184 | + onClick={() => router.push("/credentials")} |
| 185 | + asChild |
| 186 | + > |
| 187 | + <Link href={"/credentials"} prefetch> |
| 188 | + Cancel |
| 189 | + </Link> |
| 190 | + </Button> |
| 191 | + </div> |
| 192 | + </form> |
| 193 | + </FormProvider> |
| 194 | + </CardContent> |
| 195 | + </Card> |
| 196 | + </> |
| 197 | + ); |
| 198 | +}; |
| 199 | + |
| 200 | +export default CredentialForm; |
0 commit comments