From c64cf39b6990764edfb42b03471a4a7f1b3892d9 Mon Sep 17 00:00:00 2001 From: Stijn-Kuijper Date: Tue, 13 Jun 2023 12:37:07 +0200 Subject: [PATCH] feat: cloudflare pages integration create page --- .../integrations/cloudflare-pages/create.tsx | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/frontend/src/pages/integrations/cloudflare-pages/create.tsx b/frontend/src/pages/integrations/cloudflare-pages/create.tsx index e69de29bb..19d4e39b1 100644 --- a/frontend/src/pages/integrations/cloudflare-pages/create.tsx +++ b/frontend/src/pages/integrations/cloudflare-pages/create.tsx @@ -0,0 +1,135 @@ +import { useState, useEffect } from 'react'; +import { useRouter } from 'next/router'; +import { useGetWorkspaceById } from '~/hooks/api'; +import queryString from 'query-string'; +import { useGetIntegrationAuthApps, useGetIntegrationAuthById } from '~/hooks/api/integrationAuth'; +import createIntegration from '~/pages/api/integrations/createIntegration'; +import { Button, Card, CardTitle, FormControl, Select, SelectItem } from '~/components/v2'; + +export default function CloudflarePagesIntegrationPage() { + const router = useRouter(); + + const { integrationAuthId } = queryString.parse(router.asPath.split('?')[1]); + const { data: workspace } = useGetWorkspaceById(localStorage.getItem('projectData.id') ?? ''); + const { data: integrationAuth } = useGetIntegrationAuthById((integrationAuthId as string) ?? ''); + const { data: integrationAuthApps } = useGetIntegrationAuthApps({ + integrationAuthId: (integrationAuthId as string) ?? '' + }); + + const [selectedSourceEnvironment, setSelectedSourceEnvironment] = useState(''); + const [targetApp, setTargetApp] = useState(''); + const [targetAppId, setTargetAppId] = useState(''); + + const [isLoading, setIsLoading] = useState(false); + + useEffect(() => { + if (workspace) { + setSelectedSourceEnvironment(workspace.environments[0].slug); + } + }, [workspace]); + + useEffect(() => { + if (integrationAuthApps) { + if (integrationAuthApps.length > 0) { + setTargetApp(integrationAuthApps[0].name); + setTargetAppId(String(integrationAuthApps[0].appId)); + } else { + setTargetApp('none'); + } + } + }, [integrationAuthApps]); + + const handleButtonClick = async () => { + try { + if (!integrationAuth?._id) return; + + setIsLoading(true); + + await createIntegration({ + integrationAuthId: integrationAuth?._id, + isActive: true, + app: targetApp, + appId: targetAppId, + sourceEnvironment: selectedSourceEnvironment, + targetEnvironment: null, + targetEnvironmentId: null, + targetService: null, + targetServiceId: null, + owner: null, + path: null, + region: null + }); + + setIsLoading(false); + + router.push(`/integrations/${localStorage.getItem('projectData.id')}`); + } catch(err) { + console.error(err); + } + } + + return integrationAuth && + workspace && + selectedSourceEnvironment && + integrationAuthApps && + targetApp ? ( +
+ + Cloudflare Pages Integration + + + + + + + + +
+ ) : ( +
+ ); +} + +CloudflarePagesIntegrationPage.requireAuth = true; \ No newline at end of file