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

[WIP] ♻️ (typescript) convert TransactionListWithBalances.jsx to .tsx #3876

Closed
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
Expand Up @@ -43,6 +43,7 @@ import { Button } from '../../common/Button2';
import { Text } from '../../common/Text';
import { View } from '../../common/View';
import { MobilePageHeader, Page } from '../../Page';
import { type SheetFields, type SheetNames } from '../../spreadsheet/index';
import { MobileBackButton } from '../MobileBackButton';
import { AddTransactionButton } from '../transactions/AddTransactionButton';
import { TransactionListWithBalances } from '../transactions/TransactionListWithBalances';
Expand Down Expand Up @@ -247,9 +248,9 @@ function TransactionListWithPreviews({
const dispatch = useDispatch();
const navigate = useNavigate();

const onRefresh = useCallback(() => {
const onRefresh = useCallback(async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this need to be async?

if (accountId) {
dispatch(syncAndDownload(accountId));
await dispatch(syncAndDownload(accountId));
}
}, [accountId, dispatch]);

Expand Down Expand Up @@ -314,16 +315,20 @@ function TransactionListWithPreviews({
);

const transactionsToDisplay = !isSearching
? previewTransactions.concat(transactions)
Copy link
Author

Choose a reason for hiding this comment

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

The original code was resulting in a readonly version of transactions.

Copy link
Contributor

Choose a reason for hiding this comment

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

That is intentional. We should instead update transactions prop of TransactionListWithBalances as readonly

: transactions;
? [...previewTransactions, ...transactions]
: [...transactions];

return (
<TransactionListWithBalances
isLoading={isLoading}
transactions={transactionsToDisplay}
balance={balanceQueries.balance}
balanceCleared={balanceQueries.cleared}
balanceUncleared={balanceQueries.uncleared}
balance={balanceQueries.balance.name as SheetFields<SheetNames>}
balanceCleared={
(balanceQueries.cleared?.name as SheetFields<SheetNames>) ?? undefined
Copy link
Contributor

Choose a reason for hiding this comment

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

This will change the behavior. This is ignoring the query property of the binding. These should be Bindings instead.

}
balanceUncleared={
(balanceQueries.uncleared?.name as SheetFields<SheetNames>) ?? undefined
}
onLoadMore={loadMoreTransactions}
searchPlaceholder={`Search ${accountName}`}
onSearch={onSearch}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ import React, { useState } from 'react';

import { t } from 'i18next';

import { type TransactionEntity } from 'loot-core/types/models';

import { SelectedProvider, useSelected } from '../../../hooks/useSelected';
import { SvgSearchAlternate } from '../../../icons/v2';
import { styles, theme } from '../../../style';
import { InputWithContent } from '../../common/InputWithContent';
import { Label } from '../../common/Label';
import { View } from '../../common/View';
import { CellValue, CellValueText } from '../../spreadsheet/CellValue';
import { type SheetFields, type SheetNames } from '../../spreadsheet/index';
import { useSheetValue } from '../../spreadsheet/useSheetValue';
import { PullToRefresh } from '../PullToRefresh';

import { TransactionList } from './TransactionList';

function TransactionSearchInput({ placeholder, onSearch }) {
const [text, setText] = useState('');
function TransactionSearchInput({
placeholder,
onSearch,
}: {
placeholder: string;
onSearch: (searchText: string) => void;
}) {
const [text, setText] = useState<string>('');

return (
<View
Expand Down Expand Up @@ -57,6 +66,19 @@ function TransactionSearchInput({ placeholder, onSearch }) {
);
}

type TransactionListWithBalancesProps = {
isLoading: boolean | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
isLoading: boolean | undefined;
isLoading?: boolean;

transactions: TransactionEntity[];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
transactions: TransactionEntity[];
transactions: readonly TransactionEntity[];

balance: SheetFields<SheetNames>;
balanceCleared: SheetFields<SheetNames> | undefined;
balanceUncleared: SheetFields<SheetNames> | undefined;
searchPlaceholder: string;
onSearch: (searchText: string) => void;
onLoadMore: (() => void) | undefined;
onOpenTransaction: (transaction: TransactionEntity) => void;
onRefresh: () => Promise<void>;
};

export function TransactionListWithBalances({
isLoading,
transactions,
Expand All @@ -68,51 +90,61 @@ export function TransactionListWithBalances({
onLoadMore,
onOpenTransaction,
onRefresh,
}) {
const selectedInst = useSelected('transactions', transactions);
}: TransactionListWithBalancesProps) {
const selectedInst = useSelected('transactions', transactions, []);

return (
<SelectedProvider instance={selectedInst}>
<View
style={{
flexShrink: 0,
marginTop: 10,
}}
>
<>
Copy link
Author

@cindywu cindywu Nov 22, 2024

Choose a reason for hiding this comment

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

The only change here is I wrapped the two children in a <></> so that there is only a single child inside <SelectedProvider></SelectedProvider>. I did not touch the components inside the <></>

<View
style={{
flexDirection: 'row',
justifyContent: 'space-evenly',
flexShrink: 0,
marginTop: 10,
}}
>
{balanceCleared && balanceUncleared ? (
<BalanceWithCleared
balance={balance}
balanceCleared={balanceCleared}
balanceUncleared={balanceUncleared}
/>
) : (
<Balance balance={balance} />
)}
<View
style={{
flexDirection: 'row',
justifyContent: 'space-evenly',
}}
>
{balanceCleared && balanceUncleared ? (
<BalanceWithCleared
balance={balance}
balanceCleared={balanceCleared}
balanceUncleared={balanceUncleared}
/>
) : (
<Balance balance={balance} />
)}
</View>
<TransactionSearchInput
placeholder={searchPlaceholder}
onSearch={onSearch}
/>
</View>
<TransactionSearchInput
placeholder={searchPlaceholder}
onSearch={onSearch}
/>
</View>
<PullToRefresh isPullable={!!onRefresh} onRefresh={onRefresh}>
<TransactionList
isLoading={isLoading}
transactions={transactions}
onLoadMore={onLoadMore}
onOpenTransaction={onOpenTransaction}
/>
</PullToRefresh>
<PullToRefresh isPullable={!!onRefresh} onRefresh={onRefresh}>
<TransactionList
isLoading={isLoading}
transactions={transactions}
onLoadMore={onLoadMore}
onOpenTransaction={onOpenTransaction}
/>
</PullToRefresh>
</>
</SelectedProvider>
);
}

function BalanceWithCleared({ balanceUncleared, balanceCleared, balance }) {
function BalanceWithCleared({
balanceUncleared,
balanceCleared,
balance,
}: {
balanceUncleared: SheetFields<SheetNames>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please make a BalanceWithClearedProps type

balanceCleared: SheetFields<SheetNames>;
balance: SheetFields<SheetNames>;
}) {
const unclearedAmount = useSheetValue(balanceUncleared);

return (
Expand Down Expand Up @@ -170,7 +202,7 @@ function BalanceWithCleared({ balanceUncleared, balanceCleared, balance }) {
);
}

function Balance({ balance }) {
function Balance({ balance }: { balance: SheetFields<SheetNames> }) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please make a BalanceProps

return (
<View style={{ flexBasis: '33%' }}>
<Label title={t('Balance')} style={{ textAlign: 'center' }} />
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3876.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [cindywu]
---

Convert TransactionListWithBalances.jsx to .tsx
Loading