diff --git a/frontend/src/pages/integrations/github/create.tsx b/frontend/src/pages/integrations/github/create.tsx index 23296cefd..f09602384 100644 --- a/frontend/src/pages/integrations/github/create.tsx +++ b/frontend/src/pages/integrations/github/create.tsx @@ -1,4 +1,5 @@ import { useEffect, useState } from "react"; +import { Controller, useForm } from "react-hook-form"; import Head from "next/head"; import Image from "next/image"; import Link from "next/link"; @@ -12,9 +13,13 @@ import { faCircleInfo } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { yupResolver } from "@hookform/resolvers/yup"; +import axios from "axios"; import { motion } from "framer-motion"; import queryString from "query-string"; +import * as yup from "yup"; +import { useNotificationContext } from "@app/components/context/Notifications/NotificationProvider"; import { useCreateIntegration } from "@app/hooks/api"; import { @@ -45,9 +50,22 @@ enum TabSections { Options = "options" } +const schema = yup.object({ + selectedSourceEnvironment: yup.string().trim().required("Project Environment is required"), + secretPath: yup.string().trim().required("Secrets Path is required"), + targetAppIds: yup + .array(yup.string().required()) + .min(1, "Select atleast one repo") // .min() not working showing error for empty array + .required("Select atleast one repo"), + secretSuffix: yup.string().trim().optional() +}); + +type FormData = yup.InferType; + export default function GitHubCreateIntegrationPage() { const router = useRouter(); const { mutateAsync } = useCreateIntegration(); + const { createNotification } = useNotificationContext(); const { integrationAuthId } = queryString.parse(router.asPath.split("?")[1]); @@ -58,37 +76,44 @@ export default function GitHubCreateIntegrationPage() { integrationAuthId: (integrationAuthId as string) ?? "" }); - const [selectedSourceEnvironment, setSelectedSourceEnvironment] = useState(""); - const [secretPath, setSecretPath] = useState("/"); - const [targetAppIds, setTargetAppIds] = useState([]); - const [secretSuffix, setSecretSuffix] = useState(""); + const { control, handleSubmit, watch, setValue } = useForm({ + resolver: yupResolver(schema), + defaultValues: { + selectedSourceEnvironment: "", + secretPath: "/", + targetAppIds: [], + secretSuffix: "" + } + }); + + const targetAppIds = watch("targetAppIds"); const [isLoading, setIsLoading] = useState(false); useEffect(() => { if (workspace) { - setSelectedSourceEnvironment(workspace.environments[0].slug); + setValue("selectedSourceEnvironment", workspace.environments[0].slug); } }, [workspace]); useEffect(() => { if (integrationAuthApps) { if (integrationAuthApps.length > 0) { - setTargetAppIds([String(integrationAuthApps[0].appId)]); + setValue("targetAppIds", [String(integrationAuthApps[0].appId)]); } else { - setTargetAppIds(["none"]); + setValue("targetAppIds", ["none"]); } } }, [integrationAuthApps]); - const handleButtonClick = async () => { + const onFormSubmit = async (data: FormData) => { try { setIsLoading(true); if (!integrationAuth?.id) return; const targetApps = integrationAuthApps?.filter((integrationAuthApp) => - targetAppIds.includes(String(integrationAuthApp.appId)) + data.targetAppIds.includes(String(integrationAuthApp.appId)) ); if (!targetApps) return; @@ -99,11 +124,11 @@ export default function GitHubCreateIntegrationPage() { integrationAuthId: integrationAuth?.id, isActive: true, app: targetApp.name, - sourceEnvironment: selectedSourceEnvironment, owner: targetApp.owner, - secretPath, + secretPath: data.secretPath, + sourceEnvironment: data.selectedSourceEnvironment, metadata: { - secretSuffix + secretSuffix: data.secretSuffix } }); }) @@ -113,183 +138,229 @@ export default function GitHubCreateIntegrationPage() { router.push(`/integrations/${localStorage.getItem("projectData.id")}`); } catch (err) { console.error(err); + if (axios.isAxiosError(err)) { + const { message } = err?.response?.data as { message: string }; + createNotification({ + text: message, + type: "error" + }); + } + setIsLoading(false); } }; - return integrationAuth && - workspace && - selectedSourceEnvironment && - integrationAuthApps && - targetAppIds ? ( + return integrationAuth && workspace && integrationAuthApps ? (
Set Up GitHub Integration - -
-
- GitHub logo +
+ +
+
+ GitHub logo +
+ GitHub Integration + + +
+ + Docs + +
+
+
- GitHub Integration - - -
- - Docs - -
-
- -
- - - -
- Connection - Options -
-
- - - - - - - setSecretPath(evt.target.value)} - placeholder="Provide a path, default is /" - /> - - - - - {integrationAuthApps.length > 0 ? ( -
- {targetAppIds.length === 1 - ? integrationAuthApps?.find( - (integrationAuthApp) => - targetAppIds[0] === String(integrationAuthApp.appId) - )?.name - : `${targetAppIds.length} repositories selected`} - -
- ) : ( -
- No repositories found -
- )} -
- - {integrationAuthApps.length > 0 ? ( - integrationAuthApps.map((integrationAuthApp) => { - const isSelected = targetAppIds.includes(String(integrationAuthApp.appId)); - - return ( - { - if (targetAppIds.includes(String(integrationAuthApp.appId))) { - setTargetAppIds( - targetAppIds.filter( - (appId) => appId !== String(integrationAuthApp.appId) - ) - ); - } else { - setTargetAppIds([ - ...targetAppIds, - String(integrationAuthApp.appId) - ]); - } - }} - key={integrationAuthApp.appId} - icon={ - isSelected ? ( - - ) : ( -
- ) - } - iconPos="left" - className="w-[28.4rem] text-sm" + setSecretSuffix(evt.target.value)} - placeholder="Provide a suffix for secret names, default is no suffix" + {sourceEnvironment.name} + + ))} + + + )} /> - - - - - + + ( + + + + )} + /> + + ( + + + + {integrationAuthApps.length > 0 ? ( +
+ {targetAppIds.length === 1 + ? integrationAuthApps?.find( + (integrationAuthApp) => + targetAppIds[0] === String(integrationAuthApp.appId) + )?.name + : `${targetAppIds.length} repositories selected`} + +
+ ) : ( +
+ No repositories found +
+ )} +
+ + {integrationAuthApps.length > 0 ? ( + integrationAuthApps.map((integrationAuthApp) => { + const isSelected = targetAppIds.includes( + String(integrationAuthApp.appId) + ); + + return ( + { + if (targetAppIds.includes(String(integrationAuthApp.appId))) { + onChange( + targetAppIds.filter( + (appId) => appId !== String(integrationAuthApp.appId) + ) + ); + } else { + onChange([...targetAppIds, String(integrationAuthApp.appId)]); + } + }} + key={integrationAuthApp.appId} + icon={ + isSelected ? ( + + ) : ( +
+ ) + } + iconPos="left" + className="w-[28.4rem] text-sm" + > + {integrationAuthApp.name} + + ); + }) + ) : ( +
+ )} + + + + )} + /> + + + + + ( + + + + )} + /> + + + +
+ +
+