From 8b830be2835186ce8a348c6eb469ba614852f31e Mon Sep 17 00:00:00 2001 From: Carlo Date: Mon, 8 Nov 2021 15:07:00 +0100 Subject: [PATCH] feat: amounts and commodities auto suggestion --- frontend/src/pages/dashboard/entry-row.tsx | 17 ++++++-- frontend/src/pages/dashboard/index.tsx | 46 +++++++++++++++++----- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/frontend/src/pages/dashboard/entry-row.tsx b/frontend/src/pages/dashboard/entry-row.tsx index 9d23aa6..f6ee852 100644 --- a/frontend/src/pages/dashboard/entry-row.tsx +++ b/frontend/src/pages/dashboard/entry-row.tsx @@ -21,12 +21,23 @@ const useStyles = createStyles((theme) => { const EntryRow: FC<{ accounts: string[]; + amountPlaceholder: string | null; canDelete: boolean; commodities: string[]; entry: TransactionEntry; + suggestedCommodity: string | undefined; removeRow: () => void; updateRow: (field: string, value: string) => void; -}> = ({ accounts, canDelete, commodities, entry, removeRow, updateRow }) => { +}> = ({ + accounts, + amountPlaceholder, + canDelete, + commodities, + entry, + suggestedCommodity, + removeRow, + updateRow, +}) => { const { classes } = useStyles(); return ( @@ -41,13 +52,13 @@ const EntryRow: FC<{ onChange={(value) => updateRow("account", value)} /> updateRow("amount", event.currentTarget.value)} /> updateRow("commodity", value)} diff --git a/frontend/src/pages/dashboard/index.tsx b/frontend/src/pages/dashboard/index.tsx index 2846ae1..0ee6e2d 100644 --- a/frontend/src/pages/dashboard/index.tsx +++ b/frontend/src/pages/dashboard/index.tsx @@ -7,24 +7,34 @@ import { DatePicker } from '@mantine/dates'; import { useForm } from '@mantine/hooks'; import { useNotifications } from '@mantine/notifications'; -import { Transaction } from 'pta-journal'; +import { Transaction, TransactionEntry } from 'pta-journal'; import { Api } from 'types'; import ConfirmationModal from './confirmation-modal'; import EntryRow from './entry-row'; -const EMPTY_ENTRY = { account: "", amount: "", commodity: "" }; +const EMPTY_ENTRY: TransactionEntry = { + account: "", + amount: "", +}; + +function allValuesAreEqual(values: T[]): T | undefined { + if (values.every((value) => value == null || value === values[0])) { + return values[0]; + } +} const Dashboard: FC<{ journal: Api.BootstrapResponse }> = ({ journal }) => { const notifications = useNotifications(); - const { onSubmit, values, setFieldValue, setValues, reset } = useForm({ - initialValues: { - date: new Date(), - description: "", - entries: [EMPTY_ENTRY, EMPTY_ENTRY], - }, - }); + const { onSubmit, values, setFieldValue, setValues, reset } = + useForm({ + initialValues: { + date: new Date(), + description: "", + entries: [EMPTY_ENTRY, EMPTY_ENTRY], + }, + }); const [modalOpen, setModalOpen] = useState(false); const [showOverlay, setOverlay] = useState(false); @@ -88,6 +98,22 @@ const Dashboard: FC<{ journal: Api.BootstrapResponse }> = ({ journal }) => { }); } + const outBalance = values.entries.reduce( + (acc, e) => acc + Number(e.amount), + 0 + ); + + const amountPlaceholder = + !isNaN(outBalance) && outBalance !== 0 + ? (outBalance * -1).toString() + : null; + + const singleCommodity = allValuesAreEqual( + values.entries.map((e) => e.commodity) + ); + + console.log("suggestedCommodity", singleCommodity); + return (
@@ -121,6 +147,8 @@ const Dashboard: FC<{ journal: Api.BootstrapResponse }> = ({ journal }) => { key={i} removeRow={removeRow(i)} updateRow={updateRow(i)} + amountPlaceholder={amountPlaceholder} + suggestedCommodity={singleCommodity} /> ))}