diff --git a/src/components/login/login.js b/src/components/login/login.js index 6bcc4bc85..7cefc0f40 100644 --- a/src/components/login/login.js +++ b/src/components/login/login.js @@ -4,11 +4,11 @@ import Input from 'react-toolbox/lib/input'; import Dropdown from 'react-toolbox/lib/dropdown'; import Button from 'react-toolbox/lib/button'; import i18next from 'i18next'; -import getNetworks from './networks'; import PassphraseInput from '../passphraseInput'; import styles from './login.css'; import env from '../../constants/env'; import networks from '../../constants/networks'; +import getNetwork from '../../utils/getNetwork'; import LanguageDropdown from '../languageDropdown'; import RelativeLink from '../relativeLink'; import { validateUrl, getLoginData } from '../../utils/login'; @@ -24,7 +24,7 @@ class Login extends React.Component { this.state = { passphrase: '', address: '', - network: networks.mainnet, + network: networks.mainnet.code, }; this.validators = { @@ -43,8 +43,8 @@ class Login extends React.Component { } getNetworksList() { - this.networks = getNetworks().map((network, index) => ({ - label: i18next.t(network.name), + this.networks = Object.keys(networks).map((network, index) => ({ + label: i18next.t(networks[network].name), value: index, })); } @@ -64,8 +64,8 @@ class Login extends React.Component { } onLoginSubmission(passphrase) { - const network = Object.assign({}, getNetworks()[this.state.network]); - if (this.state.network === networks.customNode) { + const network = Object.assign({}, getNetwork(this.state.network)); + if (this.state.network === networks.customNode.code) { network.address = this.state.address; } @@ -129,8 +129,8 @@ class Login extends React.Component { const { savedAccounts } = this.props; if (savedAccounts && savedAccounts.length > 0 && !this.props.account.afterLogout) { this.account = savedAccounts[0]; - const network = Object.assign({}, getNetworks()[this.account.network]); - if (this.account.network === networks.customNode) { + const network = Object.assign({}, getNetwork(this.state.network)); + if (this.account.network === networks.customNode.code) { network.address = this.account.address; } @@ -161,7 +161,7 @@ class Login extends React.Component { className={`${styles.network} network`} /> { - this.state.network === networks.customNode && + this.state.network === networks.customNode.code && diff --git a/src/components/login/networks.js b/src/components/login/networks.js deleted file mode 100644 index 1ee4b8113..000000000 --- a/src/components/login/networks.js +++ /dev/null @@ -1,16 +0,0 @@ -export default () => ([ - {// network name translation t('Mainnet'); - name: 'Mainnet', - ssl: true, - port: 443, - }, {// network name translation t('Testnet'); - name: 'Testnet', - testnet: true, - ssl: true, - port: 443, - }, {// network name translation t('Custom Node'); - name: 'Custom Node', - custom: true, - address: 'http://localhost:4000', - }, -]); diff --git a/src/components/register/register.js b/src/components/register/register.js index 55d64f5db..7dec52cf6 100644 --- a/src/components/register/register.js +++ b/src/components/register/register.js @@ -1,7 +1,7 @@ import React from 'react'; import Passphrase from '../passphrase'; import networks from '../../constants/networks'; -import getNetworks from '../login/networks'; +import getNetwork from '../../utils/getNetwork'; import { validateUrl, getLoginData } from '../../utils/login'; const Register = ({ @@ -12,12 +12,14 @@ const Register = ({ let index = networkIndex; - if (!index || (index === networks.customNode && validateUrl(address).addressValidity !== '')) { - index = networks.mainnet; + // if (!index || (index === networksCode.customNode && + // validateUrl(address).addressValidity !== '')) { + if (!index || (index === networks.customNode.code && validateUrl(address).addressValidity !== '')) { + index = networks.mainnet.code; } - const network = Object.assign({}, getNetworks()[index]); - if (index === networks.customNode) { network.address = address; } + const network = Object.assign({}, getNetwork(index)); + if (index === networks.customNode.code) { network.address = address; } // set active peer activePeerSet({ diff --git a/src/components/register/register.test.js b/src/components/register/register.test.js index 69e3dc42d..04b4cccc3 100644 --- a/src/components/register/register.test.js +++ b/src/components/register/register.test.js @@ -7,7 +7,6 @@ import configureMockStore from 'redux-mock-store'; import i18n from '../../i18n'; import networks from '../../constants/networks'; import Register from './register'; -import getNetworks from '../login/networks'; import * as Utils from '../../utils/login'; @@ -60,11 +59,11 @@ describe('Register', () => { it('should call activePeerSet if props.onPassGenerated is called', () => { const props = wrapper.find('Passphrase').props(); - loginData.returns({ address: 'some address', networkIndex: networks.mainnet }); - + loginData.returns({ address: 'some address', networkIndex: networks.mainnet.code }); props.onPassGenerated('sample passphrase'); + expect(prop.activePeerSet).to.have.been.calledWith({ - network: getNetworks()[networks.mainnet], + network: networks.mainnet, passphrase: 'sample passphrase', }); }); @@ -72,13 +71,13 @@ describe('Register', () => { it('should call activePeerSet with testnet if network index is testnet', () => { const props = wrapper.find('Passphrase').props(); - loginData.returns({ address: 'invalid address', networkIndex: networks.testnet }); + loginData.returns({ address: 'invalid address', networkIndex: networks.testnet.code }); props.onPassGenerated('sample passphrase'); expect(loginData).to.have.been.calledWith(); expect(prop.activePeerSet).to.have.been.calledWith({ - network: getNetworks()[networks.testnet], + network: networks.testnet, passphrase: 'sample passphrase', }); }); @@ -86,13 +85,13 @@ describe('Register', () => { it('should call activePeerSet with mainnet if network index is custom node and address is invalid', () => { const props = wrapper.find('Passphrase').props(); - loginData.returns({ address: 'invalid address', networkIndex: networks.customNode }); + loginData.returns({ address: 'invalid address', networkIndex: networks.customNode.code }); props.onPassGenerated('sample passphrase'); expect(loginData).to.have.been.calledWith(); expect(prop.activePeerSet).to.have.been.calledWith({ - network: getNetworks()[networks.mainnet], + network: networks.mainnet, passphrase: 'sample passphrase', }); }); @@ -100,12 +99,12 @@ describe('Register', () => { it('should call activePeerSet with custom node if network index is custom node and address is valid', () => { const props = wrapper.find('Passphrase').props(); - loginData.returns({ address: '127.0.0.1:8080', networkIndex: networks.customNode }); + loginData.returns({ address: '127.0.0.1:8080', networkIndex: networks.customNode.code }); props.onPassGenerated('sample passphrase'); expect(loginData).to.have.been.calledWith(); - const network = getNetworks()[networks.customNode]; + const network = networks.customNode; network.address = '127.0.0.1:8080'; expect(prop.activePeerSet).to.have.been.calledWith({ network, diff --git a/src/components/saveAccount/saveAccount.js b/src/components/saveAccount/saveAccount.js index 817db3b69..da86a6540 100644 --- a/src/components/saveAccount/saveAccount.js +++ b/src/components/saveAccount/saveAccount.js @@ -1,7 +1,7 @@ import React from 'react'; import InfoParagraph from '../infoParagraph'; import ActionBar from '../actionBar'; -import getNetworks from '../login/networks'; +import networks from '../../constants/networks'; const SaveAccount = ({ network, @@ -13,8 +13,8 @@ const SaveAccount = ({ }) => { const save = () => { // eslint-disable-next-line arrow-body-style - const index = getNetworks().map((item, i) => { - return (item.name === network) ? i : null; + const index = Object.keys(networks).map((item, i) => { + return (networks[item].name === network) ? i : null; }).find(item => item !== null); accountSaved({ network: index, diff --git a/src/constants/networks.js b/src/constants/networks.js index cba7395de..39cbf0e08 100644 --- a/src/constants/networks.js +++ b/src/constants/networks.js @@ -1,7 +1,21 @@ -const networks = { - mainnet: 0, - testnet: 1, - customNode: 2, +export default { + mainnet: { // network name translation t('Mainnet'); + name: 'Mainnet', + ssl: true, + port: 443, + code: 0, + }, + testnet: { // network name translation t('Testnet'); + name: 'Testnet', + testnet: true, + ssl: true, + port: 443, + code: 1, + }, + customNode: { // network name translation t('Custom Node'); + name: 'Custom Node', + custom: true, + address: 'http://localhost:4000', + code: 2, + }, }; - -export default networks; diff --git a/src/utils/getNetwork.js b/src/utils/getNetwork.js new file mode 100644 index 000000000..745a28945 --- /dev/null +++ b/src/utils/getNetwork.js @@ -0,0 +1,11 @@ +import networks from '../constants/networks'; + +export default (code) => { + let network; + Object.keys(networks).forEach((key) => { + if (networks[key].code === code) { + network = networks[key]; + } + }, this); + return network; +};