From 16888338cc22fb11f0365f73196d3f21e27c0445 Mon Sep 17 00:00:00 2001 From: VcTT Date: Tue, 11 Jun 2019 14:30:15 -0300 Subject: [PATCH] Refactor config.js (#2129) * refactor global config variables to use constants instead of strings * Add return to credentials when fatal error is in advanced mode * fix lint * review fixes --- app/actions/DaemonActions.js | 12 +- app/actions/SettingsActions.js | 2 +- app/components/views/FatalErrorPage.js | 58 ++++--- .../GetStartedPage/AdvancedStartup/index.js | 6 +- app/config.js | 144 +++++------------- app/connectors/fatalErrorPage.js | 2 + app/main_dev/configConstants.js | 62 ++++++++ app/reducers/daemon.js | 6 + 8 files changed, 160 insertions(+), 132 deletions(-) create mode 100644 app/main_dev/configConstants.js diff --git a/app/actions/DaemonActions.js b/app/actions/DaemonActions.js index 428a57f015..a4c06537e9 100644 --- a/app/actions/DaemonActions.js +++ b/app/actions/DaemonActions.js @@ -7,7 +7,7 @@ import { semverCompatible } from "./VersionActions"; import * as wallet from "wallet"; import { push as pushHistory, goBack } from "react-router-redux"; import { ipcRenderer } from "electron"; -import { setMustOpenForm, getWalletCfg, getAppdataPath, getRemoteCredentials, getGlobalCfg, setLastHeight } from "../config"; +import { setMustOpenForm, getWalletCfg, getAppdataPath, getRemoteCredentials, getGlobalCfg, setLastHeight, getDaemonIsAdvanced } from "../config"; import { isTestNet } from "selectors"; import axios from "axios"; import { STANDARD_EXTERNAL_REQUESTS } from "main_dev/externalRequests"; @@ -56,6 +56,7 @@ export const CONNECTDAEMON_SUCCESS = "CONNECTDAEMON_SUCCESS"; export const CONNECTDAEMON_FAILURE = "CONNECTDAEMON_FAILURE"; export const SYNC_DAEMON_ATTEMPT = "SYNC_DAEMON_ATTEMPT"; export const SYNC_DAEMON_FAILED = "SYNC_DAEMON_FAILED"; +export const BACK_TO_CREDENTIALS = "BACK_TO_CREDENTIALS"; export const checkDecreditonVersion = () => (dispatch, getState) =>{ const detectedVersion = getState().daemon.appVersion; @@ -157,12 +158,12 @@ export const startDaemon = (params) => (dispatch, getState) => { dispatch({ type: DAEMONSTART_ATTEMPT }); const { daemonStarted } = getState().daemon; if (daemonStarted) { - return dispatch({ type: DAEMONSTART_SUCCESS }); + return dispatch({ type: DAEMONSTART_SUCCESS, daemonAdvanced: getDaemonIsAdvanced() }); } wallet.startDaemon(params, isTestNet(getState())) .then(rpcCreds => { - dispatch({ type: DAEMONSTART_SUCCESS, credentials: rpcCreds, appdata }); + dispatch({ type: DAEMONSTART_SUCCESS, credentials: rpcCreds, appdata, daemonAdvanced: getDaemonIsAdvanced() }); dispatch(connectDaemon(rpcCreds, appdata)); }) .catch((err) => dispatch({ err, type: DAEMONSTART_FAILURE })); @@ -192,6 +193,11 @@ export const registerForErrors = () => (dispatch) => { }); }; +export const backToCredentials = () => (dispatch) => { + dispatch({ type: BACK_TO_CREDENTIALS }); + dispatch(pushHistory("/getstarted")); +}; + export const deleteDaemonData = () => (dispatch, getState) => { const { appData } = getState().daemon; dispatch({ type: DELETE_DCRD_ATTEMPT }); diff --git a/app/actions/SettingsActions.js b/app/actions/SettingsActions.js index 12af74cbbb..36e25c3a13 100644 --- a/app/actions/SettingsActions.js +++ b/app/actions/SettingsActions.js @@ -106,7 +106,7 @@ export function updateStateSettingsChanged(settings, norestart) { if (newDiffersFromTemp) { const newDiffersFromCurrent = settingsFields .reduce((d, f) => (d || newSettings[f] !== currentSettings[f]), false); - const needNetworkReset = !norestart && Object.keys(networkChange) + const needNetworkReset = !norestart && Object.keys(networkChange) .reduce((d, f) => (d || newSettings[f] !== currentSettings[f]), false); newDiffersFromCurrent ? dispatch({ tempSettings: newSettings, needNetworkReset, type: SETTINGS_CHANGED }) diff --git a/app/components/views/FatalErrorPage.js b/app/components/views/FatalErrorPage.js index 6b4e5af5fc..515efc4c87 100644 --- a/app/components/views/FatalErrorPage.js +++ b/app/components/views/FatalErrorPage.js @@ -12,8 +12,41 @@ const corruptedError = "corrupted"; const checkSumError = "checksum does not match"; class FatalErrorPage extends React.Component { + getErrorAction() { + const { daemonError, deleteDaemonData } = this.props; + let errorMessage; + + switch (true) { + case (daemonError.indexOf(resourcesUnavailableError) !== -1): + errorMessage = ; + break; + case (daemonError.indexOf(DIFF_CONNECTION_ERROR) !== -1): + errorMessage = ; + break; + case ((daemonError.indexOf(corruptedError) !== -1 || daemonError.indexOf(checkSumError) !== -1 )): + errorMessage = ( + <> +
+ +
+ } + modalContent={} + onSubmit={deleteDaemonData} + buttonLabel={ }/> + ); + break; + default: + errorMessage = ; + break; + } + + return errorMessage; + } + render() { - const { daemonError, walletError, shutdownApp, deleteDaemonData } = this.props; + const { daemonError, walletError, shutdownApp, isAdvancedDaemon, backToCredentials } = this.props; return (
@@ -38,26 +71,13 @@ class FatalErrorPage extends React.Component {
:
- {daemonError && daemonError.indexOf(resourcesUnavailableError) > 0 ? - : - daemonError && daemonError.indexOf(DIFF_CONNECTION_ERROR) !== -1 ? - : - daemonError && (daemonError.indexOf(corruptedError) > 0 || daemonError.indexOf(checkSumError) > 0) ? - <> -
- -
- } - modalContent={} - onSubmit={deleteDaemonData} - buttonLabel={ }/> - : - - } + {daemonError && this.getErrorAction()}
+ { isAdvancedDaemon && + + + } diff --git a/app/components/views/GetStartedPage/AdvancedStartup/index.js b/app/components/views/GetStartedPage/AdvancedStartup/index.js index 78d78ab934..ceaa389f23 100644 --- a/app/components/views/GetStartedPage/AdvancedStartup/index.js +++ b/app/components/views/GetStartedPage/AdvancedStartup/index.js @@ -137,11 +137,7 @@ class AdvancedStartupBody extends React.Component { return; } const { rpc_user, rpc_pass, rpc_cert, rpc_host, rpc_port } = this.state; - setRemoteCredentials("rpc_user", rpc_user); - setRemoteCredentials("rpc_pass", rpc_pass); - setRemoteCredentials("rpc_host", rpc_host); - setRemoteCredentials("rpc_port", rpc_port); - setRemoteCredentials("rpc_cert", rpc_cert); + setRemoteCredentials(rpc_user, rpc_pass, rpc_cert, rpc_host, rpc_port); let args = { rpc_user, rpc_pass, rpc_cert, rpc_host, rpc_port }; this.props.onStartDaemon({ rpcCreds: args }); } diff --git a/app/config.js b/app/config.js index 96eac35715..1128e61ade 100644 --- a/app/config.js +++ b/app/config.js @@ -3,6 +3,7 @@ import Store from "electron-store"; import ini from "ini"; import { stakePoolInfo } from "./middleware/stakepoolapi"; import { appDataDirectory, getGlobalCfgPath, dcrdCfg, getWalletPath, dcrwalletCfg, getDcrdRpcCert, getDcrdPath } from "./main_dev/paths"; +import * as cfgConstants from "./main_dev/configConstants.js"; export function getGlobalCfg() { const config = new Store(); @@ -76,90 +77,24 @@ function cleanWalletCfg(config) { export function initGlobalCfg() { const config = new Store(); - if (!config.has("theme")) { - config.set("theme", "theme-light"); - } - if (!config.has("daemon_start_advanced")) { - config.set("daemon_start_advanced", false); - } - if (!config.has("must_open_form")) { - config.set("must_open_form",true); - } - if (!config.has("locale")) { - config.set("locale",""); - } - if (!config.has("network")) { - config.set("network","mainnet"); - } - if (!config.has("set_language")) { - config.set("set_language","true"); - } - if (!config.has("ui_animations")) { - config.set("ui_animations", true); - } - if (!config.has("show_tutorial")) { - config.set("show_tutorial","true"); - } - if (!config.has("show_privacy")) { - config.set("show_privacy", true); - } - if (!config.has("show_spvchoice")) { - config.set("show_spvchoice", true); - } - if (!config.has("allowed_external_requests")) { - config.set("allowed_external_requests", []); - } - if (!config.has("proxy_type")) { - config.set("proxy_type", null); - } - if (!config.has("proxy_location")) { - config.set("proxy_location", null); - } - if (!config.has("remote_credentials")) { - const credentialKeys = { - rpc_user : "", - rpc_pass : "", - rpc_cert : "", - rpc_host : "", - rpc_port : "", - }; - config.set("remote_credentials",credentialKeys); - } - if (!config.has("appdata_path")) { - config.set("appdata_path", ""); - } - if (!config.has("spv_mode")) { - config.set("spv_mode", false); - } - if (!config.has("spv_connect")) { - config.set("spv_connect", []); - } - if (!config.has("max_wallet_count")) { - config.set("max_wallet_count", 3); - } - if (!config.has("timezone")) { - config.set("timezone", "local"); - } - if (!config.has("disable_hardware_accel")) { - config.set("disable_hardware_accel", false); - } + Object.keys(cfgConstants.INITIAL_VALUES).map(key => { + if (!config.has(key)) { + config.set(key, cfgConstants.INITIAL_VALUES[key]); + } + }); cleanGlobalCfg(config); return(config); } function cleanGlobalCfg(config) { - var key; - const globalCfgFields = [ "theme", "daemon_start_advanced", "must_open_form", - "locale", "network", "set_language", "ui_animations", "show_spvchoice", - "show_tutorial", "show_privacy", "allowed_external_requests", "proxy_type", - "proxy_location", "remote_credentials", "spv_mode", "spv_connect", - "max_wallet_count", "timezone", "last_height", "appdata_path", - "disable_hardware_accel" ]; - for (key in config.store) { - var found = false; - for (var i = 0; i < globalCfgFields.length; i++) { - if (key == globalCfgFields[i]) { + const globalCfgFields = Object.keys(cfgConstants.INITIAL_VALUES); + + for (let key in config.store) { + let found = false; + for (let i = 0; i < globalCfgFields.length; i++) { + if (key === globalCfgFields[i]) { found = true; + break; } } if (!found) { @@ -169,7 +104,7 @@ function cleanGlobalCfg(config) { } export function validateGlobalCfgFile() { - var fileContents; + let fileContents; try { fileContents = fs.readFileSync(getGlobalCfgPath(), "utf8"); } @@ -301,45 +236,46 @@ export function updateStakePoolConfig(config, foundStakePoolConfigs) { } } -export function getAppdataPath() { +export function getConfigData(configKey) { const config = getGlobalCfg(); - return config.get("appdata_path"); + return config.get(configKey); } -export function setAppdataPath(appdataPath) { - const config = getGlobalCfg(); - const credentialKeys = { - rpc_user : "", - rpc_pass : "", - rpc_cert : "", - rpc_host : "", - rpc_port : "", - }; - config.set("remote_credentials",credentialKeys); - return config.set("appdata_path",appdataPath); +export function getRemoteCredentials() { + return getConfigData(cfgConstants.REMOTE_CREDENTIALS); } -export function getRemoteCredentials() { - const config = getGlobalCfg(); - return config.get("remote_credentials"); +export function getAppdataPath() { + return getConfigData(cfgConstants.APPDATA); +} + +export function getDaemonIsAdvanced() { + return getConfigData(cfgConstants.DAEMON_ADVANCED); } -export function setRemoteCredentials(key, value) { +export function setConfigData(key, value) { const config = getGlobalCfg(); - config.set("appdata_path",""); - let credentials = config.get("remote_credentials"); - credentials[key] = value; - return config.set("remote_credentials",credentials); + return config.set(key, value); +} + +export function setAppdataPath(appdataPath) { + const credentialKeys = cfgConstants.setDaemonRemoteCredentials("", "", "", "", ""); + setConfigData(cfgConstants.REMOTE_CREDENTIALS, credentialKeys); + return setConfigData(cfgConstants.APPDATA, appdataPath); +} + +export function setRemoteCredentials(rpcuser, rpcpass, rpccert, rpchost, rpcport) { + setConfigData(cfgConstants.APPDATA, ""); + const credentials = cfgConstants.setDaemonRemoteCredentials(rpcuser, rpcpass, rpccert, rpchost, rpcport); + return setConfigData(cfgConstants.REMOTE_CREDENTIALS, credentials); } export function setMustOpenForm(openForm) { - const config = getGlobalCfg(); - return config.set("must_open_form", openForm); + return setConfigData(cfgConstants.OPEN_FORM, openForm); } export function setLastHeight(height) { - const config = getGlobalCfg(); - return config.set("last_height", height); + return setConfigData(cfgConstants.LAST_HEIGHT, height); } function makeRandomString(length) { diff --git a/app/connectors/fatalErrorPage.js b/app/connectors/fatalErrorPage.js index 5ffd5b4bba..2f33e06c7d 100644 --- a/app/connectors/fatalErrorPage.js +++ b/app/connectors/fatalErrorPage.js @@ -7,11 +7,13 @@ import * as da from "../actions/DaemonActions"; const mapStateToProps = selectorMap({ daemonError: sel.daemonError, walletError: sel.walletError, + isAdvancedDaemon: sel.isAdvancedDaemon, }); const mapDispatchToProps = dispatch => bindActionCreators({ shutdownApp: da.shutdownApp, deleteDaemonData: da.deleteDaemonData, + backToCredentials: da.backToCredentials, }, dispatch); export default connect(mapStateToProps, mapDispatchToProps); diff --git a/app/main_dev/configConstants.js b/app/main_dev/configConstants.js new file mode 100644 index 0000000000..d5da041b8e --- /dev/null +++ b/app/main_dev/configConstants.js @@ -0,0 +1,62 @@ +export const DAEMON_ADVANCED = "daemon_start_advanced"; +export const THEME = "theme"; +export const OPEN_FORM = "must_open_form"; +export const LOCALE = "locale"; +export const NETWORK = "network"; +export const SET_LANGUAGE = "set_language"; +export const UI_ANIMATION = "ui_animations"; +export const SHOW_SPV_CHOICE = "show_spvchoice"; +export const SHOW_TUTORIAL = "show_tutorial"; +export const SHOW_PRIVACY = "show_privacy"; +export const ALLOW_EXTERNAL_REQUEST = "allowed_external_requests"; +export const PROXY_TYPE = "proxy_type"; +export const PROXY_LOCATION = "proxy_location"; +export const REMOTE_CREDENTIALS = "remote_credentials"; +export const SPV_MODE = "spv_mode"; +export const SPV_CONNECT = "spv_connect"; +export const MAX_WALLET_COUNT = "max_wallet_count"; +export const TIMEZONE = "timezone"; +export const LAST_HEIGHT = "last_height"; +export const APPDATA = "appdata_path"; +export const DISABLE_HARDWARE_ACCEL = "disable_hardware_accel"; + +export const RPCUSER = "rpc_user"; +export const RPCPASS = "rpc_pass"; +export const RPCCERT = "rpc_cert"; +export const RPCHOST = "rpc_host"; +export const RPCPORT = "rpc_port"; + +export const setDaemonRemoteCredentials = (rpcuser, rpcpass, rpccert, rpchost, rpcport) => { + return { + [RPCUSER]: rpcuser, + [RPCPASS]: rpcpass, + [RPCCERT]: rpccert, + [RPCHOST]: rpchost, + [RPCPORT]: rpcport, + }; +}; + +export const INITIAL_VALUES = { + [DAEMON_ADVANCED]: false, + [THEME]: "theme-light", + [OPEN_FORM]: true, + [LOCALE]: "", + [NETWORK]: "mainnet", + [SET_LANGUAGE]: true, + [UI_ANIMATION]: true, + [SHOW_SPV_CHOICE]: true, + [SHOW_TUTORIAL]: true, + [SHOW_PRIVACY]: true, + [ALLOW_EXTERNAL_REQUEST]: [], + [PROXY_TYPE]: null, + [PROXY_LOCATION]: null, + [REMOTE_CREDENTIALS]: {}, + [SPV_MODE]: false, + [SPV_CONNECT]: [], + [MAX_WALLET_COUNT]: 3, + [TIMEZONE]: "local", + [LAST_HEIGHT]: 0, + [APPDATA]: "", + [DISABLE_HARDWARE_ACCEL]: false, +}; + diff --git a/app/reducers/daemon.js b/app/reducers/daemon.js index e8ed3be568..327219544f 100644 --- a/app/reducers/daemon.js +++ b/app/reducers/daemon.js @@ -22,6 +22,7 @@ import { DAEMON_WARNING, WALLET_WARNING, CLOSEDAEMON_ATTEMPT, CLOSEDAEMON_FAILED, CLOSEDAEMON_SUCCESS, CHECK_NETWORKMATCH_ATTEMPT, CHECK_NETWORKMATCH_SUCCESS, CHECK_NETWORKMATCH_FAILED, + BACK_TO_CREDENTIALS, } from "../actions/DaemonActions"; import { CREATEWALLET_GOBACK, @@ -105,6 +106,11 @@ export default function version(state = {}, action) { timeStart: null, blockStart: null, }; + case BACK_TO_CREDENTIALS: + return { + ...state, + daemonStarted: false, + }; case DAEMONSYNCING_START: return { ...state, daemonStarted: true,