import { GitlabIcon } 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", }), gitlabUrl: z.string().url({ message: "Invalid Gitlab URL", }), groupName: z.string().optional(), }); type Schema = z.infer; interface Props { gitlabId: string; } export const EditGitlabProvider = ({ gitlabId }: Props) => { const { data: gitlab, refetch } = api.gitlab.one.useQuery( { gitlabId, }, { enabled: !!gitlabId, }, ); const utils = api.useUtils(); const [isOpen, setIsOpen] = useState(false); const { mutateAsync, error, isError } = api.gitlab.update.useMutation(); const { mutateAsync: testConnection, isLoading } = api.gitlab.testConnection.useMutation(); const form = useForm({ defaultValues: { groupName: "", name: "", gitlabUrl: "https://gitlab.com", }, resolver: zodResolver(Schema), }); const groupName = form.watch("groupName"); useEffect(() => { form.reset({ groupName: gitlab?.groupName || "", name: gitlab?.gitProvider.name || "", gitlabUrl: gitlab?.gitlabUrl || "", }); }, [form, isOpen]); const onSubmit = async (data: Schema) => { await mutateAsync({ gitlabId, gitProviderId: gitlab?.gitProviderId || "", groupName: data.groupName || "", name: data.name || "", gitlabUrl: data.gitlabUrl || "", }) .then(async () => { await utils.gitProvider.getAll.invalidate(); toast.success("Gitlab updated successfully"); setIsOpen(false); refetch(); }) .catch(() => { toast.error("Error updating Gitlab"); }); }; return ( Update GitLab {isError && {error?.message}}
( Name )} /> ( Gitlab Url )} /> ( Group Name (Optional, Comma-Separated List) )} />
); };