Refactor configMetaNetworks

This commit is contained in:
Connor Bryan 2018-07-13 17:38:31 -05:00
parent 97bfbba51f
commit 09308e3071
6 changed files with 40 additions and 29 deletions

View File

@ -1,11 +1,11 @@
import { CONFIG_NETWORKS, ChangeNetworkRequestedAction } from './types';
import * as types from './types';
export type TChangeNetworkRequested = typeof changeNetworkRequested;
export function changeNetworkRequested(
payload: ChangeNetworkRequestedAction['payload']
): ChangeNetworkRequestedAction {
payload: types.ChangeNetworkRequestedAction['payload']
): types.ChangeNetworkRequestedAction {
return {
type: CONFIG_NETWORKS.CHANGE_NETWORK_REQUESTED,
type: types.ConfigNetworksActions.CHANGE_NETWORK_REQUESTED,
payload
};
}

View File

@ -1,7 +1,14 @@
export * from './types';
export * from './actions';
export * from './reducer';
export * from './selectors';
import * as configNetworksTypes from './types';
import * as configNetworksActions from './actions';
import * as configNetworksReducer from './reducer';
import * as configNetworksSelectors from './selectors';
export {
configNetworksTypes,
configNetworksActions,
configNetworksReducer,
configNetworksSelectors
};
export * from './custom';
export * from './static';

View File

@ -1,10 +1,10 @@
import { combineReducers } from 'redux';
import { customNetworksReducer } from './custom/reducer';
import { staticNetworksReducer } from './static/reducer';
import { NetworksState } from './types';
import * as configNetworksCustomReducer from './custom/reducer';
import * as configStaticCustomReducer from './static/reducer';
import * as types from './types';
export const networksReducer = combineReducers<NetworksState>({
customNetworks: customNetworksReducer,
staticNetworks: staticNetworksReducer
export const networksReducer = combineReducers<types.NetworksState>({
customNetworks: configNetworksCustomReducer.customNetworksReducer,
staticNetworks: configStaticCustomReducer.staticNetworksReducer
});

View File

@ -1,7 +1,7 @@
import { StaticNetworkIds } from 'types/network';
import { AppState } from 'features/reducers';
import { getCustomNetworkConfigs } from './custom/selectors';
import { isStaticNetworkId, getStaticNetworkConfigs } from './static/selectors';
import * as configMetaNetworksCustomSelectors from './custom/selectors';
import * as configMetaNetworksStaticSelectors from './static/selectors';
const getConfig = (state: AppState) => state.config;
@ -11,14 +11,18 @@ export const getStaticNetworkIds = (state: AppState): StaticNetworkIds[] =>
Object.keys(getNetworks(state).staticNetworks) as StaticNetworkIds[];
export const getNetworkConfigById = (state: AppState, networkId: string) =>
isStaticNetworkId(state, networkId)
? getStaticNetworkConfigs(state)[networkId]
: getCustomNetworkConfigs(state)[networkId];
configMetaNetworksStaticSelectors.isStaticNetworkId(state, networkId)
? configMetaNetworksStaticSelectors.getStaticNetworkConfigs(state)[networkId]
: configMetaNetworksCustomSelectors.getCustomNetworkConfigs(state)[networkId];
export const getNetworkByChainId = (state: AppState, chainId: number | string) => {
const network =
Object.values(getStaticNetworkConfigs(state)).find(n => +n.chainId === +chainId) ||
Object.values(getCustomNetworkConfigs(state)).find(n => +n.chainId === +chainId);
Object.values(configMetaNetworksStaticSelectors.getStaticNetworkConfigs(state)).find(
n => +n.chainId === +chainId
) ||
Object.values(configMetaNetworksCustomSelectors.getCustomNetworkConfigs(state)).find(
n => +n.chainId === +chainId
);
if (!network) {
return null;
}

View File

@ -4,7 +4,7 @@ import { EXTRA_PATHS } from 'config/dpaths';
import { stripWeb3Network } from 'libs/nodes';
import { StaticNetworkIds } from 'types/network';
import { AppState } from 'features/reducers';
import { PathType } from './types';
import * as types from './types';
function getNetworks(state: AppState) {
return state.config.networks;
@ -21,7 +21,7 @@ export function isStaticNetworkId(
return Object.keys(getStaticNetworkConfigs(state)).includes(stripWeb3Network(networkId));
}
export function getPaths(state: AppState, pathType: PathType): DPath[] {
export function getPaths(state: AppState, pathType: types.PathType): DPath[] {
const paths = Object.values(getStaticNetworkConfigs(state))
.reduce((networkPaths: DPath[], { dPathFormats }): DPath[] => {
if (dPathFormats && dPathFormats[pathType]) {

View File

@ -1,16 +1,16 @@
import { CustomNetworksState } from './custom/types';
import { StaticNetworksState } from './static/types';
import * as configNetworksCustomTypes from './custom/types';
import * as configNetworksStaticTypes from './static/types';
export enum CONFIG_NETWORKS {
export enum ConfigNetworksActions {
CHANGE_NETWORK_REQUESTED = 'CONFIG_NETWORK_CHANGE_NETWORK_REQUESTED'
}
export interface NetworksState {
customNetworks: CustomNetworksState;
staticNetworks: StaticNetworksState;
customNetworks: configNetworksCustomTypes.CustomNetworksState;
staticNetworks: configNetworksStaticTypes.StaticNetworksState;
}
export interface ChangeNetworkRequestedAction {
type: CONFIG_NETWORKS.CHANGE_NETWORK_REQUESTED;
type: ConfigNetworksActions.CHANGE_NETWORK_REQUESTED;
payload: string;
}