Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Move networks.js to src/constants - Closes #926 #945

Merged
merged 13 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/components/login/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -24,7 +24,7 @@ class Login extends React.Component {
this.state = {
passphrase: '',
address: '',
network: networks.mainnet,
network: networks.mainnet.code,
};

this.validators = {
Expand All @@ -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,
}));
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -161,7 +161,7 @@ class Login extends React.Component {
className={`${styles.network} network`}
/>
{
this.state.network === networks.customNode &&
this.state.network === networks.customNode.code &&
<Input type='text'
label={this.props.t('Node address')}
name='address'
Expand All @@ -186,7 +186,7 @@ class Login extends React.Component {
<Button label={this.props.t('Login')} primary raised
className='login-button'
type='submit'
disabled={(this.state.network === networks.customNode && this.state.addressValidity !== '') ||
disabled={(this.state.network === networks.customNode.code && this.state.addressValidity !== '') ||
this.state.passphraseValidity !== ''} />
</div>
</footer>
Expand Down
16 changes: 0 additions & 16 deletions src/components/login/networks.js

This file was deleted.

12 changes: 7 additions & 5 deletions src/components/register/register.js
Original file line number Diff line number Diff line change
@@ -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 = ({
Expand All @@ -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({
Expand Down
19 changes: 9 additions & 10 deletions src/components/register/register.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand Down Expand Up @@ -60,52 +59,52 @@ 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',
});
});

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',
});
});

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',
});
});

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,
Expand Down
6 changes: 3 additions & 3 deletions src/components/saveAccount/saveAccount.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
26 changes: 20 additions & 6 deletions src/constants/networks.js
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 11 additions & 0 deletions src/utils/getNetwork.js
Original file line number Diff line number Diff line change
@@ -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;
};