From fa92c374c97c8020fbf2b5dd83a7f4814b2ab088 Mon Sep 17 00:00:00 2001 From: dtrai2 Date: Fri, 1 Nov 2024 18:09:56 +0100 Subject: [PATCH 1/2] add auto-completion for narration --- frontend/src/api/index.ts | 1 + frontend/src/api/validators.ts | 2 + frontend/src/entry-forms/Transaction.svelte | 48 +++++++++++-------- frontend/src/sidebar/FilterForm.svelte | 36 ++++++++------ frontend/src/stores/index.ts | 2 + src/fava/core/attributes.py | 13 +++++ src/fava/internal_api.py | 2 + src/fava/json_api.py | 7 +++ .../test_application-test_client_side_reports | 39 ++++++++++++++- ...est_internal_api-test_get_ledger_data.json | 39 ++++++++++++++- tests/test_core_attributes.py | 9 ++++ 11 files changed, 161 insertions(+), 37 deletions(-) diff --git a/frontend/src/api/index.ts b/frontend/src/api/index.ts index ff81cf653..0095b79c4 100644 --- a/frontend/src/api/index.ts +++ b/frontend/src/api/index.ts @@ -71,6 +71,7 @@ interface GetAPIParams { move: { filename: string; account: string; new_name: string }; payee_accounts: { payee: string }; payee_transaction: { payee: string }; + narration_transaction: { narration: string }; query: Filters & { query_string: string }; source: { filename: string }; } diff --git a/frontend/src/api/validators.ts b/frontend/src/api/validators.ts index ba74ab348..47de2a046 100644 --- a/frontend/src/api/validators.ts +++ b/frontend/src/api/validators.ts @@ -106,6 +106,7 @@ export const ledgerDataValidator = object({ options, other_ledgers: array(tuple(string, string)), payees: array(string), + narrations: array(string), precisions: record(number), sidebar_links: array(tuple(string, string)), tags: array(string), @@ -193,6 +194,7 @@ export const getAPIValidators = { move: string, payee_accounts: array(string), payee_transaction: Transaction.validator, + narration_transaction: Transaction.validator, query: query_validator, source, trial_balance: tree_report, diff --git a/frontend/src/entry-forms/Transaction.svelte b/frontend/src/entry-forms/Transaction.svelte index 16e37381e..14a883fae 100644 --- a/frontend/src/entry-forms/Transaction.svelte +++ b/frontend/src/entry-forms/Transaction.svelte @@ -10,7 +10,8 @@ import { Posting } from "../entries"; import { _ } from "../i18n"; import { notify_err } from "../notifications"; - import { payees } from "../stores"; + import { valueExtractor, valueSelector } from "../sidebar/FilterForm.svelte"; + import { narrations, payees } from "../stores"; import AddMetadataButton from "./AddMetadataButton.svelte"; import EntryMetadata from "./EntryMetadata.svelte"; import PostingSvelte from "./Posting.svelte"; @@ -40,13 +41,9 @@ } } - /// Extract tags and links that can be provided in the narration . - function onNarrationChange({ - currentTarget, - }: { - currentTarget: HTMLInputElement; - }) { - const { value } = currentTarget; + /// Extract tags and links that can be provided in the narration. + function onNarrationBlur() { + const value = narration; entry.tags = [...value.matchAll(TAGS_RE)].map((a) => a[1] ?? ""); entry.links = [...value.matchAll(LINKS_RE)].map((a) => a[1] ?? ""); entry.narration = value @@ -54,8 +51,7 @@ .replaceAll(LINKS_RE, "") .trim(); } - - /// Output tags and links in the narration + /// Output tags and links in the narration function combineNarrationTagsLinks(e: Transaction): string { let val = e.narration; if (e.tags.length) { @@ -66,7 +62,7 @@ } return val; } - $: narration = combineNarrationTagsLinks(entry); + let narration = ""; // Autofill complete transactions. async function autocompleteSelectPayee() { @@ -77,6 +73,17 @@ data.date = entry.date; entry = data; } + async function autocompleteSelectNarration() { + if (entry.payee || !entry.postings.every((p) => !p.account)) { + return; + } + const data = await get("narration_transaction", { + narration: narration, + }); + data.date = entry.date; + entry = data; + narration = combineNarrationTagsLinks(entry); + } function movePosting({ from, to }: { from: number; to: number }) { const moved = entry.postings[from]; @@ -108,14 +115,18 @@ on:select={autocompleteSelectPayee} /> + @@ -151,11 +162,6 @@ flex-basis: 100px; } - input[name="narration"] { - flex-grow: 1; - flex-basis: 200px; - } - label > span:first-child, .label > span:first-child { display: none; diff --git a/frontend/src/sidebar/FilterForm.svelte b/frontend/src/sidebar/FilterForm.svelte index c3c901459..6dba30ff2 100644 --- a/frontend/src/sidebar/FilterForm.svelte +++ b/frontend/src/sidebar/FilterForm.svelte @@ -1,22 +1,17 @@ - + + - \ No newline at end of file + diff --git a/tests/__snapshots__/test_internal_api-test_get_ledger_data.json b/tests/__snapshots__/test_internal_api-test_get_ledger_data.json index 39dc71d69..19c43478d 100644 --- a/tests/__snapshots__/test_internal_api-test_get_ledger_data.json +++ b/tests/__snapshots__/test_internal_api-test_get_ledger_data.json @@ -929,6 +929,43 @@ "links": [ "test-link" ], + "narrations": [ + "Investing 40% of cash in VBMPX", + "Investing 60% of cash in RGAGX", + "Payroll", + "Buying groceries", + "Eating out alone", + "Employer match for contribution", + "Eating out with Julie", + "Eating out ", + "Eating out after work", + "Eating out with Bill", + "Eating out with Natasha", + "Monthly bank fee", + "Paying off credit card", + "Eating out with Joe", + "Paying the rent", + "Tram tickets", + "Eating out with work buddies", + "Buy shares of VEA", + "Buy shares of GLD", + "Dividends on portfolio", + "Transfering accumulated savings to other account", + "Buy shares of ITOT", + "Buy shares of VHT", + "Consume vacation days", + "Sell shares of GLD", + "STATE TAX & FINANC PYMT", + "FEDERAL TAXPYMT", + "Allowed contributions for one year", + "Sell shares of VEA", + "Filing taxes for 2015", + "Sell shares of VHT", + "Sell shares of ITOT", + "Filing taxes for 2014", + "Opening Balance for checking account", + "\u00c1rv\u00edzt\u0171r\u0151 t\u00fck\u00f6rf\u00far\u00f3g\u00e9p" + ], "options": { "documents": [], "filename": "TEST_DATA_DIR/long-example.beancount", @@ -1060,4 +1097,4 @@ "1900", "1792" ] -} \ No newline at end of file +} diff --git a/tests/test_core_attributes.py b/tests/test_core_attributes.py index 46a905263..03c2c17b3 100644 --- a/tests/test_core_attributes.py +++ b/tests/test_core_attributes.py @@ -63,3 +63,12 @@ def test_payee_transaction(example_ledger: FavaLedger) -> None: txn = attr.payee_transaction("BayBook") assert txn assert str(txn.date) == "2016-05-05" + + +def test_narration_transaction(example_ledger: FavaLedger) -> None: + attr = example_ledger.attributes + assert attr.narration_transaction("NOTANARRATION") is None + + txn = attr.narration_transaction("Monthly bank fee") + assert txn + assert str(txn.date) == "2016-05-04" From 88f71c0b9c9d2a2db4f5292676651657de7e8999 Mon Sep 17 00:00:00 2001 From: dtrai2 Date: Fri, 1 Nov 2024 18:36:18 +0100 Subject: [PATCH 2/2] remove newline at end of file to fix test --- tests/__snapshots__/test_application-test_client_side_reports | 2 +- tests/__snapshots__/test_internal_api-test_get_ledger_data.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/__snapshots__/test_application-test_client_side_reports b/tests/__snapshots__/test_application-test_client_side_reports index e7bef8522..d0e4e3f49 100644 --- a/tests/__snapshots__/test_application-test_client_side_reports +++ b/tests/__snapshots__/test_application-test_client_side_reports @@ -1113,4 +1113,4 @@ - + \ No newline at end of file diff --git a/tests/__snapshots__/test_internal_api-test_get_ledger_data.json b/tests/__snapshots__/test_internal_api-test_get_ledger_data.json index 19c43478d..fcff7c69a 100644 --- a/tests/__snapshots__/test_internal_api-test_get_ledger_data.json +++ b/tests/__snapshots__/test_internal_api-test_get_ledger_data.json @@ -1097,4 +1097,4 @@ "1900", "1792" ] -} +} \ No newline at end of file