import { GithubIcon } from "@/components/icons/data-tools-icons"; import { AlertBlock } from "@/components/shared/alert-block"; import { Button } from "@/components/ui/button"; import { CardContent } from "@/components/ui/card"; import { Dialog, DialogContent, 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 { zodResolver } from "@hookform/resolvers/zod"; import { PenBoxIcon } from "lucide-react"; import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; const Schema = z.object({ name: z.string().min(1, { message: "Name is required", }), }); type Schema = z.infer; interface Props { githubId: string; } export const EditGithubProvider = ({ githubId }: Props) => { const { data: github } = api.github.one.useQuery( { githubId, }, { enabled: !!githubId, }, ); const utils = api.useUtils(); const [isOpen, setIsOpen] = useState(false); const { mutateAsync, error, isError } = api.github.update.useMutation(); const { mutateAsync: testConnection, isLoading } = api.github.testConnection.useMutation(); const form = useForm({ defaultValues: { name: "", }, resolver: zodResolver(Schema), }); useEffect(() => { form.reset({ name: github?.gitProvider.name || "", }); }, [form, isOpen]); const onSubmit = async (data: Schema) => { await mutateAsync({ githubId, name: data.name || "", gitProviderId: github?.gitProviderId || "", }) .then(async () => { await utils.gitProvider.getAll.invalidate(); toast.success("Github updated successfully"); setIsOpen(false); }) .catch(() => { toast.error("Error updating Github"); }); }; return ( Update Github {isError && {error?.message}}
( Name )} />
); };