Update Fly.io integration authorization page to use react-form

This commit is contained in:
Tuan Dang
2023-09-16 18:13:55 +01:00
parent 0f003e8ab6
commit a89ed40dcd
4 changed files with 222 additions and 190 deletions
+9 -2
View File
@@ -608,6 +608,12 @@ const getAppsLaravelForge = async ({
* @returns {String} apps.name - name of Fly.io apps * @returns {String} apps.name - name of Fly.io apps
*/ */
const getAppsFlyio = async ({ accessToken }: { accessToken: string }) => { const getAppsFlyio = async ({ accessToken }: { accessToken: string }) => {
interface FlyioApp {
id: string;
name: string;
hostname: string;
}
const query = ` const query = `
query($role: String) { query($role: String) {
apps(type: "container", first: 400, role: $role) { 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( await standardRequest.post(
INTEGRATION_FLYIO_API_URL, INTEGRATION_FLYIO_API_URL,
{ {
@@ -639,8 +645,9 @@ const getAppsFlyio = async ({ accessToken }: { accessToken: string }) => {
) )
).data.data.apps.nodes; ).data.data.apps.nodes;
const apps = res.map((a: any) => ({ const apps = res.map((a: FlyioApp) => ({
name: a.name, name: a.name,
appId: a.id
})); }));
return apps; return apps;
@@ -1,10 +1,13 @@
import { useState } from "react"; import { useState } from "react";
import { Controller, useForm } from "react-hook-form";
import Head from "next/head"; import Head from "next/head";
import Image from "next/image"; import Image from "next/image";
import Link from "next/link"; import Link from "next/link";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { faArrowUpRightFromSquare, faBookOpen } from "@fortawesome/free-solid-svg-icons"; import { faArrowUpRightFromSquare, faBookOpen } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup";
import { import {
useSaveIntegrationAccessToken useSaveIntegrationAccessToken
@@ -12,37 +15,49 @@ import {
import { Button, Card, CardTitle, FormControl, Input } from "../../../components/v2"; 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<typeof schema>;
export default function FlyioAuthorizeIntegrationPage() {
const router = useRouter(); const router = useRouter();
const {
control,
handleSubmit
} = useForm<FormData>({
resolver: yupResolver(schema),
defaultValues: {
accessToken: ""
}
});
const { mutateAsync } = useSaveIntegrationAccessToken(); const { mutateAsync } = useSaveIntegrationAccessToken();
const [accessToken, setAccessToken] = useState("");
const [accessTokenErrorText, setAccessTokenErrorText] = useState("");
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const handleButtonClick = async () => { const onFormSubmit = async ({
accessToken
}: FormData) => {
console.log("onFormSubmit accessToken: ", accessToken);
try { try {
setAccessTokenErrorText("");
if (accessToken.length === 0) {
setAccessTokenErrorText("Access token cannot be blank");
return;
}
setIsLoading(true); setIsLoading(true);
const integrationAuth = await mutateAsync({ const integrationAuth = await mutateAsync({
workspaceId: localStorage.getItem("projectData.id"), workspaceId: localStorage.getItem("projectData.id"),
integration: "flyio", integration: "flyio",
accessToken accessToken
}); });
setIsLoading(false); setIsLoading(false);
router.push(`/integrations/flyio/create?integrationAuthId=${integrationAuth._id}`); router.push(`/integrations/flyio/create?integrationAuthId=${integrationAuth._id}`);
} catch (err) { } catch (err) {
setIsLoading(false);
console.error(err); console.error(err);
} }
}; }
return ( return (
<div className="flex h-full w-full items-center justify-center"> <div className="flex h-full w-full items-center justify-center">
@@ -69,37 +84,47 @@ export default function FlyioCreateIntegrationPage() {
<a target="_blank" rel="noopener noreferrer"> <a target="_blank" rel="noopener noreferrer">
<div className="ml-2 mb-1 rounded-md text-yellow text-sm inline-block bg-yellow/20 px-1.5 pb-[0.03rem] pt-[0.04rem] opacity-80 hover:opacity-100 cursor-default"> <div className="ml-2 mb-1 rounded-md text-yellow text-sm inline-block bg-yellow/20 px-1.5 pb-[0.03rem] pt-[0.04rem] opacity-80 hover:opacity-100 cursor-default">
<FontAwesomeIcon icon={faBookOpen} className="mr-1.5"/> <FontAwesomeIcon icon={faBookOpen} className="mr-1.5"/>
Docs Docs
<FontAwesomeIcon icon={faArrowUpRightFromSquare} className="ml-1.5 text-xxs mb-[0.07rem]"/> <FontAwesomeIcon icon={faArrowUpRightFromSquare} className="ml-1.5 text-xxs mb-[0.07rem]"/>
</div> </div>
</a> </a>
</Link> </Link>
</div> </div>
</CardTitle> </CardTitle>
<FormControl <form
label="Fly.io Access Token" onSubmit={handleSubmit(onFormSubmit)}
errorText={accessTokenErrorText} className="px-6 text-right pb-8"
isError={accessTokenErrorText !== "" ?? false}
className="px-6"
> >
<Input <Controller
placeholder="" control={control}
value={accessToken} name="accessToken"
onChange={(e) => setAccessToken(e.target.value)} render={({ field, fieldState: { error } }) => (
<FormControl
label="Fly.io Access Token"
errorText={error?.message}
isError={Boolean(error)}
>
<Input
{...field}
placeholder=""
/>
</FormControl>
)}
/> />
</FormControl> <Button
<Button colorSchema="primary"
onClick={handleButtonClick} variant="outline_bg"
colorSchema="primary" className="mt-2 w-min"
variant="outline_bg" size="sm"
className="mb-6 mt-2 ml-auto mr-6 w-min" type="submit"
isLoading={isLoading} isLoading={isLoading}
> >
Connect to Fly.io Connect to Fly.io
</Button> </Button>
</form>
</Card> </Card>
</div> </div>
); );
} }
FlyioCreateIntegrationPage.requireAuth = true; FlyioAuthorizeIntegrationPage.requireAuth = true;
@@ -28,6 +28,7 @@ import { useGetWorkspaceById } from "../../../hooks/api/workspace";
export default function FlyioCreateIntegrationPage() { export default function FlyioCreateIntegrationPage() {
const router = useRouter(); const router = useRouter();
const { mutateAsync } = useCreateIntegration(); const { mutateAsync } = useCreateIntegration();
const { integrationAuthId } = queryString.parse(router.asPath.split("?")[1]); const { integrationAuthId } = queryString.parse(router.asPath.split("?")[1]);
+151 -152
View File
@@ -29,7 +29,8 @@ import {
Tab, Tab,
TabList, TabList,
TabPanel, TabPanel,
Tabs} from "../../../components/v2"; Tabs
} from "../../../components/v2";
import { import {
useGetIntegrationAuthApps, useGetIntegrationAuthApps,
useGetIntegrationAuthById, useGetIntegrationAuthById,
@@ -229,165 +230,163 @@ export default function GitLabCreateIntegrationPage() {
animate={{ opacity: 1, translateX: 0 }} animate={{ opacity: 1, translateX: 0 }}
exit={{ opacity: 0, translateX: 30 }} exit={{ opacity: 0, translateX: 30 }}
> >
<div> <Controller
<Controller control={control}
control={control} name="selectedSourceEnvironment"
name="selectedSourceEnvironment" render={({ field: { onChange, ...field }, fieldState: { error } }) => (
render={({ field: { onChange, ...field }, fieldState: { error } }) => ( <FormControl
<FormControl label="Project Environment"
label="Project Environment" errorText={error?.message}
errorText={error?.message} isError={Boolean(error)}
isError={Boolean(error)} >
<Select
defaultValue={field.value}
{...field}
onValueChange={(e) => onChange(e)}
className="w-full"
> >
<Select {workspace?.environments.map((sourceEnvironment) => (
defaultValue={field.value} <SelectItem
{...field} value={sourceEnvironment.slug}
onValueChange={(e) => onChange(e)} key={`source-environment-${sourceEnvironment.slug}`}
className="w-full"
>
{workspace?.environments.map((sourceEnvironment) => (
<SelectItem
value={sourceEnvironment.slug}
key={`source-environment-${sourceEnvironment.slug}`}
>
{sourceEnvironment.name}
</SelectItem>
))}
</Select>
</FormControl>
)}
/>
<Controller
control={control}
defaultValue=""
name="secretPath"
render={({ field, fieldState: { error } }) => (
<FormControl
label="Secrets Path"
isError={Boolean(error)}
errorText={error?.message}
> >
<Input {sourceEnvironment.name}
{...field} </SelectItem>
placeholder="/" ))}
/> </Select>
</FormControl> </FormControl>
)}
/>
<Controller
control={control}
name="targetEntity"
render={({ field: { onChange, ...field }, fieldState: { error } }) => (
<FormControl
label="GitLab Integration Type"
errorText={error?.message}
isError={Boolean(error)}
>
<Select
{...field}
onValueChange={(e) => onChange(e)}
className="w-full"
>
{gitLabEntities.map((entity) => {
return (
<SelectItem value={entity.value} key={`target-entity-${entity.value}`}>
{entity.name}
</SelectItem>
);
})}
</Select>
</FormControl>
)}
/>
{targetEntity === "group" && targetTeamId && (
<Controller
control={control}
name="targetTeamId"
render={({ field: { onChange, ...field }, fieldState: { error } }) => (
<FormControl
label="GitLab Group"
errorText={error?.message}
isError={Boolean(error)}
>
<Select
{...field}
onValueChange={(e) => onChange(e)}
className="w-full"
>
{integrationAuthTeams.length > 0 ? (
integrationAuthTeams.map((integrationAuthTeam) =>
(
<SelectItem
value={String(integrationAuthTeam.teamId as string)}
key={`target-team-${String(integrationAuthTeam.teamId)}`}
>
{integrationAuthTeam.name}
</SelectItem>
))
) : (
<SelectItem value="none" key="target-team-none">
No groups found
</SelectItem>
)}
</Select>
</FormControl>
)}
/>
)} )}
/>
<Controller
control={control}
defaultValue=""
name="secretPath"
render={({ field, fieldState: { error } }) => (
<FormControl
label="Secrets Path"
isError={Boolean(error)}
errorText={error?.message}
>
<Input
{...field}
placeholder="/"
/>
</FormControl>
)}
/>
<Controller
control={control}
name="targetEntity"
render={({ field: { onChange, ...field }, fieldState: { error } }) => (
<FormControl
label="GitLab Integration Type"
errorText={error?.message}
isError={Boolean(error)}
>
<Select
{...field}
onValueChange={(e) => onChange(e)}
className="w-full"
>
{gitLabEntities.map((entity) => {
return (
<SelectItem value={entity.value} key={`target-entity-${entity.value}`}>
{entity.name}
</SelectItem>
);
})}
</Select>
</FormControl>
)}
/>
{targetEntity === "group" && targetTeamId && (
<Controller <Controller
control={control} control={control}
name="targetAppId" name="targetTeamId"
render={({ field: { onChange, ...field }, fieldState: { error } }) => { render={({ field: { onChange, ...field }, fieldState: { error } }) => (
return ( <FormControl
<FormControl label="GitLab Group"
label="GitLab Project" errorText={error?.message}
errorText={error?.message} isError={Boolean(error)}
isError={Boolean(error)} >
<Select
{...field}
onValueChange={(e) => onChange(e)}
className="w-full"
> >
<Select {integrationAuthTeams.length > 0 ? (
{...field} integrationAuthTeams.map((integrationAuthTeam) =>
onValueChange={(e) => { (
if (e === "") return; <SelectItem
onChange(e) value={String(integrationAuthTeam.teamId as string)}
}} key={`target-team-${String(integrationAuthTeam.teamId)}`}
className="w-full" >
> {integrationAuthTeam.name}
{integrationAuthApps.length > 0 ? (
integrationAuthApps.map((integrationAuthApp) => (
<SelectItem
value={String(integrationAuthApp.appId as string)}
key={`target-app-${String(integrationAuthApp.appId)}`}
>
{integrationAuthApp.name}
</SelectItem>
))
) : (
<SelectItem value="none" key="target-app-none">
No projects found
</SelectItem> </SelectItem>
)} ))
</Select> ) : (
</FormControl> <SelectItem value="none" key="target-team-none">
)}} No groups found
/> </SelectItem>
<Controller )}
control={control} </Select>
defaultValue="" </FormControl>
name="targetEnvironment" )}
render={({ field, fieldState: { error } }) => (
<FormControl
label="GitLab Environment Scope (Optional)"
isError={Boolean(error)}
errorText={error?.message}
>
<Input
{...field}
placeholder="*"
/>
</FormControl>
)}
/> />
</div> )}
<Controller
control={control}
name="targetAppId"
render={({ field: { onChange, ...field }, fieldState: { error } }) => {
return (
<FormControl
label="GitLab Project"
errorText={error?.message}
isError={Boolean(error)}
>
<Select
{...field}
onValueChange={(e) => {
if (e === "") return;
onChange(e)
}}
className="w-full"
>
{integrationAuthApps.length > 0 ? (
integrationAuthApps.map((integrationAuthApp) => (
<SelectItem
value={String(integrationAuthApp.appId as string)}
key={`target-app-${String(integrationAuthApp.appId)}`}
>
{integrationAuthApp.name}
</SelectItem>
))
) : (
<SelectItem value="none" key="target-app-none">
No projects found
</SelectItem>
)}
</Select>
</FormControl>
)}}
/>
<Controller
control={control}
defaultValue=""
name="targetEnvironment"
render={({ field, fieldState: { error } }) => (
<FormControl
label="GitLab Environment Scope (Optional)"
isError={Boolean(error)}
errorText={error?.message}
>
<Input
{...field}
placeholder="*"
/>
</FormControl>
)}
/>
</motion.div> </motion.div>
</TabPanel> </TabPanel>
<TabPanel value={TabSections.Options}> <TabPanel value={TabSections.Options}>