Refactor configNodes

This commit is contained in:
Connor Bryan 2018-07-13 18:36:29 -05:00
parent b6b5fe801f
commit 05f8e5a53b
6 changed files with 36 additions and 32 deletions

View File

@ -1,26 +1,26 @@
import * as types from './types';
const addCustomNode = (
state: types.CustomNodesState,
state: types.ConfigCustomNodesState,
{ payload }: types.AddCustomNodeAction
): types.CustomNodesState => ({
): types.ConfigCustomNodesState => ({
...state,
[payload.id]: payload
});
function removeCustomNode(
state: types.CustomNodesState,
state: types.ConfigCustomNodesState,
{ payload }: types.RemoveCustomNodeAction
): types.CustomNodesState {
): types.ConfigCustomNodesState {
const stateCopy = { ...state };
Reflect.deleteProperty(stateCopy, payload);
return stateCopy;
}
export function customNodesReducer(
state: types.CustomNodesState = {},
state: types.ConfigCustomNodesState = {},
action: types.CustomNodeAction
): types.CustomNodesState {
): types.ConfigCustomNodesState {
switch (action.type) {
case types.ConfigNodesCustomActions.ADD:
return addCustomNode(state, action);

View File

@ -1,6 +1,6 @@
import { CustomNodeConfig } from 'types/node';
export interface CustomNodesState {
export interface ConfigCustomNodesState {
[customNodeId: string]: CustomNodeConfig;
}

View File

@ -1,6 +1,8 @@
export * from './types';
export * from './reducer';
export * from './selectors';
import * as configNodesTypes from './types';
import * as configNodesReducer from './reducer';
import * as configNodesSelectors from './selectors';
export { configNodesTypes, configNodesReducer, configNodesSelectors };
export * from './custom';
export * from './selected';

View File

@ -1,12 +1,12 @@
import { combineReducers } from 'redux';
import { customNodesReducer } from './custom/reducer';
import { staticNodesReducer } from './static/reducer';
import { selectedNodeReducer } from './selected/reducer';
import { NodesState } from './types';
import * as configNodesCustomReducer from './custom/reducer';
import * as configNodesStaticReducer from './static/reducer';
import * as configNodesSelectedReducer from './selected/reducer';
import * as types from './types';
export const nodesReducer = combineReducers<NodesState>({
customNodes: customNodesReducer,
staticNodes: staticNodesReducer,
selectedNode: selectedNodeReducer
export const nodesReducer = combineReducers<types.ConfigNodesState>({
customNodes: configNodesCustomReducer.customNodesReducer,
staticNodes: configNodesStaticReducer.staticNodesReducer,
selectedNode: configNodesSelectedReducer.selectedNodeReducer
});

View File

@ -1,9 +1,9 @@
import { shepherdProvider, INode } from 'libs/nodes';
import { CustomNodeConfig, StaticNodeConfig } from 'types/node';
import { AppState } from 'features/reducers';
import { getCustomNodeConfig } from './custom/selectors';
import { getNodeId } from './selected/selectors';
import { getStaticNodeConfig } from './static/selectors';
import * as configNodesCustomSelectors from './custom/selectors';
import * as configNodesSelectedSelectors from './selected/selectors';
import * as configNodesStaticSelectors from './static/selectors';
function getConfig(state: AppState) {
return state.config;
@ -15,8 +15,8 @@ export function getNodes(state: AppState) {
export const getWeb3Node = (state: AppState): StaticNodeConfig | null => {
const isWeb3Node = (nodeId: string) => nodeId === 'web3';
const currNode = getStaticNodeConfig(state);
const currNodeId = getNodeId(state);
const currNode = configNodesStaticSelectors.getStaticNodeConfig(state);
const currNodeId = configNodesSelectedSelectors.getNodeId(state);
if (currNode && currNodeId && isWeb3Node(currNodeId)) {
return currNode;
}
@ -24,7 +24,7 @@ export const getWeb3Node = (state: AppState): StaticNodeConfig | null => {
};
export function getIsWeb3Node(state: AppState): boolean {
return getNodeId(state) === 'web3';
return configNodesSelectedSelectors.getNodeId(state) === 'web3';
}
export function getNodeLib(_: AppState): INode {
@ -32,7 +32,9 @@ export function getNodeLib(_: AppState): INode {
}
export function getNodeConfig(state: AppState): StaticNodeConfig | CustomNodeConfig {
const config = getStaticNodeConfig(state) || getCustomNodeConfig(state);
const config =
configNodesStaticSelectors.getStaticNodeConfig(state) ||
configNodesCustomSelectors.getCustomNodeConfig(state);
if (!config) {
const { selectedNode } = getNodes(state);

View File

@ -1,9 +1,9 @@
import { CustomNodesState } from './custom/types';
import { StaticNodesState } from './static/types';
import { SelectedNodeState } from './selected/types';
import * as configNodesCustomTypes from './custom/types';
import * as configNodesStaticTypes from './static/types';
import * as configNodesSelectedTypes from './selected/types';
export interface NodesState {
customNodes: CustomNodesState;
staticNodes: StaticNodesState;
selectedNode: SelectedNodeState;
export interface ConfigNodesState {
customNodes: configNodesCustomTypes.ConfigCustomNodesState;
staticNodes: configNodesStaticTypes.ConfigStaticNodesState;
selectedNode: configNodesSelectedTypes.ConfigNodesSelectedState;
}