mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-11 03:26:14 +00:00
Infer selected network via node
This commit is contained in:
parent
f7a9162279
commit
77cf4b8f50
@ -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<State>({
|
||||
customNetworks,
|
||||
staticNetworks,
|
||||
selectedNetwork
|
||||
staticNetworks
|
||||
});
|
||||
|
||||
export { State, networks, StaticNetworksState, SelectedNetworkState, CustomNetworksState };
|
||||
export { State, networks, StaticNetworksState, CustomNetworksState };
|
||||
|
@ -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;
|
||||
}
|
||||
};
|
@ -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<CustomTokenState>('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;
|
||||
}
|
||||
|
||||
|
@ -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 };
|
Loading…
x
Reference in New Issue
Block a user