diff --git a/src/components/dialog/dialog.js b/src/components/dialog/dialog.js index 65adec27b..8f73bca06 100644 --- a/src/components/dialog/dialog.js +++ b/src/components/dialog/dialog.js @@ -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 dialogs 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: '', }; @@ -50,15 +28,26 @@ 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 => + const 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, ''); - if (dialogs[dialogName] !== undefined) { - this.open(this.current.reg, dialogs[dialogName]); + console.log('opening', reg, this.current.pathname, this.props.history.location.pathname); + // wrong route, get back + if (reg === undefined) { + this.goBack(); } else { - this.close(); + this.current.reg = reg; + this.current.pathname = this.props.history.location.pathname; + const dialogName = this.props.history.location.pathname.replace(this.current.reg.path, ''); + + // route has a dialog, open it + if (dialogs[dialogName] !== undefined) { + this.open(this.current.reg, dialogs[dialogName]); + // route doesn't have a dialog, close if already open + } else { + this.close(); + } } } } @@ -94,6 +83,9 @@ class DialogElement extends Component { goBack() { this.props.history.push(this.current.reg.path); + setTimeout(() => { + console.log('closed', this.current.reg.path); + }, 5); } render() { diff --git a/src/utils/externalLinks.js b/src/utils/externalLinks.js index 82850416b..57778eb32 100644 --- a/src/utils/externalLinks.js +++ b/src/utils/externalLinks.js @@ -1,13 +1,21 @@ import history from '../history'; +import routesReg from './routes'; +import { errorToastDisplayed } from '../actions/toaster'; +import store from '../store'; export default { init: () => { const { ipc } = window; - const protocolReg = /[lL][iI][sS][kK]:\/\//; if (ipc) { ipc.on('openUrl', (action, url) => { - history.push(url.replace(protocolReg, '/')); + 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' })); + } }); } }, diff --git a/src/utils/routes.js b/src/utils/routes.js new file mode 100644 index 000000000..582e6bef1 --- /dev/null +++ b/src/utils/routes.js @@ -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', + }, +];