Gaëtan Renaudeau
7 years ago
7 changed files with 133 additions and 80 deletions
@ -0,0 +1,43 @@ |
|||
// @flow
|
|||
|
|||
import { createSelector, createStructuredSelector } from 'reselect' |
|||
import CounterValues from 'helpers/countervalues' |
|||
import { |
|||
intermediaryCurrency, |
|||
currencySettingsForAccountSelector, |
|||
getOrderAccounts, |
|||
} from 'reducers/settings' |
|||
import { accountsSelector } from 'reducers/accounts' |
|||
import { sortAccounts } from 'helpers/accountOrdering' |
|||
|
|||
const accountsBtcBalanceSelector = createSelector( |
|||
accountsSelector, |
|||
state => state, |
|||
(accounts, state) => |
|||
accounts.map(account => { |
|||
const { exchange } = currencySettingsForAccountSelector(state, { account }) |
|||
return CounterValues.calculateSelector(state, { |
|||
from: account.currency, |
|||
to: intermediaryCurrency, |
|||
exchange, |
|||
value: account.balance, |
|||
}) |
|||
}), |
|||
) |
|||
|
|||
const selectAccountsBalanceAndOrder = createStructuredSelector({ |
|||
accounts: accountsSelector, |
|||
accountsBtcBalance: accountsBtcBalanceSelector, |
|||
orderAccounts: getOrderAccounts, |
|||
}) |
|||
|
|||
export const refreshAccountsOrdering = () => (dispatch: *, getState: *) => { |
|||
const all = selectAccountsBalanceAndOrder(getState()) |
|||
const allRatesAvailable = all.accountsBtcBalance.every(b => typeof b === 'number') |
|||
if (allRatesAvailable) { |
|||
dispatch({ |
|||
type: 'DB:REORDER_ACCOUNTS', |
|||
payload: sortAccounts(all), |
|||
}) |
|||
} |
|||
} |
@ -0,0 +1,38 @@ |
|||
// @flow
|
|||
|
|||
import { Component } from 'react' |
|||
import { connect } from 'react-redux' |
|||
import { refreshAccountsOrdering } from 'actions/general' |
|||
|
|||
const mapStateToProps = null |
|||
|
|||
const mapDispatchToProps = { |
|||
refreshAccountsOrdering, |
|||
} |
|||
|
|||
class RefreshAccountsOrdering extends Component<{ |
|||
refreshAccountsOrdering: () => *, |
|||
onMount?: boolean, |
|||
onUnmount?: boolean, |
|||
}> { |
|||
componentDidMount() { |
|||
if (this.props.onMount) { |
|||
this.props.refreshAccountsOrdering() |
|||
} |
|||
} |
|||
|
|||
componentWillUnmount() { |
|||
if (this.props.onUnmount) { |
|||
this.props.refreshAccountsOrdering() |
|||
} |
|||
} |
|||
|
|||
render() { |
|||
return null |
|||
} |
|||
} |
|||
|
|||
export default connect( |
|||
mapStateToProps, |
|||
mapDispatchToProps, |
|||
)(RefreshAccountsOrdering) |
@ -0,0 +1,37 @@ |
|||
// @flow
|
|||
|
|||
import type { Account } from '@ledgerhq/live-common/lib/types' |
|||
|
|||
type Param = { |
|||
accounts: Account[], |
|||
accountsBtcBalance: number[], |
|||
orderAccounts: string, |
|||
} |
|||
|
|||
type SortMethod = 'name' | 'balance' |
|||
|
|||
const sortMethod: { [_: SortMethod]: (Param) => string[] } = { |
|||
balance: ({ accounts, accountsBtcBalance }) => |
|||
accounts |
|||
.map((a, i) => [a.id, accountsBtcBalance[i]]) |
|||
.sort((a, b) => a[1] - b[1]) |
|||
.map(o => o[0]), |
|||
|
|||
name: ({ accounts }) => |
|||
accounts |
|||
.slice(0) |
|||
.sort((a, b) => a.name.localeCompare(b.name)) |
|||
.map(a => a.id), |
|||
} |
|||
|
|||
export function sortAccounts(param: Param) { |
|||
const [order, sort] = param.orderAccounts.split('|') |
|||
if (order === 'name' || order === 'balance') { |
|||
const ids = sortMethod[order](param) |
|||
if (sort === 'asc') { |
|||
ids.reverse() |
|||
} |
|||
return ids |
|||
} |
|||
return null |
|||
} |
Loading…
Reference in new issue