diff --git a/apps/web/app/hooks/integrations/useGitHubIntegration.ts b/apps/web/app/hooks/integrations/useGitHubIntegration.ts index ee67b355e..7e127bf31 100644 --- a/apps/web/app/hooks/integrations/useGitHubIntegration.ts +++ b/apps/web/app/hooks/integrations/useGitHubIntegration.ts @@ -115,6 +115,7 @@ export function useGitHubIntegration() { repositoriesLoading, integrationGithubMetadata, integrationGithubRepositories, + setIntegrationGithubRepositories, syncGitHubRepository, syncGitHubRepositoryLoading }; diff --git a/apps/web/app/hooks/integrations/useIntegrationTenant.ts b/apps/web/app/hooks/integrations/useIntegrationTenant.ts index cde2ae551..471512911 100644 --- a/apps/web/app/hooks/integrations/useIntegrationTenant.ts +++ b/apps/web/app/hooks/integrations/useIntegrationTenant.ts @@ -1,13 +1,17 @@ -import { getIntegrationTenantAPI } from '@app/services/client/api'; +import { deleteIntegrationTenantAPI, getIntegrationTenantAPI } from '@app/services/client/api'; import { integrationTenantState } from '@app/stores'; import { useCallback } from 'react'; import { useRecoilState } from 'recoil'; import { useQuery } from '../useQuery'; +import { useGitHubIntegration } from './useGitHubIntegration'; export function useIntegrationTenant() { const [integrationTenant, setIntegrationTenant] = useRecoilState(integrationTenantState); + const { setIntegrationGithubRepositories } = useGitHubIntegration(); + const { loading: loading, queryCall: queryCall } = useQuery(getIntegrationTenantAPI); + const { loading: deleteLoading, queryCall: deleteQueryCall } = useQuery(deleteIntegrationTenantAPI); const getIntegrationTenant = useCallback( (name: string) => { @@ -20,9 +24,21 @@ export function useIntegrationTenant() { [queryCall, setIntegrationTenant] ); + const deleteIntegrationTenant = useCallback( + (integrationId: string) => { + return deleteQueryCall(integrationId).then(() => { + setIntegrationTenant([]); + setIntegrationGithubRepositories(null); + }); + }, + [deleteQueryCall, setIntegrationTenant, setIntegrationGithubRepositories] + ); + return { loading, getIntegrationTenant, - integrationTenant + integrationTenant, + deleteIntegrationTenant, + deleteLoading }; } diff --git a/apps/web/app/services/client/api/integrations/integration-tenant.ts b/apps/web/app/services/client/api/integrations/integration-tenant.ts index 2efd0651b..c53183704 100644 --- a/apps/web/app/services/client/api/integrations/integration-tenant.ts +++ b/apps/web/app/services/client/api/integrations/integration-tenant.ts @@ -1,4 +1,4 @@ -import { IIntegrationTenant, PaginationResponse, CreateResponse } from '@app/interfaces'; +import { IIntegrationTenant, PaginationResponse, CreateResponse, DeleteResponse } from '@app/interfaces'; import api from '../../axios'; export function getIntegrationTenantAPI(name: string) { @@ -6,3 +6,7 @@ export function getIntegrationTenantAPI(name: string) { `/integration-tenant/remember/state?name=${name}` ); } + +export function deleteIntegrationTenantAPI(integrationId: string) { + return api.delete(`/integration-tenant/${integrationId}`); +} diff --git a/apps/web/app/services/server/requests/integrations/integration-tenant.ts b/apps/web/app/services/server/requests/integrations/integration-tenant.ts index e557c6ee2..8f612b43f 100644 --- a/apps/web/app/services/server/requests/integrations/integration-tenant.ts +++ b/apps/web/app/services/server/requests/integrations/integration-tenant.ts @@ -15,7 +15,7 @@ export function getIntegrationTenantRequest( const query = new URLSearchParams({ 'where[organizationId]': organizationId, 'where[tenantId]': tenantId, - 'where[name]': name, + 'where[name]': name }); return serverFetch({ path: `/integration-tenant?${query.toString()}`, @@ -24,3 +24,12 @@ export function getIntegrationTenantRequest( tenantId: tenantId }); } + +export function deleteIntegrationTenantRequest(integrationId: string, tenantId: string, bearer_token: string) { + return serverFetch({ + path: `/integration-tenant/${integrationId}`, + method: 'DELETE', + bearer_token, + tenantId + }); +} diff --git a/apps/web/lib/settings/integration-setting.tsx b/apps/web/lib/settings/integration-setting.tsx index af08ae335..602087332 100644 --- a/apps/web/lib/settings/integration-setting.tsx +++ b/apps/web/lib/settings/integration-setting.tsx @@ -57,7 +57,12 @@ export const IntegrationSetting = () => { const { editOrganizationProjectSetting, editOrganizationProject } = useOrganizationProjects(); - const { getIntegrationTenant, loading: integrationTenantLoading, integrationTenant } = useIntegrationTenant(); + const { + getIntegrationTenant, + loading: integrationTenantLoading, + integrationTenant, + deleteIntegrationTenant + } = useIntegrationTenant(); useEffect(() => { if (integrationTenant && integrationTenant.length) { @@ -149,6 +154,15 @@ export const IntegrationSetting = () => { [editOrganizationProject] ); + const handleDeleteIntegrationTenant = useCallback(() => { + if (integrationTenant && integrationTenant.length) { + deleteIntegrationTenant(integrationTenant[0].id).then(() => { + getIntegrationTenant('Github'); + setSelectedRepo(undefined); + }); + } + }, [integrationTenant, deleteIntegrationTenant, getIntegrationTenant]); + return (
@@ -198,6 +212,9 @@ export const IntegrationSetting = () => { )} +
)}
diff --git a/apps/web/pages/api/integration-tenant/[id].ts b/apps/web/pages/api/integration-tenant/[id].ts new file mode 100644 index 000000000..22f03b679 --- /dev/null +++ b/apps/web/pages/api/integration-tenant/[id].ts @@ -0,0 +1,20 @@ +import { authenticatedGuard } from '@app/services/server/guards/authenticated-guard'; +import { deleteIntegrationTenantRequest } from '@app/services/server/requests'; +import { NextApiRequest, NextApiResponse } from 'next'; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + const { $res, user, access_token, tenantId } = await authenticatedGuard(req, res); + if (!user) return $res(); + + const { id } = req.query; + + if (req.method !== 'DELETE') { + return $res.status(405).json({}); + } + + if (id) { + const response = await deleteIntegrationTenantRequest(id as string, tenantId, access_token); + + return $res.status(200).json(response); + } +} diff --git a/apps/web/pages/integration/github.tsx b/apps/web/pages/integration/github.tsx index 9d3081261..bfbe5ef3a 100644 --- a/apps/web/pages/integration/github.tsx +++ b/apps/web/pages/integration/github.tsx @@ -2,11 +2,13 @@ import { useIntegrationTenant, useIntegrationTypes } from '@app/hooks'; import { useGitHubIntegration } from '@app/hooks/integrations/useGitHubIntegration'; import { withAuthentication } from 'lib/app/authenticator'; import { useRouter } from 'next/router'; -import { useEffect } from 'react'; +import { useCallback, useEffect, useRef } from 'react'; const GitHub = () => { const router = useRouter(); + const installing = useRef(false); + const { installGitHub, getRepositories } = useGitHubIntegration(); // const { loading: integrationLoading } = useIntegration(); const { getIntegrationTenant, loading: integrationTenantLoading, integrationTenant } = useIntegrationTenant(); @@ -20,7 +22,9 @@ const GitHub = () => { // const queries = new URLSearchParams(params || {}); // const url = `https://github.com/apps/badal-ever-testing-probot/installations/new?${queries.toString()}`; - useEffect(() => { + const handleInstallGitHub = useCallback(() => { + installing.current = true; + if (router && router.query.installation_id && router.query.setup_action) { setTimeout(() => { installGitHub(router.query.installation_id as string, router.query.setup_action as string).then(() => { @@ -30,6 +34,14 @@ const GitHub = () => { } }, [installGitHub, router]); + useEffect(() => { + if (installing.current) { + return; + } + + handleInstallGitHub(); + }, [handleInstallGitHub]); + useEffect(() => { if (!integrationTenantLoading && integrationTenant && integrationTenant.length && integrationTenant[0]?.id) { getRepositories(integrationTenant[0].id);