diff --git a/common/reducers/config/networks/index.ts b/common/reducers/config/networks/index.ts index c4a6b49e..9c0a2760 100644 --- a/common/reducers/config/networks/index.ts +++ b/common/reducers/config/networks/index.ts @@ -1,18 +1,15 @@ import { customNetworks, State as CustomNetworksState } from './customNetworks'; import { staticNetworks, State as StaticNetworksState } from './staticNetworks'; -import { selectedNetwork, State as SelectedNetworkState } from './selectedNetwork'; import { combineReducers } from 'redux'; interface State { customNetworks: CustomNetworksState; staticNetworks: StaticNetworksState; - selectedNetwork: SelectedNetworkState; } const networks = combineReducers({ customNetworks, - staticNetworks, - selectedNetwork + staticNetworks }); -export { State, networks, StaticNetworksState, SelectedNetworkState, CustomNetworksState }; +export { State, networks, StaticNetworksState, CustomNetworksState }; diff --git a/common/reducers/config/networks/selectedNetwork.ts b/common/reducers/config/networks/selectedNetwork.ts deleted file mode 100644 index 677364bc..00000000 --- a/common/reducers/config/networks/selectedNetwork.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { NodeAction, TypeKeys, ChangeNodeAction } from 'actions/config'; -import { INITIAL_STATE as INITIAL_NODE_STATE } from '../nodes/selectedNode'; -import { INITIAL_STATE as INITIAL_DEFAULT_NODE_STATE } from '../nodes/staticNodes'; -import { NonWeb3NodeConfigs } from 'types/node'; -import { StaticNetworkIds } from 'types/network'; - -const initalNode = - INITIAL_DEFAULT_NODE_STATE[INITIAL_NODE_STATE.nodeId as keyof NonWeb3NodeConfigs]; - -export type State = string | StaticNetworkIds; - -const INITIAL_STATE: State = initalNode.network; - -const handleNodeChange = (_: State, { payload }: ChangeNodeAction) => payload.networkId; - -export const selectedNetwork = (state: State = INITIAL_STATE, action: NodeAction) => { - switch (action.type) { - case TypeKeys.CONFIG_NODE_CHANGE: - return handleNodeChange(state, action); - default: - return state; - } -}; diff --git a/common/store/configAndTokens.ts b/common/store/configAndTokens.ts index ad0ce340..7dd7ae0d 100644 --- a/common/store/configAndTokens.ts +++ b/common/store/configAndTokens.ts @@ -11,8 +11,7 @@ import { getLanguageSelection, getCustomNodeConfigs, getSelectedNode, - getCustomNetworkConfigs, - getSelectedNetwork + getCustomNetworkConfigs } from 'selectors/config'; import RootReducer, { AppState } from 'reducers'; import CustomNode from 'libs/nodes/custom'; @@ -27,8 +26,7 @@ export function getConfigAndCustomTokensStateToSubscribe( meta: { languageSelection: getLanguageSelection(state) }, nodes: { customNodes: getCustomNodeConfigs(state), selectedNode: getSelectedNode(state) }, networks: { - customNetworks: getCustomNetworkConfigs(state), - selectedNetwork: getSelectedNetwork(state) + customNetworks: getCustomNetworkConfigs(state) } }; @@ -58,20 +56,33 @@ export function rehydrateConfigAndCustomTokenState() { nextConfigState.meta = { ...nextConfigState.meta, ...savedConfigState.meta }; } - const nextCustomTokenState = rehydrateCustomTokens(nextConfigState.networks); + const { customNodes, selectedNode: { nodeId }, staticNodes } = nextConfigState.nodes; + const selectedNode = isStaticNodeId(appInitialState, nodeId) + ? staticNodes[nodeId] + : customNodes[nodeId]; + + if (!selectedNode) { + return { config: configInitialState, customTokens: customTokensInitialState }; + } + + const nextCustomTokenState = rehydrateCustomTokens( + nextConfigState.networks, + selectedNode.network + ); return { config: nextConfigState, customTokens: nextCustomTokenState }; } -function rehydrateCustomTokens(networkState: ConfigState['networks']) { +function rehydrateCustomTokens(networkState: ConfigState['networks'], selectedNetwork: string) { // Dedupe custom tokens initially const savedCustomTokensState = loadStatePropertyOrEmptyObject('customTokens') || customTokensInitialState; - const { customNetworks, selectedNetwork, staticNetworks } = networkState; + const { customNetworks, staticNetworks } = networkState; const network = isStaticNetworkId(appInitialState, selectedNetwork) ? staticNetworks[selectedNetwork] : customNetworks[selectedNetwork]; + return network.isCustom ? savedCustomTokensState : dedupeCustomTokens(network.tokens, savedCustomTokensState); @@ -83,13 +94,6 @@ function rehydrateNetworks( ): ConfigState['networks'] { const nextNetworkState = { ...initialState }; nextNetworkState.customNetworks = savedState.customNetworks; - const { customNetworks, selectedNetwork, staticNetworks } = nextNetworkState; - const nextSelectedNetwork = isStaticNetworkId(appInitialState, savedState.selectedNetwork) - ? staticNetworks[selectedNetwork] - : customNetworks[selectedNetwork]; - nextNetworkState.selectedNetwork = nextSelectedNetwork - ? savedState.selectedNetwork - : initialState.selectedNetwork; return nextNetworkState; } diff --git a/spec/reducers/config/networks/selectedNetwork.spec.ts b/spec/reducers/config/networks/selectedNetwork.spec.ts deleted file mode 100644 index 1795bac1..00000000 --- a/spec/reducers/config/networks/selectedNetwork.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { actions } from '../nodes/selectedNode.spec'; -import { selectedNetwork } from 'reducers/config/networks/selectedNetwork'; - -const expectedState = { - initialState: 'ETH', - nodeChange: 'networkToChangeTo' -}; - -describe('selected network reducer', () => { - it('should return the initial state', () => - expect(selectedNetwork(undefined, {} as any)).toEqual(expectedState.initialState)); - it('should handle changing nodes by changing to the right network', () => - expect(selectedNetwork(expectedState.initialState, actions.changeNode)).toEqual( - expectedState.nodeChange - )); -}); - -export { actions as selectedNetworkActions, expectedState as selectedNetworkExpectedState };