A local-first personal finance app
Add ability to import categories from CSV (#2163)
Neilcaca2497ea4e
diff --git a/packages/desktop-client/src/components/accounts/Account.jsx b/packages/desktop-client/src/components/accounts/Account.jsxindex a91ad99ee5..e61c46c68a 100644--- a/packages/desktop-client/src/components/accounts/Account.jsx+++ b/packages/desktop-client/src/components/accounts/Account.jsx@@ -463,6 +463,7 @@ class AccountInternal extends PureComponent {onImport = async () => {const accountId = this.props.accountId;const account = this.props.accounts.find(acct => acct.id === accountId);+ const categories = await this.props.getCategories();if (account) {const res = await window.Actual.openFileDialog({@@ -477,6 +478,7 @@ class AccountInternal extends PureComponent {if (res) {this.props.pushModal('import-transactions', {accountId,+ categories,filename: res[0],onImported: didChange => {if (didChange) {diff --git a/packages/desktop-client/src/components/modals/ImportTransactions.jsx b/packages/desktop-client/src/components/modals/ImportTransactions.jsxindex 8c55e3b322..79f23a37a8 100644--- a/packages/desktop-client/src/components/modals/ImportTransactions.jsx+++ b/packages/desktop-client/src/components/modals/ImportTransactions.jsx@@ -194,14 +194,24 @@ function getInitialMappings(transactions) {fields.find(([name, value]) => value.match(/^-?[.,\d]+$/)),);+ const categoryField = key(+ fields.find(([name, value]) => name.toLowerCase().includes('category')),+ );+const payeeField = key(- fields.find(([name, value]) => name !== dateField && name !== amountField),+ fields.find(+ ([name, value]) =>+ name !== dateField && name !== amountField && name !== categoryField,+ ),);const notesField = key(fields.find(([name, value]) =>- name !== dateField && name !== amountField && name !== payeeField,+ name !== dateField &&+ name !== amountField &&+ name !== categoryField &&+ name !== payeeField,),);@@ -221,6 +231,7 @@ function getInitialMappings(transactions) {payee: payeeField,notes: notesField,inOut: inOutField,+ category: categoryField,};}@@ -290,6 +301,19 @@ function parseAmountFields(};}+function parseCategoryFields(trans, categories) {+ let match = null;+ categories.forEach(category => {+ if (category.id === trans.category) {+ return null;+ }+ if (category.name === trans.category) {+ match = category.id;+ }+ });+ return match;+}+function Transaction({transaction: rawTransaction,fieldMappings,@@ -301,7 +325,9 @@ function Transaction({outValue,flipAmount,multiplierAmount,+ categories,}) {+ const categoryList = categories.map(category => category.name);const transaction = useMemo(() =>fieldMappings@@ -348,6 +374,14 @@ function Transaction({<Field width="flex" title={transaction.notes}>{transaction.notes}</Field>+ <Field+ width="flex"+ title={+ categoryList.includes(transaction.category) && transaction.category+ }+ >+ {categoryList.includes(transaction.category) && transaction.category}+ </Field>{splitMode ? (<><Field@@ -602,6 +636,17 @@ function FieldMappings({firstTransaction={transactions[0]}/></View>+ <View style={{ flex: 1 }}>+ <SubLabel title="Category" />+ <SelectField+ options={options}+ value={mappings.category}+ style={{ marginRight: 5 }}+ onChange={name => onChange('category', name)}+ hasHeaderRow={hasHeaderRow}+ firstTransaction={transactions[0]}+ />+ </View>{splitMode ? (<><View style={{ flex: 0.5 }}>@@ -676,7 +721,7 @@ export function ImportTransactions({ modalProps, options }) {const [outValue, setOutValue] = useState('');const [flipAmount, setFlipAmount] = useState(false);const [multiplierEnabled, setMultiplierEnabled] = useState(false);- const { accountId, onImported } = options;+ const { accountId, categories, onImported } = options;// This cannot be set after parsing the file, because changing it// requires re-parsing the file. This is different from the other@@ -866,6 +911,11 @@ export function ImportTransactions({ modalProps, options }) {break;}+ const category_id = parseCategoryFields(trans, categories.list);+ if (category_id != null) {+ trans.category = category_id;+ }+const { inflow, outflow, inOut, ...finalTransaction } = trans;finalTransactions.push({...finalTransaction,@@ -919,6 +969,7 @@ export function ImportTransactions({ modalProps, options }) {{ name: 'Date', width: 200 },{ name: 'Payee', width: 'flex' },{ name: 'Notes', width: 'flex' },+ { name: 'Category', width: 'flex' },];if (inOutMode) {@@ -959,7 +1010,7 @@ export function ImportTransactions({ modalProps, options }) {<TableWithNavigatoritems={transactions}- fields={['payee', 'amount']}+ fields={['payee', 'category', 'amount']}style={{ backgroundColor: theme.tableHeaderBackground }}getItemKey={index => index}renderEmpty={() => {@@ -989,6 +1040,7 @@ export function ImportTransactions({ modalProps, options }) {outValue={outValue}flipAmount={flipAmount}multiplierAmount={multiplierAmount}+ categories={categories.list}/></View>)}diff --git a/upcoming-release-notes/2163.md b/upcoming-release-notes/2163.mdnew file mode 100644index 0000000000..2d7313306b--- /dev/null+++ b/upcoming-release-notes/2163.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [ScottFries, blakegearin, carkom]+---++Add ability to import categories from CSV\ No newline at end of file