William O'Beirne a043334685 Node Refactor (#1603)
* Initial work on refactoring node definitions to reduce number of places theyre defined, amount of copy pasting.

* Use makeAutoNodeNAme instead of manually appending _auto

* Add getNetVersion to list of unsupported methods

* PR feedback

* Rework web template node selector to be a network selector. Refactor some types to help with that. Better handle removing custom nodes.

* Remove color dropdown.

* Fix selecting custom networks. Show notification if change network intent fails.

* Use selectors for current node / network instead of intuiting from nodeSelection

* Add id key to all networks, simplify add and remove custom node and network functions.

* Fix a lot of uses of network.name to use network.id instead.

* Dont allow network chainid conflicts

* Fix web3 network by chainid

* Add testnet badge to network selector

* Change nomenclature from change(Node|Network)(Intent)? to change(Node|Network)(Requested|Succeeded)

* tscheck

* Better code for chainid collision

* Remove console logs

* Fix tests

* Network selector becomes self contained component used both by web header and electron nav.

* Dont select node again

* Additional title text

* tscheck

* Custom node behavior in Electron

* Close panel too

* Convert node label data into selector function

* tscheck

* Parens & space
2018-05-29 09:51:42 -05:00

50 lines
1.3 KiB
TypeScript

import { TypeKeys, NodeAction } from 'actions/config';
import { NODE_CONFIGS, makeAutoNodeName } from 'libs/nodes';
import { StaticNodesState } from './types';
import { RawNodeConfig } from 'types/node';
import { StaticNetworkIds } from 'types/network';
function makeStateFromNodeConfigs(prev: Partial<StaticNodesState>, network: StaticNetworkIds) {
// Auto network
const autoId = makeAutoNodeName(network);
prev[autoId] = {
network,
id: autoId,
isAuto: true,
isCustom: false,
service: 'AUTO'
};
// Static networks
NODE_CONFIGS[network].forEach((config: RawNodeConfig) => {
prev[config.name] = {
network,
id: config.name,
isCustom: false,
service: config.service
};
});
return prev;
}
export const INITIAL_STATE: StaticNodesState = Object.keys(NODE_CONFIGS).reduce(
makeStateFromNodeConfigs,
{}
);
const staticNodes = (state: StaticNodesState = INITIAL_STATE, action: NodeAction) => {
switch (action.type) {
case TypeKeys.CONFIG_NODE_WEB3_SET:
return { ...state, [action.payload.id]: action.payload.config };
case TypeKeys.CONFIG_NODE_WEB3_UNSET:
const stateCopy = { ...state };
Reflect.deleteProperty(stateCopy, 'web3');
return stateCopy;
default:
return state;
}
};
export { StaticNodesState, staticNodes };