From da1fdb85f927e9953d5694e4718fa1baa21df9f7 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 16 Feb 2023 17:04:06 +0800 Subject: [PATCH] Update web to work with latest integration API --- .../components/templates/integrations/Readwise.tsx | 5 +++-- .../networking/mutations/setIntegrationMutation.ts | 14 +++++++------- .../networking/queries/useGetIntegrationsQuery.tsx | 3 ++- packages/web/pages/settings/integrations.tsx | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/web/components/templates/integrations/Readwise.tsx b/packages/web/components/templates/integrations/Readwise.tsx index a6f4de6fc..4e0469896 100644 --- a/packages/web/components/templates/integrations/Readwise.tsx +++ b/packages/web/components/templates/integrations/Readwise.tsx @@ -26,7 +26,7 @@ const Header = styled(Box, { export function Readwise(): JSX.Element { const { integrations, revalidate } = useGetIntegrationsQuery() const readwiseIntegration = useMemo(() => { - return integrations.find((i) => i.type == 'READWISE') + return integrations.find((i) => i.name == 'READWISE' && i.type == 'EXPORT') }, [integrations]) return ( @@ -82,7 +82,8 @@ function AddReadwiseForm(): JSX.Element { try { const result = await setIntegrationMutation({ token, - type: 'READWISE', + name: 'READWISE', + type: 'EXPORT', enabled: true, }) if (result) { diff --git a/packages/web/lib/networking/mutations/setIntegrationMutation.ts b/packages/web/lib/networking/mutations/setIntegrationMutation.ts index cae1ff341..bda8beaf0 100644 --- a/packages/web/lib/networking/mutations/setIntegrationMutation.ts +++ b/packages/web/lib/networking/mutations/setIntegrationMutation.ts @@ -4,8 +4,9 @@ import { IntegrationType } from '../queries/useGetIntegrationsQuery' export type SetIntegrationInput = { id?: string + name: string type: IntegrationType - token: string, + token: string enabled: boolean } @@ -20,6 +21,7 @@ type SetIntegrationData = { type Integration = { id: string + name: string type: IntegrationType token: string enabled: boolean @@ -31,13 +33,12 @@ export async function setIntegrationMutation( input: SetIntegrationInput ): Promise { const mutation = gql` - mutation SetIntegration( - $input: SetIntegrationInput! - ) { + mutation SetIntegration($input: SetIntegrationInput!) { setIntegration(input: $input) { ... on SetIntegrationSuccess { integration { id + name type token enabled @@ -52,12 +53,11 @@ export async function setIntegrationMutation( } ` - const data = await gqlFetcher(mutation, { input }) as SetIntegrationResult + const data = (await gqlFetcher(mutation, { input })) as SetIntegrationResult const output = data as any const error = data.setIntegration?.errorCodes?.find(() => true) if (error) { - if (error === 'INVALID_TOKEN') - throw 'Your token is invalid.' + if (error === 'INVALID_TOKEN') throw 'Your token is invalid.' throw error } return output.setIntegration?.integration diff --git a/packages/web/lib/networking/queries/useGetIntegrationsQuery.tsx b/packages/web/lib/networking/queries/useGetIntegrationsQuery.tsx index e61ef35ae..6b748c0b7 100644 --- a/packages/web/lib/networking/queries/useGetIntegrationsQuery.tsx +++ b/packages/web/lib/networking/queries/useGetIntegrationsQuery.tsx @@ -4,6 +4,7 @@ import { publicGqlFetcher } from '../networkHelpers' export interface Integration { id: string + name: string type: IntegrationType token: string enabled: boolean @@ -11,7 +12,7 @@ export interface Integration { updatedAt: Date } -export type IntegrationType = 'READWISE' +export type IntegrationType = 'EXPORT' | 'IMPORT' interface IntegrationsQueryResponse { isValidating: boolean diff --git a/packages/web/pages/settings/integrations.tsx b/packages/web/pages/settings/integrations.tsx index 02835e9b2..501675c57 100644 --- a/packages/web/pages/settings/integrations.tsx +++ b/packages/web/pages/settings/integrations.tsx @@ -65,7 +65,7 @@ export default function Integrations(): JSX.Element { const router = useRouter() const readwiseConnected = useMemo(() => { - return integrations.find((i) => i.type == 'READWISE') + return integrations.find((i) => i.name == 'READWISE' && i.type == 'EXPORT') }, [integrations]) const deleteIntegration = async (id: string) => {