activate token for all safes functionality

This commit is contained in:
Mikhail Mikheev 2019-04-25 13:22:30 +04:00
parent aaaf782754
commit e186114026
5 changed files with 75 additions and 60 deletions

View File

@ -2,15 +2,18 @@
import fetchTokens from '~/logic/tokens/store/actions/fetchTokens' import fetchTokens from '~/logic/tokens/store/actions/fetchTokens'
import { addToken } from '~/logic/tokens/store/actions/addToken' import { addToken } from '~/logic/tokens/store/actions/addToken'
import updateActiveTokens from '~/routes/safe/store/actions/updateActiveTokens' import updateActiveTokens from '~/routes/safe/store/actions/updateActiveTokens'
import activateTokenForAllSafes from '~/routes/safe/store/actions/activateTokenForAllSafes'
export type Actions = { export type Actions = {
fetchTokens: Function, fetchTokens: Function,
updateActiveTokens: Function, updateActiveTokens: Function,
addToken: Function, addToken: Function,
activateTokenForAllSafes: Function,
} }
export default { export default {
fetchTokens, fetchTokens,
addToken, addToken,
updateActiveTokens, updateActiveTokens,
activateTokenForAllSafes,
} }

View File

@ -1,5 +1,5 @@
// @flow // @flow
import * as React from 'react' import React, { useState } from 'react'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { List } from 'immutable' import { List } from 'immutable'
import { withStyles } from '@material-ui/core/styles' import { withStyles } from '@material-ui/core/styles'
@ -22,60 +22,55 @@ type Props = Actions & {
activeTokens: List<Token>, activeTokens: List<Token>,
} }
type State = { const Tokens = (props: Props) => {
activeScreen: string, const [activeScreen, setActiveScreen] = useState<string>('tokenList')
} const {
onClose,
classes,
tokens,
activeTokens,
fetchTokens,
updateActiveTokens,
safeAddress,
addToken,
activateTokenForAllSafes,
} = props
class Tokens extends React.Component<Props, State> { return (
state = { <React.Fragment>
activeScreen: 'tokenList', <Row align="center" grow className={classes.heading}>
} <Paragraph className={classes.manage} noMargin>
Manage Tokens
setActiveScreen = (activeScreen: string) => this.setState({ </Paragraph>
activeScreen, <IconButton onClick={onClose} disableRipple>
}) <Close className={classes.close} />
</IconButton>
render() { </Row>
const { <Hairline />
onClose, classes, tokens, activeTokens, fetchTokens, updateActiveTokens, safeAddress, addToken, {activeScreen === 'tokenList' && (
} = this.props <TokenList
const { activeScreen } = this.state tokens={tokens}
activeTokens={activeTokens}
return ( fetchTokens={fetchTokens}
<React.Fragment> updateActiveTokens={updateActiveTokens}
<Row align="center" grow className={classes.heading}> safeAddress={safeAddress}
<Paragraph className={classes.manage} noMargin> setActiveScreen={setActiveScreen}
Manage Tokens />
</Paragraph> )}
<IconButton onClick={onClose} disableRipple> {activeScreen === 'addCustomToken' && (
<Close className={classes.close} /> <AddCustomToken
</IconButton> setActiveScreen={setActiveScreen}
</Row> onClose={onClose}
<Hairline /> addToken={addToken}
{activeScreen === 'tokenList' && ( safeAddress={safeAddress}
<TokenList activeTokens={activeTokens}
tokens={tokens} updateActiveTokens={updateActiveTokens}
activeTokens={activeTokens} activateTokenForAllSafes={activateTokenForAllSafes}
fetchTokens={fetchTokens} tokens={tokens}
updateActiveTokens={updateActiveTokens} />
safeAddress={safeAddress} )}
setActiveScreen={this.setActiveScreen} </React.Fragment>
/> )
)}
{activeScreen === 'addCustomToken' && (
<AddCustomToken
setActiveScreen={this.setActiveScreen}
onClose={onClose}
addToken={addToken}
safeAddress={safeAddress}
activeTokens={activeTokens}
updateActiveTokens={updateActiveTokens}
tokens={tokens}
/>
)}
</React.Fragment>
)
}
} }
const TokenComponent = withStyles(styles)(Tokens) const TokenComponent = withStyles(styles)(Tokens)

View File

@ -32,6 +32,7 @@ type Props = {
tokens: List<Token>, tokens: List<Token>,
setActiveScreen: Function, setActiveScreen: Function,
onClose: Function, onClose: Function,
activateTokenForAllSafes: Function,
} }
const INITIAL_FORM_STATE = { const INITIAL_FORM_STATE = {
@ -43,12 +44,11 @@ const INITIAL_FORM_STATE = {
const AddCustomToken = (props: Props) => { const AddCustomToken = (props: Props) => {
const { const {
classes, setActiveScreen, onClose, addToken, updateActiveTokens, safeAddress, activeTokens, tokens, classes, setActiveScreen, onClose, addToken, updateActiveTokens, safeAddress, activeTokens, tokens, activateTokenForAllSafes,
} = props } = props
const [formValues, setFormValues] = useState(INITIAL_FORM_STATE) const [formValues, setFormValues] = useState(INITIAL_FORM_STATE)
const handleSubmit = (values) => { const handleSubmit = (values) => {
const activeTokensAddresses = activeTokens.map(({ address }) => address)
const token = { const token = {
address: values.address, address: values.address,
decimals: values.decimals, decimals: values.decimals,
@ -57,7 +57,13 @@ const AddCustomToken = (props: Props) => {
} }
addToken(token) addToken(token)
updateActiveTokens(safeAddress, activeTokensAddresses.push(token.address)) if (values.showForAllSafes) {
activateTokenForAllSafes(token.address)
} else {
const activeTokensAddresses = activeTokens.map(({ address }) => address)
updateActiveTokens(safeAddress, activeTokensAddresses.push(token.address))
}
onClose() onClose()
} }

View File

@ -9,8 +9,9 @@ import { getActiveTokensAddressesForAllSafes } from '~/routes/safe/store/selecto
import { tokensSelector } from '~/logic/tokens/store/selectors' import { tokensSelector } from '~/logic/tokens/store/selectors'
import type { Token } from '~/logic/tokens/store/model/token' import type { Token } from '~/logic/tokens/store/model/token'
import { saveActiveTokens } from '~/logic/tokens/utils/tokensStorage' import { saveActiveTokens } from '~/logic/tokens/utils/tokensStorage'
import { ACTIVATE_TOKEN_FOR_ALL_SAFES } from '~/routes/safe/store/actions/activateTokenForAllSafes';
const watchedActions = [ADD_SAFE, UPDATE_SAFE] const watchedActions = [ADD_SAFE, UPDATE_SAFE, ACTIVATE_TOKEN_FOR_ALL_SAFES]
const safeStorageMware = (store: Store<GlobalState>) => (next: Function) => async (action: AnyAction) => { const safeStorageMware = (store: Store<GlobalState>) => (next: Function) => async (action: AnyAction) => {
const handledAction = next(action) const handledAction = next(action)
@ -21,7 +22,7 @@ const safeStorageMware = (store: Store<GlobalState>) => (next: Function) => asyn
saveSafes(safes.toJSON()) saveSafes(safes.toJSON())
// recalculate active tokens // recalculate active tokens
if (action.payload.activeTokens) { if (action.payload.activeTokens || action.type === ACTIVATE_TOKEN_FOR_ALL_SAFES) {
const tokens = tokensSelector(state) const tokens = tokensSelector(state)
const activeTokenAddresses = getActiveTokensAddressesForAllSafes(state) const activeTokenAddresses = getActiveTokensAddressesForAllSafes(state)

View File

@ -67,7 +67,17 @@ export default handleActions<State, *>(
[ACTIVATE_TOKEN_FOR_ALL_SAFES]: (state: State, action: ActionType<Function>): State => { [ACTIVATE_TOKEN_FOR_ALL_SAFES]: (state: State, action: ActionType<Function>): State => {
const tokenAddress = action.payload const tokenAddress = action.payload
return state const newState = state.withMutations((map) => {
console.log(map.keys())
map.keySeq().forEach((safeAddress) => {
const safeActiveTokens = map.getIn([safeAddress, 'activeTokens'])
const activeTokens = safeActiveTokens.push(tokenAddress)
map.update(safeAddress, prevSafe => prevSafe.merge({ activeTokens }))
})
})
return newState
}, },
[ADD_SAFE]: (state: State, action: ActionType<Function>): State => { [ADD_SAFE]: (state: State, action: ActionType<Function>): State => {
const { safe }: { safe: SafeProps } = action.payload const { safe }: { safe: SafeProps } = action.payload