From fa456af0b77de2e7c6a2190b0d85ece1cd4f0157 Mon Sep 17 00:00:00 2001 From: meriadec Date: Fri, 9 Feb 2018 09:17:33 +0100 Subject: [PATCH] Ability to remove an account with no transactions --- src/actions/accounts.js | 6 ++++++ src/components/modals/SettingsAccount.js | 25 +++++++++++++++++------- src/reducers/accounts.js | 3 +++ src/renderer/events.js | 6 ++++-- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/actions/accounts.js b/src/actions/accounts.js index 23dc8535..17db0b2b 100644 --- a/src/actions/accounts.js +++ b/src/actions/accounts.js @@ -34,6 +34,12 @@ export const updateAccount: AddAccount = payload => ({ payload, }) +export type RemoveAccount = Account => { type: string, payload: Account } +export const removeAccount: RemoveAccount = payload => ({ + type: 'DB:REMOVE_ACCOUNT', + payload, +}) + export type FetchAccounts = () => (Dispatch<*>, Function) => void export const fetchAccounts: FetchAccounts = () => (dispatch, getState) => { const { settings: { orderAccounts } } = getState() diff --git a/src/components/modals/SettingsAccount.js b/src/components/modals/SettingsAccount.js index 962f17ad..dee06452 100644 --- a/src/components/modals/SettingsAccount.js +++ b/src/components/modals/SettingsAccount.js @@ -9,7 +9,7 @@ import { MODAL_SETTINGS_ACCOUNT } from 'constants' import type { Account } from 'types/common' -import { updateAccount } from 'actions/accounts' +import { updateAccount, removeAccount } from 'actions/accounts' import { setDataModal, closeModal } from 'reducers/modals' import Box from 'components/base/Box' @@ -28,6 +28,7 @@ type State = { type Props = { closeModal: Function, updateAccount: Function, + removeAccount: Function, setDataModal: Function, push: Function, } @@ -35,6 +36,7 @@ type Props = { const mapDispatchToProps = { closeModal, updateAccount, + removeAccount, setDataModal, push, } @@ -45,6 +47,10 @@ const defaultState = { nameHovered: false, } +function hasNoTransactions(account: Account) { + return get(account, 'data.transactions.length', 0) === 0 +} + class SettingsAccount extends PureComponent { state = { ...defaultState, @@ -102,12 +108,15 @@ class SettingsAccount extends PureComponent { } handleArchiveAccount = (account: Account) => () => { - const { push, closeModal, updateAccount } = this.props + const { push, closeModal, updateAccount, removeAccount } = this.props + const shouldRemove = hasNoTransactions(account) + + if (shouldRemove) { + removeAccount(account) + } else { + updateAccount({ ...account, archived: true }) + } - updateAccount({ - ...account, - archived: true, - }) closeModal(MODAL_SETTINGS_ACCOUNT) push('/') @@ -170,7 +179,9 @@ class SettingsAccount extends PureComponent { - + diff --git a/src/reducers/accounts.js b/src/reducers/accounts.js index 9ef973b7..17465991 100644 --- a/src/reducers/accounts.js +++ b/src/reducers/accounts.js @@ -91,6 +91,9 @@ const handlers: Object = { return orderAccountsTransactions(updatedAccount) }), + + REMOVE_ACCOUNT: (state: AccountsState, { payload: account }: { payload: Account }) => + state.filter(acc => acc.id !== account.id), } // Selectors diff --git a/src/renderer/events.js b/src/renderer/events.js index a1ff17a1..807f75c6 100644 --- a/src/renderer/events.js +++ b/src/renderer/events.js @@ -69,12 +69,14 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => { success: account => { if (syncAccounts) { const currentAccountData = getAccountData(store.getState(), account.id) || {} + const currentAccountTransactions = get(currentAccountData, 'transactions', []) + const transactions = uniqBy( - [...currentAccountData.transactions, ...account.transactions], + [...currentAccountTransactions, ...account.transactions], tx => tx.hash, ) - if (currentAccountData.transactions.length !== transactions.length) { + if (currentAccountTransactions.length !== transactions.length) { store.dispatch(updateAccount(account)) } }