Browse Source

Merge pull request #95 from meriadec/master

Ability to remove an account with no transactions
master
Loëck Vézien 7 years ago
committed by GitHub
parent
commit
c0864faff7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  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, 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 type FetchAccounts = () => (Dispatch<*>, Function) => void
export const fetchAccounts: FetchAccounts = () => (dispatch, getState) => { export const fetchAccounts: FetchAccounts = () => (dispatch, getState) => {
const { settings: { orderAccounts } } = 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 type { Account } from 'types/common'
import { updateAccount } from 'actions/accounts' import { updateAccount, removeAccount } from 'actions/accounts'
import { setDataModal, closeModal } from 'reducers/modals' import { setDataModal, closeModal } from 'reducers/modals'
import Box from 'components/base/Box' import Box from 'components/base/Box'
@ -28,6 +28,7 @@ type State = {
type Props = { type Props = {
closeModal: Function, closeModal: Function,
updateAccount: Function, updateAccount: Function,
removeAccount: Function,
setDataModal: Function, setDataModal: Function,
push: Function, push: Function,
} }
@ -35,6 +36,7 @@ type Props = {
const mapDispatchToProps = { const mapDispatchToProps = {
closeModal, closeModal,
updateAccount, updateAccount,
removeAccount,
setDataModal, setDataModal,
push, push,
} }
@ -45,6 +47,10 @@ const defaultState = {
nameHovered: false, nameHovered: false,
} }
function hasNoTransactions(account: Account) {
return get(account, 'data.transactions.length', 0) === 0
}
class SettingsAccount extends PureComponent<Props, State> { class SettingsAccount extends PureComponent<Props, State> {
state = { state = {
...defaultState, ...defaultState,
@ -102,12 +108,15 @@ class SettingsAccount extends PureComponent<Props, State> {
} }
handleArchiveAccount = (account: Account) => () => { 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) closeModal(MODAL_SETTINGS_ACCOUNT)
push('/') push('/')
@ -170,7 +179,9 @@ class SettingsAccount extends PureComponent<Props, State> {
</Box> </Box>
<Box horizontal grow align="flex-end" flow={2}> <Box horizontal grow align="flex-end" flow={2}>
<Box grow> <Box grow>
<Button onClick={this.handleArchiveAccount(account)}>Archive account</Button> <Button onClick={this.handleArchiveAccount(account)}>
{hasNoTransactions(account) ? 'Remove account' : 'Archive account'}
</Button>
</Box> </Box>
<Box grow> <Box grow>
<Button primary>Go to account</Button> <Button primary>Go to account</Button>

3
src/reducers/accounts.js

@ -91,6 +91,9 @@ const handlers: Object = {
return orderAccountsTransactions(updatedAccount) return orderAccountsTransactions(updatedAccount)
}), }),
REMOVE_ACCOUNT: (state: AccountsState, { payload: account }: { payload: Account }) =>
state.filter(acc => acc.id !== account.id),
} }
// Selectors // Selectors

6
src/renderer/events.js

@ -69,12 +69,14 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => {
success: account => { success: account => {
if (syncAccounts) { if (syncAccounts) {
const currentAccountData = getAccountData(store.getState(), account.id) || {} const currentAccountData = getAccountData(store.getState(), account.id) || {}
const currentAccountTransactions = get(currentAccountData, 'transactions', [])
const transactions = uniqBy( const transactions = uniqBy(
[...currentAccountData.transactions, ...account.transactions], [...currentAccountTransactions, ...account.transactions],
tx => tx.hash, tx => tx.hash,
) )
if (currentAccountData.transactions.length !== transactions.length) { if (currentAccountTransactions.length !== transactions.length) {
store.dispatch(updateAccount(account)) store.dispatch(updateAccount(account))
} }
} }

Loading…
Cancel
Save