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] Auto-fill empty split transaction with outstanding amount on click. #1271

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
78777b5
add ability to balance
Shazib Jul 3, 2023
77c8860
Merge branch 'master' into split-transactions-autofill
Shazib Jul 4, 2023
4a90621
Release notes
Shazib Jul 4, 2023
d57fe5c
Merge branch 'master' into split-transactions-autofill
Shazib Jul 4, 2023
ad78d50
Merge branch 'actualbudget:master' into split-transactions-autofill
Shazib Jul 5, 2023
e1542ec
Fix
Shazib Jul 6, 2023
30b8e26
Merge branch 'master' into split-transactions-autofill
Shazib Jul 9, 2023
2d02e02
Merge branch 'master' into split-transactions-autofill
Shazib Jul 9, 2023
a64bc1a
Merge branch 'master' into split-transactions-autofill
Shazib Jul 14, 2023
448ce91
Merge branch 'master' into split-transactions-autofill
Shazib Jul 18, 2023
c376b20
Fix how test locates the “Add Split” button
j-f1 Jul 19, 2023
0422603
Fix remaining failed check
j-f1 Jul 19, 2023
517fd0e
Merge branch 'master' into split-transactions-autofill
Shazib Jul 19, 2023
fd55daa
Merge branch 'master' into split-transactions-autofill
Shazib Jul 29, 2023
a3fb0f2
Merge branch 'master' into split-transactions-autofill
Shazib Oct 5, 2023
d05d9cb
Merge branch 'master' into split-transactions-autofill
Shazib Oct 25, 2023
38f28af
Merge branch 'master' into split-transactions-autofill
Shazib Oct 28, 2023
84f676c
Better implementation
Shazib Oct 28, 2023
16455a7
Merge branch 'master' into split-transactions-autofill
Shazib Oct 28, 2023
4313cd4
Merge branch 'master' into split-transactions-autofill
Shazib Nov 9, 2023
64141ea
Merge branch 'master' into split-transactions-autofill
Shazib Nov 10, 2023
5e8df33
Merge branch 'master' into split-transactions-autofill
Shazib Nov 11, 2023
0c8c625
Fix for existing splits
Nov 16, 2023
d2f6734
Existing split support
Nov 16, 2023
98785c5
Try passing error null to trigger end edit
Nov 16, 2023
410aa15
Merge branch 'master' into split-transactions-autofill
Shazib Nov 17, 2023
fd0028a
lint
Nov 17, 2023
7206b3a
Merge branch 'master' into split-transactions-autofill
Shazib Nov 26, 2023
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 @@ -1345,7 +1345,13 @@ const Transaction = memo(function Transaction(props) {
);
});

function TransactionError({ error, isDeposit, onAddSplit, style }) {
function TransactionError({
error,
isDeposit,
onAddSplit,
style,
onBalanceLastSplit,
}) {
switch (error.type) {
case 'SplitTransactionError':
if (error.version === 1) {
Expand All @@ -1361,17 +1367,25 @@ function TransactionError({ error, isDeposit, onAddSplit, style }) {
>
<Text>
Amount left:{' '}
<Text style={{ fontWeight: 500 }}>
<Button
bare
onClick={onBalanceLastSplit}
style={{
display: 'inline',
fontWeight: 500,
}}
>
{integerToCurrency(
isDeposit ? error.difference : -error.difference,
)}
</Text>
</Button>
</Text>
<View style={{ flex: 1 }} />
<Button
type="primary"
style={{ marginLeft: 15, padding: '4px 10px' }}
onClick={onAddSplit}
data-testid="add-split-button"
>
Add Split
</Button>
Expand Down Expand Up @@ -1433,6 +1447,7 @@ function NewTransaction({
onCreatePayee,
onNavigateToTransferAccount,
onNavigateToSchedule,
onBalanceLastSplit,
balance,
}) {
const error = transactions[0].error;
Expand Down Expand Up @@ -1503,6 +1518,7 @@ function NewTransaction({
<TransactionError
error={error}
isDeposit={isDeposit}
onBalanceLastSplit={onBalanceLastSplit}
onAddSplit={() => onAddSplit(transactions[0].id)}
/>
) : (
Expand All @@ -1527,6 +1543,7 @@ function TransactionTableInner({
newNavigator,
renderEmpty,
onScroll,
onBalanceLastSplit,
...props
}) {
const containerRef = createRef();
Expand Down Expand Up @@ -1609,6 +1626,7 @@ function TransactionTableInner({
error={error}
isDeposit={isChildDeposit}
onAddSplit={() => props.onAddSplit(trans.id)}
onBalanceLastSplit={onBalanceLastSplit}
/>
</Tooltip>
)}
Expand Down Expand Up @@ -1701,6 +1719,7 @@ function TransactionTableInner({
onManagePayees={props.onManagePayees}
onCreatePayee={props.onCreatePayee}
onNavigateToTransferAccount={onNavigateToTransferAccount}
onBalanceLastSplit={onBalanceLastSplit}
onNavigateToSchedule={onNavigateToSchedule}
balance={
props.transactions?.length > 0
Expand Down Expand Up @@ -2091,6 +2110,56 @@ export let TransactionTable = forwardRef((props, ref) => {
[splitsExpanded.dispatch],
);

let onBalanceLastSplit = useCallback(async () => {
let { tableNavigator, newTransactions } = latestState.current;

if (newTransactions === null) {
let editingTransaction = latestState.current.transactions.filter(
t => t.id === tableNavigator.editingId,
);
newTransactions = latestState.current.transactions.filter(
t => t.parent_id === editingTransaction.parent_id,
);
}

const parentTransaction = newTransactions[0];
const allSubTransactions = newTransactions.filter(
t => t.parent_id === parentTransaction.id,
);

const emptyTransactions = allSubTransactions.filter(t => t.amount === 0);
const remainingAmount =
parentTransaction.amount -
allSubTransactions.reduce((acc, t) => acc + t.amount, 0);

const amountPerTransaction = Math.floor(
remainingAmount / emptyTransactions.length,
);
let remainingCents =
remainingAmount - amountPerTransaction * emptyTransactions.length;

let amounts = new Array(emptyTransactions.length).fill(
amountPerTransaction,
);
for (let amountIndex in amounts) {
if (remainingCents === 0) break;

amounts[amountIndex] += 1;
remainingCents--;
}

for (const transactionIndex in emptyTransactions) {
await onSave({
...emptyTransactions[transactionIndex],
amount: amounts[transactionIndex],
error: null,
});
}

// cancel focused field
// tableNavigator.onEdit(null, null);
}, [latestState]);

return (
<TransactionTableInner
tableRef={mergedRef}
Expand All @@ -2111,6 +2180,7 @@ export let TransactionTable = forwardRef((props, ref) => {
newTransactions={newTransactions}
tableNavigator={tableNavigator}
newNavigator={newNavigator}
onBalanceLastSplit={onBalanceLastSplit}
/>
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,9 @@ describe('Transactions', () => {
await waitForAutocomplete();

await userEvent.click(
container.querySelector('[data-testid="transaction-error"] button'),
container.querySelector(
'[data-testid="transaction-error"] [data-testid="add-split-button"]',
),
);

input = await editNewField(container, 'debit', 1);
Expand Down Expand Up @@ -825,7 +827,9 @@ describe('Transactions', () => {

// Add another split transaction and make sure everything is
// updated properly
await userEvent.click(toolbar.querySelector('button'));
await userEvent.click(
toolbar.querySelector('[data-testid="add-split-button"]'),
);
expect(getTransactions().length).toBe(7);
expect(getTransactions()[2].amount).toBe(0);
expectErrorToExist(getTransactions().slice(0, 3));
Expand Down Expand Up @@ -919,7 +923,9 @@ describe('Transactions', () => {
await userEvent.click(splitButton);
await waitForAutocomplete();
await userEvent.click(
container.querySelector('[data-testid="transaction-error"] button'),
container.querySelector(
'[data-testid="transaction-error"] [data-testid="add-split-button"]',
),
);
expect(getTransactions().length).toBe(7);

Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1271.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [Shazib,NikxDa]
---

Allow clicking on 'Amount Left' value to auto-fill the last empty split(s).
Loading