2017-09-25 02:06:28 +00:00
|
|
|
import {
|
|
|
|
ChangeGasPriceAction,
|
2017-07-20 17:06:10 +00:00
|
|
|
ChangeLanguageAction,
|
2017-09-25 02:06:28 +00:00
|
|
|
ChangeNodeAction,
|
2017-11-18 20:33:53 +00:00
|
|
|
AddCustomNodeAction,
|
|
|
|
RemoveCustomNodeAction,
|
|
|
|
SetLatestBlockAction,
|
|
|
|
ConfigAction,
|
2017-07-04 03:21:19 +00:00
|
|
|
} from 'actions/config';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { TypeKeys } from 'actions/config/constants';
|
2017-11-18 20:33:53 +00:00
|
|
|
import {
|
|
|
|
NODES,
|
|
|
|
NodeConfig,
|
|
|
|
CustomNodeConfig,
|
|
|
|
} from '../config/data';
|
|
|
|
import { makeCustomNodeId } from 'utils/node';
|
2017-04-14 06:25:01 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export interface State {
|
2017-07-02 05:49:06 +00:00
|
|
|
// FIXME
|
2017-09-25 02:06:28 +00:00
|
|
|
languageSelection: string;
|
|
|
|
nodeSelection: string;
|
2017-11-18 20:33:53 +00:00
|
|
|
node: NodeConfig;
|
|
|
|
isChangingNode: boolean;
|
2017-09-25 02:06:28 +00:00
|
|
|
gasPriceGwei: number;
|
2017-10-11 05:04:49 +00:00
|
|
|
offline: boolean;
|
|
|
|
forceOffline: boolean;
|
2017-11-18 20:33:53 +00:00
|
|
|
customNodes: CustomNodeConfig[];
|
|
|
|
latestBlock: string;
|
2017-09-25 02:06:28 +00:00
|
|
|
}
|
2017-04-14 06:25:01 +00:00
|
|
|
|
2017-11-18 20:33:53 +00:00
|
|
|
const defaultNode = 'eth_mew';
|
2017-07-27 17:05:09 +00:00
|
|
|
export const INITIAL_STATE: State = {
|
2017-09-26 23:03:38 +00:00
|
|
|
languageSelection: 'en',
|
2017-11-18 20:33:53 +00:00
|
|
|
nodeSelection: defaultNode,
|
|
|
|
node: NODES[defaultNode],
|
|
|
|
isChangingNode: false,
|
2017-10-11 05:04:49 +00:00
|
|
|
gasPriceGwei: 21,
|
|
|
|
offline: false,
|
2017-11-18 20:33:53 +00:00
|
|
|
forceOffline: false,
|
|
|
|
customNodes: [],
|
|
|
|
latestBlock: "???",
|
2017-07-02 05:49:06 +00:00
|
|
|
};
|
2017-04-14 06:25:01 +00:00
|
|
|
|
2017-07-04 01:25:01 +00:00
|
|
|
function changeLanguage(state: State, action: ChangeLanguageAction): State {
|
2017-07-04 03:21:19 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2017-10-11 05:04:49 +00:00
|
|
|
languageSelection: action.payload
|
2017-07-04 03:21:19 +00:00
|
|
|
};
|
2017-06-26 22:27:55 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 01:25:01 +00:00
|
|
|
function changeNode(state: State, action: ChangeNodeAction): State {
|
2017-07-04 03:21:19 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2017-11-18 20:33:53 +00:00
|
|
|
nodeSelection: action.payload.nodeSelection,
|
|
|
|
node: action.payload.node,
|
|
|
|
isChangingNode: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function changeNodeIntent(state: State): State {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isChangingNode: true,
|
2017-07-04 03:21:19 +00:00
|
|
|
};
|
2017-04-14 06:25:01 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 17:06:10 +00:00
|
|
|
function changeGasPrice(state: State, action: ChangeGasPriceAction): State {
|
|
|
|
return {
|
|
|
|
...state,
|
2017-10-11 05:04:49 +00:00
|
|
|
gasPriceGwei: action.payload
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-19 02:29:49 +00:00
|
|
|
function toggleOffline(state: State): State {
|
2017-10-11 05:04:49 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
offline: !state.offline
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-19 02:29:49 +00:00
|
|
|
function forceOffline(state: State): State {
|
2017-10-11 05:04:49 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
forceOffline: !state.forceOffline
|
2017-07-20 17:06:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-18 20:33:53 +00:00
|
|
|
function addCustomNode(state: State, action: AddCustomNodeAction): State {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
customNodes: [
|
|
|
|
...state.customNodes,
|
|
|
|
action.payload,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeCustomNode(state: State, action: RemoveCustomNodeAction): State {
|
|
|
|
const id = makeCustomNodeId(action.payload);
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
customNodes: state.customNodes.filter((cn) => cn !== action.payload),
|
|
|
|
nodeSelection: id === state.nodeSelection ?
|
|
|
|
defaultNode : state.nodeSelection,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function setLatestBlock(state: State, action: SetLatestBlockAction): State {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
latestBlock: action.payload,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-04 03:21:19 +00:00
|
|
|
export function config(
|
2017-07-27 17:05:09 +00:00
|
|
|
state: State = INITIAL_STATE,
|
2017-07-04 03:21:19 +00:00
|
|
|
action: ConfigAction
|
|
|
|
): State {
|
2017-07-02 05:49:06 +00:00
|
|
|
switch (action.type) {
|
2017-09-25 02:06:28 +00:00
|
|
|
case TypeKeys.CONFIG_LANGUAGE_CHANGE:
|
2017-07-04 03:21:19 +00:00
|
|
|
return changeLanguage(state, action);
|
2017-09-25 02:06:28 +00:00
|
|
|
case TypeKeys.CONFIG_NODE_CHANGE:
|
2017-07-04 03:21:19 +00:00
|
|
|
return changeNode(state, action);
|
2017-11-18 20:33:53 +00:00
|
|
|
case TypeKeys.CONFIG_NODE_CHANGE_INTENT:
|
|
|
|
return changeNodeIntent(state);
|
2017-09-25 02:06:28 +00:00
|
|
|
case TypeKeys.CONFIG_GAS_PRICE:
|
2017-07-20 17:06:10 +00:00
|
|
|
return changeGasPrice(state, action);
|
2017-10-11 05:04:49 +00:00
|
|
|
case TypeKeys.CONFIG_TOGGLE_OFFLINE:
|
2017-10-19 02:29:49 +00:00
|
|
|
return toggleOffline(state);
|
2017-10-11 05:04:49 +00:00
|
|
|
case TypeKeys.CONFIG_FORCE_OFFLINE:
|
2017-10-19 02:29:49 +00:00
|
|
|
return forceOffline(state);
|
2017-11-18 20:33:53 +00:00
|
|
|
case TypeKeys.CONFIG_ADD_CUSTOM_NODE:
|
|
|
|
return addCustomNode(state, action);
|
|
|
|
case TypeKeys.CONFIG_REMOVE_CUSTOM_NODE:
|
|
|
|
return removeCustomNode(state, action);
|
|
|
|
case TypeKeys.CONFIG_SET_LATEST_BLOCK:
|
|
|
|
return setLatestBlock(state, action);
|
2017-07-02 05:49:06 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2017-04-14 06:25:01 +00:00
|
|
|
}
|