Skip to content

Commit

Permalink
[Typescript migration] Migrate AccountSyncCheck to ts (#3757)
Browse files Browse the repository at this point in the history
* Migrate AccountSyncCheck to ts

* Release notes

* Fix lint
  • Loading branch information
joel-jeremy authored Nov 3, 2024
1 parent 41d5922 commit e078ed2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { useRef, useState } from 'react';
import React, { useCallback, useRef, useState } from 'react';
import { Trans } from 'react-i18next';
import { useDispatch, useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';

import { t } from 'i18next';

import { unlinkAccount } from 'loot-core/client/actions';
import { type AccountEntity } from 'loot-core/types/models';

import { authorizeBank } from '../../gocardless';
import { useAccounts } from '../../hooks/useAccounts';
Expand All @@ -16,7 +17,7 @@ import { Link } from '../common/Link';
import { Popover } from '../common/Popover';
import { View } from '../common/View';

function getErrorMessage(type, code) {
function getErrorMessage(type: string, code: string) {
switch (type.toUpperCase()) {
case 'ITEM_ERROR':
switch (code.toUpperCase()) {
Expand Down Expand Up @@ -81,7 +82,29 @@ export function AccountSyncCheck() {
const [open, setOpen] = useState(false);
const triggerRef = useRef(null);

if (!failedAccounts) {
const reauth = useCallback(
(acc: AccountEntity) => {
setOpen(false);

if (acc.account_id) {
authorizeBank(dispatch, { upgradingAccountId: acc.account_id });
}
},
[dispatch],
);

const unlink = useCallback(
(acc: AccountEntity) => {
if (acc.id) {
dispatch(unlinkAccount(acc.id));
}

setOpen(false);
},
[dispatch],
);

if (!failedAccounts || !id) {
return null;
}

Expand All @@ -91,22 +114,15 @@ export function AccountSyncCheck() {
}

const account = accounts.find(account => account.id === id);
if (!account) {
return null;
}

const { type, code } = error;
const showAuth =
(type === 'ITEM_ERROR' && code === 'ITEM_LOGIN_REQUIRED') ||
(type === 'INVALID_INPUT' && code === 'INVALID_ACCESS_TOKEN');

function reauth() {
setOpen(false);

authorizeBank(dispatch, { upgradingAccountId: account.account_id });
}

async function unlink() {
dispatch(unlinkAccount(account.id));
setOpen(false);
}

return (
<View>
<Button
Expand Down Expand Up @@ -148,20 +164,20 @@ export function AccountSyncCheck() {
<View style={{ justifyContent: 'flex-end', flexDirection: 'row' }}>
{showAuth ? (
<>
<Button onPress={unlink}>
<Button onPress={() => unlink(account)}>
<Trans>Unlink</Trans>
</Button>
<Button
variant="primary"
autoFocus
onPress={reauth}
onPress={() => reauth(account)}
style={{ marginLeft: 5 }}
>
<Trans>Reauthorize</Trans>
</Button>
</>
) : (
<Button onPress={unlink}>
<Button onPress={() => unlink(account)}>
<Trans>Unlink account</Trans>
</Button>
)}
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop-client/src/components/budget/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function separateGroups(categoryGroups: CategoryGroupEntity[]) {
return [
categoryGroups.filter(g => !g.is_income),
categoryGroups.find(g => g.is_income),
];
] as const;
}

export function makeAmountGrey(value: number | string): CSSProperties {
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3757.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [joel-jeremy]
---

Migrate AccountSyncCheck.jsx file to typescript

0 comments on commit e078ed2

Please sign in to comment.