Unset Web3 to previous node (#1472)

This commit is contained in:
HenryNguyen5 2018-04-09 13:23:37 -04:00 committed by Daniel Ternyak
parent 05a751b1f3
commit b9694c7be8
9 changed files with 29 additions and 38 deletions

View File

@ -161,7 +161,6 @@ class Header extends Component<Props, State> {
color="white" color="white"
/> />
</div> </div>
{console.log(nodeSelection)}
<div <div
className={classnames({ className={classnames({
'Header-branding-right-dropdown': true, 'Header-branding-right-dropdown': true,

View File

@ -70,7 +70,6 @@ class LogOutPromptClass extends React.Component<Props, State> {
private onConfirm = () => { private onConfirm = () => {
const { nextLocation: next } = this.state; const { nextLocation: next } = this.state;
this.props.resetWallet(); this.props.resetWallet();
this.props.web3UnsetNode();
this.setState( this.setState(
{ {
openModal: false, openModal: false,
@ -79,6 +78,7 @@ class LogOutPromptClass extends React.Component<Props, State> {
() => { () => {
if (next) { if (next) {
this.props.history.push(`${next.pathname}${next.search}${next.hash}`); this.props.history.push(`${next.pathname}${next.search}${next.hash}`);
this.props.web3UnsetNode();
} }
} }
); );

View File

@ -10,11 +10,14 @@ import { SelectedNodeState as State } from './types';
export const INITIAL_STATE: State = { export const INITIAL_STATE: State = {
nodeId: 'eth_auto', nodeId: 'eth_auto',
prevNode: 'eth_auto',
pending: false pending: false
}; };
const changeNode = (_: State, { payload }: ChangeNodeAction): State => ({ const changeNode = (state: State, { payload }: ChangeNodeAction): State => ({
nodeId: payload.nodeId, nodeId: payload.nodeId,
// make sure we dont accidentally switch back to a web3 node
prevNode: state.nodeId === 'web3' ? state.prevNode : state.nodeId,
pending: false pending: false
}); });

View File

@ -8,11 +8,13 @@ export interface CustomNodesState {
interface NodeLoaded { interface NodeLoaded {
pending: false; pending: false;
prevNode: string;
nodeId: string; nodeId: string;
} }
interface NodeChangePending { interface NodeChangePending {
pending: true; pending: true;
prevNode: string;
nodeId: string; nodeId: string;
} }

View File

@ -11,7 +11,7 @@ import {
} from 'actions/config'; } from 'actions/config';
import { import {
getNodeId, getNodeId,
getStaticAltNodeIdToWeb3, getPreviouslySelectedNode,
getNetworkNameByChainId, getNetworkNameByChainId,
getWeb3Node getWeb3Node
} from 'selectors/config'; } from 'selectors/config';
@ -106,10 +106,10 @@ export function* unsetWeb3NodeOnWalletEvent(action: SetWalletAction): SagaIterat
return; return;
} }
const altNodeId: string = yield select(getStaticAltNodeIdToWeb3); const prevNodeId: string = yield select(getPreviouslySelectedNode);
// forcefully switch back to a node with the same network as MetaMask/Mist // forcefully switch back to a node with the same network as MetaMask/Mist
yield put(changeNodeForce(altNodeId)); yield put(changeNodeForce(prevNodeId));
} }
export function* unsetWeb3Node(): SagaIterator { export function* unsetWeb3Node(): SagaIterator {
@ -119,10 +119,10 @@ export function* unsetWeb3Node(): SagaIterator {
return; return;
} }
const altNodeId: string = yield select(getStaticAltNodeIdToWeb3); const prevNodeId: string = yield select(getPreviouslySelectedNode);
// forcefully switch back to a node with the same network as MetaMask/Mist // forcefully switch back to a node with the same network as MetaMask/Mist
yield put(changeNodeForce(altNodeId)); yield put(changeNodeForce(prevNodeId));
} }
export const web3 = [ export const web3 = [

View File

@ -6,10 +6,7 @@ import {
} from 'selectors/config'; } from 'selectors/config';
import { CustomNodeConfig, StaticNodeConfig, StaticNodeId } from 'types/node'; import { CustomNodeConfig, StaticNodeConfig, StaticNodeId } from 'types/node';
import { StaticNetworkIds } from 'types/network'; import { StaticNetworkIds } from 'types/network';
const getConfig = (state: AppState) => state.config; const getConfig = (state: AppState) => state.config;
import { INITIAL_STATE as SELECTED_NODE_INITIAL_STATE } from 'reducers/config/nodes/selectedNode';
import { shepherdProvider, INode, stripWeb3Network } from 'libs/nodes'; import { shepherdProvider, INode, stripWeb3Network } from 'libs/nodes';
export const getNodes = (state: AppState) => getConfig(state).nodes; export const getNodes = (state: AppState) => getConfig(state).nodes;
@ -23,21 +20,6 @@ export const getCustomNodeFromId = (
nodeId: string nodeId: string
): CustomNodeConfig | undefined => getCustomNodeConfigs(state)[nodeId]; ): CustomNodeConfig | undefined => getCustomNodeConfigs(state)[nodeId];
export const getStaticAltNodeIdToWeb3 = (state: AppState) => {
const { web3, ...configs } = getStaticNodeConfigs(state);
if (!web3) {
return SELECTED_NODE_INITIAL_STATE.nodeId;
}
const res = Object.entries(configs).find(
([_, config]: [StaticNodeId, StaticNodeConfig]) =>
stripWeb3Network(web3.network) === config.network
);
if (res) {
return res[0];
}
return SELECTED_NODE_INITIAL_STATE.nodeId;
};
export const getStaticNodeFromId = (state: AppState, nodeId: StaticNodeId) => export const getStaticNodeFromId = (state: AppState, nodeId: StaticNodeId) =>
getStaticNodeConfigs(state)[nodeId]; getStaticNodeConfigs(state)[nodeId];
@ -82,6 +64,10 @@ export function getSelectedNode(state: AppState) {
return getNodes(state).selectedNode; return getNodes(state).selectedNode;
} }
export function getPreviouslySelectedNode(state: AppState) {
return getSelectedNode(state).prevNode;
}
export function isNodeChanging(state: AppState): boolean { export function isNodeChanging(state: AppState): boolean {
return getSelectedNode(state).pending; return getSelectedNode(state).pending;
} }

View File

@ -129,7 +129,7 @@ function getSavedSelectedNode(
// necessary because web3 is only initialized as a node upon MetaMask / Mist unlock // necessary because web3 is only initialized as a node upon MetaMask / Mist unlock
if (savedNodeId === 'web3') { if (savedNodeId === 'web3') {
return { nodeId: initialState.nodeId, pending: false }; return { nodeId: initialState.nodeId, prevNode: initialState.nodeId, pending: false };
} }
const nodeConfigExists = isStaticNodeId(appInitialState, savedNodeId) const nodeConfigExists = isStaticNodeId(appInitialState, savedNodeId)
@ -143,7 +143,8 @@ function getSavedSelectedNode(
shepherd.manual(savedNodeId, false); shepherd.manual(savedNodeId, false);
} }
} }
return { nodeId: nodeConfigExists ? savedNodeId : initialState.nodeId, pending: false }; const nodeId = nodeConfigExists ? savedNodeId : initialState.nodeId;
return { nodeId, prevNode: nodeId, pending: false };
} }
function rehydrateCustomNodes( function rehydrateCustomNodes(

View File

@ -22,7 +22,7 @@ import {
isStaticNodeId, isStaticNodeId,
getStaticNodeFromId, getStaticNodeFromId,
getCustomNodeFromId, getCustomNodeFromId,
getStaticAltNodeIdToWeb3 getPreviouslySelectedNode
} from 'selectors/config'; } from 'selectors/config';
import { Web3Wallet } from 'libs/wallet'; import { Web3Wallet } from 'libs/wallet';
import { showNotification } from 'actions/notifications'; import { showNotification } from 'actions/notifications';
@ -269,7 +269,7 @@ describe('handleNodeChangeIntent*', () => {
}); });
describe('unsetWeb3Node*', () => { describe('unsetWeb3Node*', () => {
const alternativeNodeId = 'eth_mycrypto'; const previousNodeId = 'eth_mycrypto';
const mockNodeId = 'web3'; const mockNodeId = 'web3';
const gen = unsetWeb3Node(); const gen = unsetWeb3Node();
@ -279,11 +279,11 @@ describe('unsetWeb3Node*', () => {
it('should select an alternative node to web3', () => { it('should select an alternative node to web3', () => {
// get a 'no visual difference' error here // get a 'no visual difference' error here
expect(gen.next(mockNodeId).value).toEqual(select(getStaticAltNodeIdToWeb3)); expect(gen.next(mockNodeId).value).toEqual(select(getPreviouslySelectedNode));
}); });
it('should put changeNodeForce', () => { it('should put changeNodeForce', () => {
expect(gen.next(alternativeNodeId).value).toEqual(put(changeNodeForce(alternativeNodeId))); expect(gen.next(previousNodeId).value).toEqual(put(changeNodeForce(previousNodeId)));
}); });
it('should be done', () => { it('should be done', () => {
@ -301,7 +301,7 @@ describe('unsetWeb3Node*', () => {
describe('unsetWeb3NodeOnWalletEvent*', () => { describe('unsetWeb3NodeOnWalletEvent*', () => {
const fakeAction: any = {}; const fakeAction: any = {};
const mockNodeId = 'web3'; const mockNodeId = 'web3';
const alternativeNodeId = 'eth_mycrypto'; const previousNodeId = 'eth_mycrypto';
const gen = unsetWeb3NodeOnWalletEvent(fakeAction); const gen = unsetWeb3NodeOnWalletEvent(fakeAction);
it('should select getNode', () => { it('should select getNode', () => {
@ -309,11 +309,11 @@ describe('unsetWeb3NodeOnWalletEvent*', () => {
}); });
it('should select an alternative node to web3', () => { it('should select an alternative node to web3', () => {
expect(gen.next(mockNodeId).value).toEqual(select(getStaticAltNodeIdToWeb3)); expect(gen.next(mockNodeId).value).toEqual(select(getPreviouslySelectedNode));
}); });
it('should put changeNodeForce', () => { it('should put changeNodeForce', () => {
expect(gen.next(alternativeNodeId).value).toEqual(put(changeNodeForce(alternativeNodeId))); expect(gen.next(previousNodeId).value).toEqual(put(changeNodeForce(previousNodeId)));
}); });
it('should be done', () => { it('should be done', () => {

View File

@ -3,9 +3,9 @@ import { selectedNode } from 'reducers/config/nodes/selectedNode';
import { SelectedNodeState } from 'reducers/config/nodes/types'; import { SelectedNodeState } from 'reducers/config/nodes/types';
export const expectedState = { export const expectedState = {
initialState: { nodeId: 'eth_mycrypto', pending: false }, initialState: { nodeId: 'eth_mycrypto', prevNode: 'eth_mycrypto', pending: false },
nodeChange: { nodeId: 'nodeToChangeTo', pending: false }, nodeChange: { nodeId: 'nodeToChangeTo', prevNode: 'eth_auto', pending: false },
nodeChangeIntent: { nodeId: 'eth_mycrypto', pending: true } nodeChangeIntent: { nodeId: 'eth_mycrypto', prevNode: 'eth_mycrypto', pending: true }
}; };
export const actions = { export const actions = {