Skip to content

A local-first personal finance app

:recycle: (typescript) ported gocardless auth file to TS (#1941)

Matiss Janis Aboltins8be2389fd947
diff --git a/packages/desktop-client/src/components/modals/CreateAccount.tsx b/packages/desktop-client/src/components/modals/CreateAccount.tsx
index 9ae80ecdc9..dde2f32c85 100644
--- a/packages/desktop-client/src/components/modals/CreateAccount.tsx
+++ b/packages/desktop-client/src/components/modals/CreateAccount.tsx
@@ -1,11 +1,11 @@
import React, { useEffect, useState } from 'react';
-import { useDispatch } from 'react-redux';
-
-import { pushModal } from 'loot-core/src/client/actions/modals';
import { authorizeBank } from '../../gocardless';
+import { useActions } from '../../hooks/useActions';
import useGoCardlessStatus from '../../hooks/useGoCardlessStatus';
+import { type SyncServerStatus } from '../../hooks/useSyncServerStatus';
import { theme } from '../../style';
+import { type CommonModalProps } from '../../types/modals';
import Button, { ButtonWithLoading } from '../common/Button';
import ExternalLink from '../common/ExternalLink';
import Modal from '../common/Modal';
@@ -13,8 +13,16 @@ import Paragraph from '../common/Paragraph';
import Text from '../common/Text';
import View from '../common/View';
-export default function CreateAccount({ modalProps, syncServerStatus }) {
- const dispatch = useDispatch();
+type CreateAccountProps = {
+ modalProps: CommonModalProps;
+ syncServerStatus: SyncServerStatus;
+};
+
+export default function CreateAccount({
+ modalProps,
+ syncServerStatus,
+}: CreateAccountProps) {
+ const actions = useActions();
const [isGoCardlessSetupComplete, setIsGoCardlessSetupComplete] =
useState(null);
@@ -24,19 +32,17 @@ export default function CreateAccount({ modalProps, syncServerStatus }) {
return;
}
- authorizeBank((modal, params) => dispatch(pushModal(modal, params)));
+ authorizeBank(actions.pushModal);
};
const onGoCardlessInit = () => {
- dispatch(
- pushModal('gocardless-init', {
- onSuccess: () => setIsGoCardlessSetupComplete(true),
- }),
- );
+ actions.pushModal('gocardless-init', {
+ onSuccess: () => setIsGoCardlessSetupComplete(true),
+ });
};
const onCreateLocalAccount = () => {
- dispatch(pushModal('add-local-account'));
+ actions.pushModal('add-local-account');
};
const { configured } = useGoCardlessStatus();
diff --git a/packages/desktop-client/src/gocardless.js b/packages/desktop-client/src/gocardless.ts
similarity index 52%
rename from packages/desktop-client/src/gocardless.js
rename to packages/desktop-client/src/gocardless.ts
index 308feb2c9e..fe987b8446 100644
--- a/packages/desktop-client/src/gocardless.js
+++ b/packages/desktop-client/src/gocardless.ts
@@ -1,25 +1,34 @@
+import type { pushModal as pushModalAction } from 'loot-core/src/client/actions/modals';
import { send } from 'loot-core/src/platform/client/fetch';
+import { type GoCardlessToken } from 'loot-core/src/types/models';
-function _authorize(pushModal, upgradingAccountId, { onSuccess, onClose }) {
+function _authorize(
+ pushModal: typeof pushModalAction,
+ upgradingAccountId: string | undefined,
+ {
+ onSuccess,
+ onClose,
+ }: {
+ onSuccess: (data: GoCardlessToken) => Promise<void>;
+ onClose?: () => void;
+ },
+) {
pushModal('gocardless-external-msg', {
onMoveExternal: async ({ institutionId }) => {
- const accessValidForDays = 30;
const resp = await send('gocardless-create-web-token', {
upgradingAccountId,
institutionId,
- accessValidForDays,
+ accessValidForDays: 30,
});
- if (resp.error) return resp;
+ if ('error' in resp) return resp;
const { link, requisitionId } = resp;
window.Actual.openURLInBrowser(link);
- const { error, data } = await send('gocardless-poll-web-token', {
+ return send('gocardless-poll-web-token', {
upgradingAccountId,
requisitionId,
});
-
- return { error, data };
},
onClose,
@@ -27,7 +36,10 @@ function _authorize(pushModal, upgradingAccountId, { onSuccess, onClose }) {
});
}
-export async function authorizeBank(pushModal, { upgradingAccountId } = {}) {
+export async function authorizeBank(
+ pushModal: typeof pushModalAction,
+ { upgradingAccountId }: { upgradingAccountId?: string } = {},
+) {
_authorize(pushModal, upgradingAccountId, {
onSuccess: async data => {
pushModal('select-linked-accounts', {
diff --git a/packages/desktop-client/src/hooks/useSyncServerStatus.ts b/packages/desktop-client/src/hooks/useSyncServerStatus.ts
index 114e58f2f4..492c2bd870 100644
--- a/packages/desktop-client/src/hooks/useSyncServerStatus.ts
+++ b/packages/desktop-client/src/hooks/useSyncServerStatus.ts
@@ -2,7 +2,7 @@ import { useSelector } from 'react-redux';
import { useServerURL } from '../components/ServerContext';
-type SyncServerStatus = 'offline' | 'no-server' | 'online';
+export type SyncServerStatus = 'offline' | 'no-server' | 'online';
export default function useSyncServerStatus(): SyncServerStatus {
const serverUrl = useServerURL();
diff --git a/packages/loot-core/src/client/state-types/modals.d.ts b/packages/loot-core/src/client/state-types/modals.d.ts
index bd2f36d31a..517f1778aa 100644
--- a/packages/loot-core/src/client/state-types/modals.d.ts
+++ b/packages/loot-core/src/client/state-types/modals.d.ts
@@ -1,4 +1,4 @@
-import type { AccountEntity } from '../../types/models';
+import type { AccountEntity, GoCardlessToken } from '../../types/models';
import type { RuleEntity } from '../../types/models/rule';
import type { EmptyObject, StripNever } from '../../types/util';
import type * as constants from '../constants';
@@ -33,7 +33,7 @@ type FinanceModals = {
'select-linked-accounts': {
accounts: unknown[];
requisitionId: string;
- upgradingAccountId: string;
+ upgradingAccountId?: string;
};
'confirm-category-delete': { onDelete: () => void } & (
@@ -67,7 +67,7 @@ type FinanceModals = {
institutionId: string;
}) => Promise<{ error: string } | { data: unknown }>;
onClose?: () => void;
- onSuccess: (data: unknown) => Promise<void>;
+ onSuccess: (data: GoCardlessToken) => Promise<void>;
};
'create-encryption-key': { recreate?: boolean };
diff --git a/packages/loot-core/src/server/main.ts b/packages/loot-core/src/server/main.ts
index 52802c9365..45bf72bf6e 100644
--- a/packages/loot-core/src/server/main.ts
+++ b/packages/loot-core/src/server/main.ts
@@ -1043,7 +1043,7 @@ handlers['gocardless-poll-web-token'] = async function ({
requisitionId,
}) {
let userToken = await asyncStorage.getItem('user-token');
- if (!userToken) return null;
+ if (!userToken) return { error: 'unknown' };
let startTime = Date.now();
stopPolling = false;
diff --git a/packages/loot-core/src/types/models/gocardless.d.ts b/packages/loot-core/src/types/models/gocardless.d.ts
new file mode 100644
index 0000000000..17893d1dc0
--- /dev/null
+++ b/packages/loot-core/src/types/models/gocardless.d.ts
@@ -0,0 +1,4 @@
+export type GoCardlessToken = {
+ id: string;
+ accounts: unknown[];
+};
diff --git a/packages/loot-core/src/types/models/index.d.ts b/packages/loot-core/src/types/models/index.d.ts
index 1ae5a25bfa..d411ff64eb 100644
--- a/packages/loot-core/src/types/models/index.d.ts
+++ b/packages/loot-core/src/types/models/index.d.ts
@@ -1,6 +1,7 @@
export type * from './account';
export type * from './category';
export type * from './category-group';
+export type * from './gocardless';
export type * from './payee';
export type * from './rule';
export type * from './schedule';
diff --git a/packages/loot-core/src/types/server-handlers.d.ts b/packages/loot-core/src/types/server-handlers.d.ts
index 65d85e18f5..87f9fbdc4c 100644
--- a/packages/loot-core/src/types/server-handlers.d.ts
+++ b/packages/loot-core/src/types/server-handlers.d.ts
@@ -6,7 +6,12 @@ import { RemoteFile } from '../server/cloud-storage';
import { Node as SpreadsheetNode } from '../server/spreadsheet/spreadsheet';
import { Message } from '../server/sync';
-import { AccountEntity, CategoryEntity, CategoryGroupEntity } from './models';
+import {
+ AccountEntity,
+ CategoryEntity,
+ CategoryGroupEntity,
+ GoCardlessToken,
+} from './models';
import { EmptyObject } from './util';
export interface ServerHandlers {
@@ -196,9 +201,11 @@ export interface ServerHandlers {
'secret-check': (arg: string) => Promise<string | { error?: string }>;
'gocardless-poll-web-token': (arg: {
- upgradingAccountId;
- requisitionId;
- }) => Promise<{ error } | { data }>;
+ upgradingAccountId?: string;
+ requisitionId: string;
+ }) => Promise<
+ { error: 'unknown' } | { error: 'timeout' } | { data: GoCardlessToken }
+ >;
'gocardless-status': () => Promise<{ configured: boolean }>;
@@ -207,10 +214,17 @@ export interface ServerHandlers {
'gocardless-poll-web-token-stop': () => Promise<'ok'>;
'gocardless-create-web-token': (arg: {
- upgradingAccountId;
- institutionId;
- accessValidForDays;
- }) => Promise<unknown>;
+ upgradingAccountId?: string;
+ institutionId: string;
+ accessValidForDays: number;
+ }) => Promise<
+ | {
+ requisitionId: string;
+ link: string;
+ }
+ | { error: 'unauthorized' }
+ | { error: 'failed' }
+ >;
'gocardless-accounts-sync': (arg: { id }) => Promise<{
errors;
diff --git a/upcoming-release-notes/1941.md b/upcoming-release-notes/1941.md
new file mode 100644
index 0000000000..b9c937e796
--- /dev/null
+++ b/upcoming-release-notes/1941.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [MatissJanis]
+---
+
+Ported `gocardless` file to TypeScript