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"
/>
</div>
{console.log(nodeSelection)}
<div
className={classnames({
'Header-branding-right-dropdown': true,

View File

@ -70,7 +70,6 @@ class LogOutPromptClass extends React.Component<Props, State> {
private onConfirm = () => {
const { nextLocation: next } = this.state;
this.props.resetWallet();
this.props.web3UnsetNode();
this.setState(
{
openModal: false,
@ -79,6 +78,7 @@ class LogOutPromptClass extends React.Component<Props, State> {
() => {
if (next) {
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 = {
nodeId: 'eth_auto',
prevNode: 'eth_auto',
pending: false
};
const changeNode = (_: State, { payload }: ChangeNodeAction): State => ({
const changeNode = (state: State, { payload }: ChangeNodeAction): State => ({
nodeId: payload.nodeId,
// make sure we dont accidentally switch back to a web3 node
prevNode: state.nodeId === 'web3' ? state.prevNode : state.nodeId,
pending: false
});

View File

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

View File

@ -11,7 +11,7 @@ import {
} from 'actions/config';
import {
getNodeId,
getStaticAltNodeIdToWeb3,
getPreviouslySelectedNode,
getNetworkNameByChainId,
getWeb3Node
} from 'selectors/config';
@ -106,10 +106,10 @@ export function* unsetWeb3NodeOnWalletEvent(action: SetWalletAction): SagaIterat
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
yield put(changeNodeForce(altNodeId));
yield put(changeNodeForce(prevNodeId));
}
export function* unsetWeb3Node(): SagaIterator {
@ -119,10 +119,10 @@ export function* unsetWeb3Node(): SagaIterator {
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
yield put(changeNodeForce(altNodeId));
yield put(changeNodeForce(prevNodeId));
}
export const web3 = [

View File

@ -6,10 +6,7 @@ import {
} from 'selectors/config';
import { CustomNodeConfig, StaticNodeConfig, StaticNodeId } from 'types/node';
import { StaticNetworkIds } from 'types/network';
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';
export const getNodes = (state: AppState) => getConfig(state).nodes;
@ -23,21 +20,6 @@ export const getCustomNodeFromId = (
nodeId: string
): 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) =>
getStaticNodeConfigs(state)[nodeId];
@ -82,6 +64,10 @@ export function getSelectedNode(state: AppState) {
return getNodes(state).selectedNode;
}
export function getPreviouslySelectedNode(state: AppState) {
return getSelectedNode(state).prevNode;
}
export function isNodeChanging(state: AppState): boolean {
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
if (savedNodeId === 'web3') {
return { nodeId: initialState.nodeId, pending: false };
return { nodeId: initialState.nodeId, prevNode: initialState.nodeId, pending: false };
}
const nodeConfigExists = isStaticNodeId(appInitialState, savedNodeId)
@ -143,7 +143,8 @@ function getSavedSelectedNode(
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(

View File

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

View File

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