Skip to content

Commit

Permalink
Added Make Transfer button to mobile.
Browse files Browse the repository at this point in the history
  • Loading branch information
tempiz committed Mar 3, 2025
1 parent eeeb1d3 commit bc29314
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ function TransactionListWithPreviews({
onSearch={onSearch}
onOpenTransaction={onOpenTransaction}
onRefresh={onRefresh}
account={account}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import { theme } from '../../../style';
import { useScrollListener } from '../../ScrollProvider';
import { FloatingActionBar } from '../FloatingActionBar';

import { validForTransfer } from 'loot-core/client/transfer';
import {type AccountEntity} from 'loot-core/types/models';
import { TransactionListItem } from './TransactionListItem';

const NOTIFICATION_BOTTOM_INSET = 75;
Expand Down Expand Up @@ -72,6 +74,7 @@ type TransactionListProps = {
onOpenTransaction?: (transaction: TransactionEntity) => void;
isLoadingMore: boolean;
onLoadMore: () => void;
account: AccountEntity;
};

export function TransactionList({
Expand All @@ -80,6 +83,7 @@ export function TransactionList({
onOpenTransaction,
isLoadingMore,
onLoadMore,
account,
}: TransactionListProps) {
const { t } = useTranslation();
const sections = useMemo(() => {
Expand Down Expand Up @@ -205,7 +209,11 @@ export function TransactionList({
)}

{selectedTransactions.size > 0 && (
<SelectedTransactionsFloatingActionBar transactions={transactions} />
<SelectedTransactionsFloatingActionBar
transactions={transactions}
getTransaction={id => transactions.find(t => t.id === id)}
account={account}
/>
)}
</>
);
Expand All @@ -214,11 +222,15 @@ export function TransactionList({
type SelectedTransactionsFloatingActionBarProps = {
transactions: readonly TransactionEntity[];
style?: CSSProperties;
getTransaction: (id: string) => TransactionEntity | undefined;
account: AccountEntity;
};

function SelectedTransactionsFloatingActionBar({
transactions,
getTransaction,
style = {},
account,
}: SelectedTransactionsFloatingActionBarProps) {
const editMenuTriggerRef = useRef(null);
const [isEditMenuOpen, setIsEditMenuOpen] = useState(false);
Expand All @@ -232,8 +244,11 @@ function SelectedTransactionsFloatingActionBar({
}),
[],
);
// only show the make transfer button on all accounts view.
var showMakeTransfer = !account;
const selectedTransactions = useSelectedItems();
const selectedTransactionsArray = Array.from(selectedTransactions);
const selectedIds = useMemo(() => [...selectedTransactions], [selectedTransactions]);
const dispatchSelected = useSelectedDispatch();

const buttonProps = useMemo(
Expand All @@ -253,6 +268,21 @@ function SelectedTransactionsFloatingActionBar({
[],
);

const canBeTransfer = useMemo(() => {
// only two selected
if (selectedIds.length !== 2) {
return false;
}
const fromTrans = getTransaction(selectedIds[0]);
const toTrans = getTransaction(selectedIds[1]);

// previously selected transactions aren't always present in current transaction list
if (!fromTrans || !toTrans) {
return false;
}

return validForTransfer(fromTrans, toTrans);
}, [selectedIds, getTransaction]);
const allTransactionsAreLinked = useMemo(() => {
return transactions
.filter(t => selectedTransactions.has(t.id))
Expand All @@ -268,6 +298,7 @@ function SelectedTransactionsFloatingActionBar({
onBatchDelete,
onBatchLinkSchedule,
onBatchUnlinkSchedule,
onSetTransfer,
} = useTransactionBatchActions();

const navigate = useNavigate();
Expand Down Expand Up @@ -509,6 +540,8 @@ function SelectedTransactionsFloatingActionBar({
});
},
});
} else if (type === 'set-transfer') {
onSetTransfer(selectedIds);
}
setIsMoreOptionsMenuOpen(false);
}}
Expand All @@ -530,6 +563,14 @@ function SelectedTransactionsFloatingActionBar({
text: 'Link schedule',
},
]),
...(showMakeTransfer && canBeTransfer
? [
{
name: 'set-transfer',
text: ('Make transfer'),
} as const,
]
: []),
{
name: 'delete',
text: 'Delete',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useSheetValue } from '../../spreadsheet/useSheetValue';
import { PullToRefresh } from '../PullToRefresh';

import { TransactionList } from './TransactionList';
import {type AccountEntity} from 'loot-core/types/models';

type TransactionSearchInputProps = {
placeholder: string;
Expand Down Expand Up @@ -91,6 +92,7 @@ type TransactionListWithBalancesProps = {
onLoadMore: () => void;
onOpenTransaction: (transaction: TransactionEntity) => void;
onRefresh?: () => void;
account: AccountEntity;
};

export function TransactionListWithBalances({
Expand All @@ -105,6 +107,7 @@ export function TransactionListWithBalances({
onLoadMore,
onOpenTransaction,
onRefresh,
account,
}: TransactionListWithBalancesProps) {
const selectedInst = useSelected('transactions', [...transactions], []);

Expand Down Expand Up @@ -148,6 +151,7 @@ export function TransactionListWithBalances({
isLoadingMore={isLoadingMore}
onLoadMore={onLoadMore}
onOpenTransaction={onOpenTransaction}
account={account}
/>
</PullToRefresh>
</>
Expand Down
48 changes: 48 additions & 0 deletions packages/desktop-client/src/hooks/useTransactionBatchActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import {
type TransactionEntity,
} from 'loot-core/types/models';

import { validForTransfer } from 'loot-core/client/transfer';
import { useDispatch } from '../redux';
import { usePayees } from './usePayees';

type BatchEditProps = {
name: keyof TransactionEntity;
Expand Down Expand Up @@ -57,6 +59,7 @@ type BatchUnlinkScheduleProps = {
export function useTransactionBatchActions() {
const dispatch = useDispatch();

const payees = usePayees();
const onBatchEdit = async ({ name, ids, onSuccess }: BatchEditProps) => {
const { data } = await runQuery(
q('transactions')
Expand Down Expand Up @@ -277,6 +280,50 @@ export function useTransactionBatchActions() {
);
};

const onSetTransfer = async (ids: string[]) => {
const onConfirmTransfer = async (ids: string[]) => {

const { data: transactions } = await runQuery(
q('transactions')
.filter({ id: { $oneof: ids } })
.select('*'),
);
const [fromTrans, toTrans] = transactions;

if (transactions.length === 2 && validForTransfer(fromTrans, toTrans)) {
const fromPayee = payees.find(
p => p.transfer_acct === fromTrans.account,
);
const toPayee = payees.find(p => p.transfer_acct === toTrans.account);

const changes = {
updated: [
{
...fromTrans,
payee: toPayee?.id,
transfer_id: toTrans.id,
},
{
...toTrans,
payee: fromPayee?.id,
transfer_id: fromTrans.id,
},
],
};

await send('transactions-batch-update', changes);
}


};

await checkForReconciledTransactions(
ids,
'batchEditWithReconciled',
onConfirmTransfer,
);
};

const onBatchDelete = async ({ ids, onSuccess }: BatchDeleteProps) => {
const onConfirmDelete = (ids: Array<TransactionEntity['id']>) => {
dispatch(
Expand Down Expand Up @@ -414,5 +461,6 @@ export function useTransactionBatchActions() {
onBatchDelete,
onBatchLinkSchedule,
onBatchUnlinkSchedule,
onSetTransfer,
};
}

0 comments on commit bc29314

Please sign in to comment.