Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Typescript migration] Migrate AccountSyncCheck to ts #3757

Merged
merged 3 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding more TypeScript type definitions

While the migration adds some type safety, there are opportunities for more comprehensive typing.

Consider adding these type definitions:

interface Account {
  id: string;
  account_id: string;
  // ... other properties
}

interface FailedAccount {
  type: string;
  code: string;
}

interface AccountSyncCheckProps {
  // if any props are needed in the future
}

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