add extended token selector

This commit is contained in:
Mikhail Mikheev 2019-04-10 20:04:29 +04:00
parent 93cf045c00
commit a158fe4a15
4 changed files with 53 additions and 7 deletions

View File

@ -8,6 +8,7 @@ export type TokenProps = {
symbol: string,
decimals: number,
logoUri: string,
balance: string
}
export const makeToken: RecordFactory<TokenProps> = Record({
@ -16,6 +17,9 @@ export const makeToken: RecordFactory<TokenProps> = Record({
symbol: '',
decimals: 0,
logoUri: '',
balance: undefined,
})
// balance is only set in extendedSafeTokensSelector when we display user's token balances
export type Token = RecordOf<TokenProps>

View File

@ -1,13 +1,18 @@
// @flow
import { List } from 'immutable'
import { List, Map } from 'immutable'
import { createSelector, createStructuredSelector, type Selector } from 'reselect'
import { safeSelector, type RouterProps, type SafeSelectorProps } from '~/routes/safe/store/selectors'
import {
safeSelector,
safeTokensSelector,
type RouterProps,
type SafeSelectorProps,
} from '~/routes/safe/store/selectors'
import { providerNameSelector, userAccountSelector, networkSelector } from '~/logic/wallets/store/selectors'
import { type Safe } from '~/routes/safe/store/model/safe'
import { type Owner } from '~/routes/safe/store/model/owner'
import { type GlobalState } from '~/store'
import { sameAddress } from '~/logic/wallets/ethAddresses'
import { activeTokensSelector, orderedTokenListSelector } from '~/logic/tokens/store/selectors'
import { activeTokensSelector, orderedTokenListSelector, tokensSelector } from '~/logic/tokens/store/selectors'
import { type Token } from '~/logic/tokens/store/model/token'
import { safeParamAddressSelector } from '../store/selectors'
@ -42,10 +47,25 @@ export const grantedSelector: Selector<GlobalState, RouterProps, boolean> = crea
},
)
const extendedSafeTokensSelector: Selector<GlobalState, RouterProps, List<Token>> = createSelector(
safeTokensSelector,
tokensSelector,
(safeTokens: Map<string, string>, tokensList: Map<string, Token>) => {
// const extendedTokens = safeTokens.map(token => tokensList.get(token.address).set('balance', token.balance))
const extendedTokens = Map().withMutations((map) => {
safeTokens.forEach(({ address, balance }: { address: string, balance: string }) => {
map.set(address, tokensList.get(address).set(balance))
})
})
return extendedTokens
},
)
export default createStructuredSelector<Object, *>({
safe: safeSelector,
provider: providerNameSelector,
tokens: orderedTokenListSelector,
tokens: extendedSafeTokensSelector,
activeTokens: activeTokensSelector,
granted: grantedSelector,
userAddress: userAccountSelector,

View File

@ -2,13 +2,14 @@
import { List, Record, Map } from 'immutable'
import type { RecordFactory, RecordOf } from 'immutable'
import type { Owner } from '~/routes/safe/store/model/owner'
import { ETH_ADDRESS } from '~/logic/tokens/utils/tokenHelpers'
export type SafeProps = {
name: string,
address: string,
threshold: number,
owners: List<Owner>,
activeTokensBalances: Map<String, String>,
tokens: List<Map<string, string>>,
}
const SafeRecord: RecordFactory<SafeProps> = Record({
@ -16,9 +17,16 @@ const SafeRecord: RecordFactory<SafeProps> = Record({
address: '',
threshold: 0,
owners: List([]),
activeTokensBalances: Map([]),
tokens: List([
{
address: ETH_ADDRESS,
balance: '0',
},
]),
})
// Tokens is a list of currently enabled tokens for the safe with balances
export type Safe = RecordOf<SafeProps>
export default SafeRecord

View File

@ -78,6 +78,20 @@ export const safeSelector: Selector<GlobalState, RouterProps, SafeSelectorProps>
},
)
export default createStructuredSelector({
export const safeTokensSelector: Selector<GlobalState, RouterProps, Map<string, string>> = createSelector(
safeSelector,
(safe: Safe) => {
if (!safe) {
return Map()
}
const tokens = safe.get('tokens')
return tokens
},
)
export default createStructuredSelector<Object, *>({
safe: safeSelector,
tokens: safeTokensSelector,
})