From a89ed40dcd858ba386db31b5e40524eff70b5581 Mon Sep 17 00:00:00 2001 From: Tuan Dang Date: Sat, 16 Sep 2023 18:13:55 +0100 Subject: [PATCH] Update Fly.io integration authorization page to use react-form --- backend/src/integrations/apps.ts | 11 +- .../pages/integrations/flyio/authorize.tsx | 97 +++--- .../src/pages/integrations/flyio/create.tsx | 1 + .../src/pages/integrations/gitlab/create.tsx | 303 +++++++++--------- 4 files changed, 222 insertions(+), 190 deletions(-) diff --git a/backend/src/integrations/apps.ts b/backend/src/integrations/apps.ts index dc947f6f1..cd6cb263f 100644 --- a/backend/src/integrations/apps.ts +++ b/backend/src/integrations/apps.ts @@ -608,6 +608,12 @@ const getAppsLaravelForge = async ({ * @returns {String} apps.name - name of Fly.io apps */ const getAppsFlyio = async ({ accessToken }: { accessToken: string }) => { + interface FlyioApp { + id: string; + name: string; + hostname: string; + } + const query = ` query($role: String) { apps(type: "container", first: 400, role: $role) { @@ -620,7 +626,7 @@ const getAppsFlyio = async ({ accessToken }: { accessToken: string }) => { } `; - const res = ( + const res: FlyioApp[] = ( await standardRequest.post( INTEGRATION_FLYIO_API_URL, { @@ -639,8 +645,9 @@ const getAppsFlyio = async ({ accessToken }: { accessToken: string }) => { ) ).data.data.apps.nodes; - const apps = res.map((a: any) => ({ + const apps = res.map((a: FlyioApp) => ({ name: a.name, + appId: a.id })); return apps; diff --git a/frontend/src/pages/integrations/flyio/authorize.tsx b/frontend/src/pages/integrations/flyio/authorize.tsx index b40cde5da..277e62167 100644 --- a/frontend/src/pages/integrations/flyio/authorize.tsx +++ b/frontend/src/pages/integrations/flyio/authorize.tsx @@ -1,10 +1,13 @@ import { 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"; import { useRouter } from "next/router"; import { faArrowUpRightFromSquare, faBookOpen } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { yupResolver } from "@hookform/resolvers/yup"; +import * as yup from "yup"; import { useSaveIntegrationAccessToken @@ -12,37 +15,49 @@ import { import { Button, Card, CardTitle, FormControl, Input } from "../../../components/v2"; -export default function FlyioCreateIntegrationPage() { +const schema = yup.object({ + accessToken: yup.string().trim().required("Fly.io Access Token is required") +}); + +type FormData = yup.InferType; + +export default function FlyioAuthorizeIntegrationPage() { const router = useRouter(); + + const { + control, + handleSubmit + } = useForm({ + resolver: yupResolver(schema), + defaultValues: { + accessToken: "" + } + }); + const { mutateAsync } = useSaveIntegrationAccessToken(); - const [accessToken, setAccessToken] = useState(""); - const [accessTokenErrorText, setAccessTokenErrorText] = useState(""); const [isLoading, setIsLoading] = useState(false); - - const handleButtonClick = async () => { + + const onFormSubmit = async ({ + accessToken + }: FormData) => { + console.log("onFormSubmit accessToken: ", accessToken); try { - setAccessTokenErrorText(""); - if (accessToken.length === 0) { - setAccessTokenErrorText("Access token cannot be blank"); - return; - } - setIsLoading(true); - + const integrationAuth = await mutateAsync({ workspaceId: localStorage.getItem("projectData.id"), integration: "flyio", accessToken }); - + setIsLoading(false); - router.push(`/integrations/flyio/create?integrationAuthId=${integrationAuth._id}`); } catch (err) { + setIsLoading(false); console.error(err); } - }; + } return (
@@ -69,37 +84,47 @@ export default function FlyioCreateIntegrationPage() {
- Docs + Docs
- - setAccessToken(e.target.value)} + ( + + + + )} /> - - + + ); } -FlyioCreateIntegrationPage.requireAuth = true; +FlyioAuthorizeIntegrationPage.requireAuth = true; diff --git a/frontend/src/pages/integrations/flyio/create.tsx b/frontend/src/pages/integrations/flyio/create.tsx index e24e636bf..be1a577de 100644 --- a/frontend/src/pages/integrations/flyio/create.tsx +++ b/frontend/src/pages/integrations/flyio/create.tsx @@ -28,6 +28,7 @@ import { useGetWorkspaceById } from "../../../hooks/api/workspace"; export default function FlyioCreateIntegrationPage() { const router = useRouter(); + const { mutateAsync } = useCreateIntegration(); const { integrationAuthId } = queryString.parse(router.asPath.split("?")[1]); diff --git a/frontend/src/pages/integrations/gitlab/create.tsx b/frontend/src/pages/integrations/gitlab/create.tsx index 96c651fdd..ecf787ee5 100644 --- a/frontend/src/pages/integrations/gitlab/create.tsx +++ b/frontend/src/pages/integrations/gitlab/create.tsx @@ -29,7 +29,8 @@ import { Tab, TabList, TabPanel, - Tabs} from "../../../components/v2"; + Tabs +} from "../../../components/v2"; import { useGetIntegrationAuthApps, useGetIntegrationAuthById, @@ -229,165 +230,163 @@ export default function GitLabCreateIntegrationPage() { animate={{ opacity: 1, translateX: 0 }} exit={{ opacity: 0, translateX: 30 }} > -
- ( - ( + + onChange(e)} - className="w-full" - > - {workspace?.environments.map((sourceEnvironment) => ( - - {sourceEnvironment.name} - - ))} - - - )} - /> - ( - ( + - - - )} - /> - ( - - - - )} - /> - {targetEntity === "group" && targetTeamId && ( - ( - - - - )} - /> + {sourceEnvironment.name} + + ))} + + )} + /> + ( + + + + )} + /> + ( + + + + )} + /> + {targetEntity === "group" && targetTeamId && ( { - return ( - ( + + { - if (e === "") return; - onChange(e) - }} - className="w-full" - > - {integrationAuthApps.length > 0 ? ( - integrationAuthApps.map((integrationAuthApp) => ( - - {integrationAuthApp.name} - - )) - ) : ( - - No projects found + {integrationAuthTeams.length > 0 ? ( + integrationAuthTeams.map((integrationAuthTeam) => + ( + + {integrationAuthTeam.name} - )} - - - )}} - /> - ( - - - - )} + )) + ) : ( + + No groups found + + )} + + + )} /> -
+ )} + { + return ( + + + + )}} + /> + ( + + + + )} + />