From 06803519e68b9a60c9cd636117952e0554bf0d99 Mon Sep 17 00:00:00 2001 From: Tuan Dang Date: Wed, 18 Jan 2023 17:38:52 +0700 Subject: [PATCH] Reduce page reloads for integrations page --- .../controllers/v1/integrationController.ts | 14 +-- .../components/integrations/Integration.tsx | 91 +++++++++++++------ .../integrations/IntegrationSection.tsx | 23 ++++- .../api/integrations/DeleteIntegration.ts | 2 +- .../api/integrations/updateIntegration.ts | 2 +- frontend/src/pages/integrations/[id].tsx | 8 +- 6 files changed, 100 insertions(+), 40 deletions(-) diff --git a/backend/src/controllers/v1/integrationController.ts b/backend/src/controllers/v1/integrationController.ts index 4f02836a1d..6fcb02b5ad 100644 --- a/backend/src/controllers/v1/integrationController.ts +++ b/backend/src/controllers/v1/integrationController.ts @@ -92,24 +92,24 @@ export const updateIntegration = async (req: Request, res: Response) => { * @returns */ export const deleteIntegration = async (req: Request, res: Response) => { - let deletedIntegration; + let integration; try { const { integrationId } = req.params; - deletedIntegration = await Integration.findOneAndDelete({ + integration = await Integration.findOneAndDelete({ _id: integrationId }); - if (!deletedIntegration) throw new Error('Failed to find integration'); + if (!integration) throw new Error('Failed to find integration'); const integrations = await Integration.find({ - workspace: deletedIntegration.workspace + workspace: integration.workspace }); if (integrations.length === 0) { // case: no integrations left, deactivate bot const bot = await Bot.findOneAndUpdate({ - workspace: deletedIntegration.workspace + workspace: integration.workspace }, { isActive: false }, { @@ -129,8 +129,8 @@ export const deleteIntegration = async (req: Request, res: Response) => { message: 'Failed to delete integration' }); } - + return res.status(200).send({ - deletedIntegration + integration }); }; diff --git a/frontend/src/components/integrations/Integration.tsx b/frontend/src/components/integrations/Integration.tsx index fc034637cb..acba9fdf41 100644 --- a/frontend/src/components/integrations/Integration.tsx +++ b/frontend/src/components/integrations/Integration.tsx @@ -32,10 +32,21 @@ interface IntegrationApp { type Props = { integration: TIntegration; + integrations: TIntegration[]; + setIntegrations: any; + bot: any; + setBot: any; environments: Array<{ name: string; slug: string }>; }; -const Integration = ({ integration, environments = [] }: Props) => { +const Integration = ({ + integration, + integrations, + bot, + setBot, + setIntegrations, + environments = [] +}: Props) => { // set initial environment. This find will only execute when component is mounting const [integrationEnvironment, setIntegrationEnvironment] = useState( environments.find(({ slug }) => slug === integration.environment) || { @@ -79,6 +90,56 @@ const Integration = ({ integration, environments = [] }: Props) => { loadIntegration(); }, []); + + const handleStartIntegration = async () => { + try { + const siteApp = apps.find((app) => app.name === integrationApp); // obj or undefined + const siteId = siteApp?.siteId ?? null; + const owner = siteApp?.owner ?? null; + + // return updated integration + const updatedIntegration = await updateIntegration({ + integrationId: integration._id, + environment: integrationEnvironment.slug, + app: integrationApp, + isActive: true, + target: integrationTarget ? integrationTarget.toLowerCase() : null, + context: integrationContext + ? reverseContextNetlifyMapping[integrationContext] + : null, + siteId, + owner + }); + + setIntegrations( + integrations.map((i) => i._id === updatedIntegration._id ? updatedIntegration : i) + ); + } catch (err) { + console.error(err); + } + } + + const handleDeleteIntegration = async () => { + try { + const deletedIntegration = await deleteIntegration({ + integrationId: integration._id + }); + + const newIntegrations = integrations.filter((i) => i._id !== deletedIntegration._id); + setIntegrations(newIntegrations); + + if (newIntegrations.length < 1) { + // case: no integrations left + setBot({ + ...bot, + isActive: false + }) + } + + } catch (err) { + console.error(err); + } + } // eslint-disable-next-line @typescript-eslint/no-shadow const renderIntegrationSpecificParams = (integration: TIntegration) => { @@ -172,38 +233,14 @@ const Integration = ({ integration, environments = [] }: Props) => { ) : (