import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { api } from "@/utils/api"; import { AlertBlock } from "@/components/shared/alert-block"; import { zodResolver } from "@hookform/resolvers/zod"; import { PenBoxIcon } from "lucide-react"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; const updateDestination = z.object({ name: z.string().min(1, "Name is required"), accessKeyId: z.string(), secretAccessKey: z.string(), bucket: z.string(), region: z.string(), endpoint: z.string(), }); type UpdateDestination = z.infer; interface Props { destinationId: string; } export const UpdateDestination = ({ destinationId }: Props) => { const utils = api.useUtils(); const { data, refetch } = api.destination.one.useQuery( { destinationId, }, { enabled: !!destinationId, }, ); const { mutateAsync, isError, error } = api.destination.update.useMutation(); const { mutateAsync: testConnection, isLoading: isLoadingConnection } = api.destination.testConnection.useMutation(); const form = useForm({ defaultValues: { accessKeyId: "", bucket: "", name: "", region: "", secretAccessKey: "", endpoint: "", }, resolver: zodResolver(updateDestination), }); useEffect(() => { if (data) { form.reset({ accessKeyId: data.accessKey, bucket: data.bucket, endpoint: data.endpoint, name: data.name, region: data.region, secretAccessKey: data.secretAccessKey, }); } }, [form, form.reset, data]); const onSubmit = async (data: UpdateDestination) => { await mutateAsync({ accessKey: data.accessKeyId, bucket: data.bucket, endpoint: data.endpoint, name: data.name, region: data.region, secretAccessKey: data.secretAccessKey, destinationId, }) .then(async () => { toast.success("Destination Updated"); await refetch(); await utils.destination.all.invalidate(); }) .catch(() => { toast.error("Error to update the Destination"); }); }; return ( Update Destination Update the current destination config {isError && {error?.message}}
{ return ( Name ); }} /> { return ( Access Key Id ); }} /> (
Secret Access Key
)} /> (
Bucket
)} /> (
Region
)} /> ( Endpoint )} />
); };