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,24 +22,19 @@ type Props = Actions & {
activeTokens: List<Token>, activeTokens: List<Token>,
} }
type State = { const Tokens = (props: Props) => {
activeScreen: string, const [activeScreen, setActiveScreen] = useState<string>('tokenList')
}
class Tokens extends React.Component<Props, State> {
state = {
activeScreen: 'tokenList',
}
setActiveScreen = (activeScreen: string) => this.setState({
activeScreen,
})
render() {
const { const {
onClose, classes, tokens, activeTokens, fetchTokens, updateActiveTokens, safeAddress, addToken, onClose,
} = this.props classes,
const { activeScreen } = this.state tokens,
activeTokens,
fetchTokens,
updateActiveTokens,
safeAddress,
addToken,
activateTokenForAllSafes,
} = props
return ( return (
<React.Fragment> <React.Fragment>
@ -59,24 +54,24 @@ class Tokens extends React.Component<Props, State> {
fetchTokens={fetchTokens} fetchTokens={fetchTokens}
updateActiveTokens={updateActiveTokens} updateActiveTokens={updateActiveTokens}
safeAddress={safeAddress} safeAddress={safeAddress}
setActiveScreen={this.setActiveScreen} setActiveScreen={setActiveScreen}
/> />
)} )}
{activeScreen === 'addCustomToken' && ( {activeScreen === 'addCustomToken' && (
<AddCustomToken <AddCustomToken
setActiveScreen={this.setActiveScreen} setActiveScreen={setActiveScreen}
onClose={onClose} onClose={onClose}
addToken={addToken} addToken={addToken}
safeAddress={safeAddress} safeAddress={safeAddress}
activeTokens={activeTokens} activeTokens={activeTokens}
updateActiveTokens={updateActiveTokens} updateActiveTokens={updateActiveTokens}
activateTokenForAllSafes={activateTokenForAllSafes}
tokens={tokens} tokens={tokens}
/> />
)} )}
</React.Fragment> </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)
if (values.showForAllSafes) {
activateTokenForAllSafes(token.address)
} else {
const activeTokensAddresses = activeTokens.map(({ address }) => address)
updateActiveTokens(safeAddress, activeTokensAddresses.push(token.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