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

Open external URLs on openUrl event - Closes #787 #788

Merged
merged 6 commits into from
Oct 2, 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
29 changes: 4 additions & 25 deletions src/components/dialog/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,15 @@ import AppBar from 'react-toolbox/lib/app_bar';
import { IconButton } from 'react-toolbox/lib/button';
import styles from './dialog.css';
import getDialogs from './dialogs';
import routesReg from '../../utils/routes';

class DialogElement extends Component {
constructor() {
super();
this.state = {};
this.routesReg = [
{
regex: /\/main\/transactions(?:\/[^/]*)?$/,
path: '/main/transactions/',
params: 'dialog',
name: 'transactions',
}, {
regex: /\/main\/voting(?:\/[^/]*)?$/,
path: '/main/voting/',
params: 'dialog',
name: 'voting',
}, {
regex: /\/main\/forging(?:\/[^/]*)?$/,
path: '/main/forging/',
params: 'dialog',
name: 'forging',
}, {
regex: /\/(\w+)?$/,
path: '/',
params: 'dialog',
name: 'login',
},
];
this.current = {
pathname: '/',
reg: this.routesReg[3],
reg: routesReg[3],
list: [],
dialog: '',
};
Expand All @@ -50,8 +28,9 @@ class DialogElement extends Component {
}

checkForDialog() {
// if the dialog is wrong, show a toast
if (this.current.pathname !== this.props.history.location.pathname) {
this.current.reg = this.routesReg.find(item =>
this.current.reg = routesReg.find(item =>
item.regex.test(this.props.history.location.pathname));
this.current.pathname = this.props.history.location.pathname;
const dialogName = this.props.history.location.pathname.replace(this.current.reg.path, '');
Expand Down
2 changes: 2 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import App from './components/app';
import store from './store';
import i18n from './i18n'; // initialized i18next instance
import proxyLogin from './utils/proxyLogin';
import externalLinks from './utils/externalLinks';
import env from './constants/env';

if (env.production) {
proxyLogin.init();
externalLinks.init();
}

const rootElement = document.getElementById('app');
Expand Down
23 changes: 23 additions & 0 deletions src/utils/externalLinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import history from '../history';
import routesReg from './routes';
import { errorToastDisplayed } from '../actions/toaster';
import store from '../store';

export default {
init: () => {
const { ipc } = window;

if (ipc) {
ipc.on('openUrl', (action, url) => {
const normalizedUrl = url.toLowerCase().replace('lisk://', '/');
const route = routesReg.find(item => item.regex.test(normalizedUrl));
if (route !== undefined) {
history.push(normalizedUrl);
} else {
store.dispatch(errorToastDisplayed({ label: 'The URL was invalid' }));
}
});
}
},
};

26 changes: 26 additions & 0 deletions src/utils/externalLinks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from 'chai';
import { spy } from 'sinon';
import externalLinks from './externalLinks';

describe('externalLinks', () => {
const ipc = {
on: spy(),
};

describe('init', () => {
it('should be a function', () => {
expect(typeof externalLinks.init).to.be.equal('function');
});

it('calling init when ipc is not on window should do nothing', () => {
externalLinks.init();
expect(ipc.on).to.not.have.been.calledWith();
});

it('calling init when ipc is available on window should bind listeners', () => {
window.ipc = ipc;
externalLinks.init();
expect(ipc.on).to.have.been.calledWith();
});
});
});
28 changes: 28 additions & 0 deletions src/utils/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default [
{
regex: /\/main\/transactions(?:\/[^/]*)?$/,
path: '/main/transactions/',
params: 'dialog',
name: 'transactions',
}, {
regex: /\/main\/voting(?:\/[^/]*)?$/,
path: '/main/voting/',
params: 'dialog',
name: 'voting',
}, {
regex: /\/main\/forging(?:\/[^/]*)?$/,
path: '/main/forging/',
params: 'dialog',
name: 'forging',
}, {
regex: /register(\/)?$/,
path: '/',
params: 'dialog',
name: 'login',
}, {
regex: /^\/$/,
path: '/',
params: 'dialog',
name: 'login',
},
];