Browse Source

Ability to remove an account with no transactions

master
meriadec 7 years ago
parent
commit
fa456af0b7
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 6
      src/actions/accounts.js
  2. 25
      src/components/modals/SettingsAccount.js
  3. 3
      src/reducers/accounts.js
  4. 6
      src/renderer/events.js

6
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()

25
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<Props, State> {
state = {
...defaultState,
@ -102,12 +108,15 @@ class SettingsAccount extends PureComponent<Props, State> {
}
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<Props, State> {
</Box>
<Box horizontal grow align="flex-end" flow={2}>
<Box grow>
<Button onClick={this.handleArchiveAccount(account)}>Archive account</Button>
<Button onClick={this.handleArchiveAccount(account)}>
{hasNoTransactions(account) ? 'Remove account' : 'Archive account'}
</Button>
</Box>
<Box grow>
<Button primary>Go to account</Button>

3
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

6
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))
}
}

Loading…
Cancel
Save