summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2018-09-07 20:05:03 +0300
committerAndrej Mihajlov <and@mullvad.net>2018-09-07 20:05:03 +0300
commitac9d0098d5fcb2d710620af476e972f8d11318ec (patch)
treead484b44427ccaaa4022b87987d2c003a1489ca0
parentbb945c76a710ecc1416531b59116dbba0483a306 (diff)
parent2f9c45e264137dc1e5689d1590f40a421973aee2 (diff)
downloadmullvadvpn-ac9d0098d5fcb2d710620af476e972f8d11318ec.tar.xz
mullvadvpn-ac9d0098d5fcb2d710620af476e972f8d11318ec.zip
Merge branch 'fix-routing'
-rw-r--r--gui/packages/desktop/src/renderer/app.js38
-rw-r--r--gui/packages/desktop/src/renderer/redux/daemon/actions.js24
-rw-r--r--gui/packages/desktop/src/renderer/redux/daemon/reducers.js33
-rw-r--r--gui/packages/desktop/src/renderer/redux/store.js8
-rw-r--r--gui/packages/desktop/src/renderer/routes.js121
5 files changed, 45 insertions, 179 deletions
diff --git a/gui/packages/desktop/src/renderer/app.js b/gui/packages/desktop/src/renderer/app.js
index 3384a6d3c8..ebc32b9f81 100644
--- a/gui/packages/desktop/src/renderer/app.js
+++ b/gui/packages/desktop/src/renderer/app.js
@@ -24,7 +24,6 @@ import accountActions from './redux/account/actions';
import connectionActions from './redux/connection/actions';
import settingsActions from './redux/settings/actions';
import versionActions from './redux/version/actions';
-import daemonActions from './redux/daemon/actions';
import windowActions from './redux/window/actions';
import type { WindowShapeParameters } from '../main/window-controller';
@@ -59,7 +58,6 @@ export default class AppRenderer {
connection: bindActionCreators(connectionActions, dispatch),
settings: bindActionCreators(settingsActions, dispatch),
version: bindActionCreators(versionActions, dispatch),
- daemon: bindActionCreators(daemonActions, dispatch),
window: bindActionCreators(windowActions, dispatch),
history: bindActionCreators(
{
@@ -116,9 +114,7 @@ export default class AppRenderer {
renderView() {
return (
<Provider store={this._reduxStore}>
- <ConnectedRouter history={this._memoryHistory}>
- {makeRoutes(this._reduxStore.getState, { app: this })}
- </ConnectedRouter>
+ <ConnectedRouter history={this._memoryHistory}>{makeRoutes({ app: this })}</ConnectedRouter>
</Provider>
);
}
@@ -182,6 +178,14 @@ export default class AppRenderer {
// take user to main view if user is still at launch screen `/`
if (history.location.pathname === '/') {
actions.history.replace('/connect');
+ } else {
+ // TODO: Reinvent the navigation back in history to make sure that user does not end up on
+ // the restricted screen due to changes in daemon's state.
+ for (const entry of history.entries) {
+ if (entry.pathname === '/') {
+ entry.pathname = '/connect';
+ }
+ }
}
} else {
log.debug('No account set, showing login view.');
@@ -192,6 +196,14 @@ export default class AppRenderer {
// take user to `/login` screen if user is at launch screen `/`
if (history.location.pathname === '/') {
actions.history.replace('/login');
+ } else {
+ // TODO: Reinvent the navigation back in history to make sure that user does not end up on
+ // the restricted screen due to changes in daemon's state.
+ for (const entry of history.entries) {
+ if (!entry.pathname.startsWith('/settings')) {
+ entry.pathname = '/login';
+ }
+ }
}
}
}
@@ -427,9 +439,6 @@ export default class AppRenderer {
}
async _onOpenConnection() {
- // save to redux that the app connected to daemon
- this._reduxActions.daemon.connected();
-
// reset the reconnect backoff when connection established.
this._reconnectBackoff.reset();
@@ -470,9 +479,6 @@ export default class AppRenderer {
const actions = this._reduxActions;
const history = this._memoryHistory;
- // save to redux that the app disconnected from daemon
- actions.daemon.disconnected();
-
// recover connection on error
if (error) {
log.debug(`Lost connection to daemon: ${error.message}`);
@@ -490,7 +496,15 @@ export default class AppRenderer {
});
// take user back to the launch screen `/` except when user is in settings.
- if (!history.location.pathname.startsWith('/settings')) {
+ if (history.location.pathname.startsWith('/settings')) {
+ // TODO: Reinvent the navigation back in history to make sure that user does not end up on
+ // the restricted screen due to changes in daemon's state.
+ for (const entry of history.entries) {
+ if (!entry.pathname.startsWith('/settings')) {
+ entry.pathname = '/';
+ }
+ }
+ } else {
actions.history.replace('/');
}
}
diff --git a/gui/packages/desktop/src/renderer/redux/daemon/actions.js b/gui/packages/desktop/src/renderer/redux/daemon/actions.js
deleted file mode 100644
index 904cb3bd61..0000000000
--- a/gui/packages/desktop/src/renderer/redux/daemon/actions.js
+++ /dev/null
@@ -1,24 +0,0 @@
-// @flow
-
-export type DaemonConnectedAction = {
- type: 'DAEMON_CONNECTED',
-};
-export type DaemonDisconnectedAction = {
- type: 'DAEMON_DISCONNECTED',
-};
-
-export type DaemonAction = DaemonConnectedAction | DaemonDisconnectedAction;
-
-function connected(): DaemonConnectedAction {
- return {
- type: 'DAEMON_CONNECTED',
- };
-}
-
-function disconnected(): DaemonDisconnectedAction {
- return {
- type: 'DAEMON_DISCONNECTED',
- };
-}
-
-export default { connected, disconnected };
diff --git a/gui/packages/desktop/src/renderer/redux/daemon/reducers.js b/gui/packages/desktop/src/renderer/redux/daemon/reducers.js
deleted file mode 100644
index 5e9e707ff6..0000000000
--- a/gui/packages/desktop/src/renderer/redux/daemon/reducers.js
+++ /dev/null
@@ -1,33 +0,0 @@
-// @flow
-
-import type { ReduxAction } from '../store';
-
-export type DaemonReduxState = {
- isConnected: boolean,
-};
-
-const initialState: DaemonReduxState = {
- isConnected: false,
-};
-
-export default function(
- state: DaemonReduxState = initialState,
- action: ReduxAction,
-): DaemonReduxState {
- switch (action.type) {
- case 'DAEMON_CONNECTED':
- return {
- ...state,
- isConnected: true,
- };
-
- case 'DAEMON_DISCONNECTED':
- return {
- ...state,
- isConnected: false,
- };
-
- default:
- return state;
- }
-}
diff --git a/gui/packages/desktop/src/renderer/redux/store.js b/gui/packages/desktop/src/renderer/redux/store.js
index 133402e590..961b14d4b6 100644
--- a/gui/packages/desktop/src/renderer/redux/store.js
+++ b/gui/packages/desktop/src/renderer/redux/store.js
@@ -13,8 +13,6 @@ import supportReducer from './support/reducers';
import supportActions from './support/actions';
import versionReducer from './version/reducers';
import versionActions from './version/actions';
-import daemonReducer from './daemon/reducers';
-import daemonActions from './daemon/actions';
import windowReducer from './window/reducers';
import windowActions from './window/actions';
@@ -25,7 +23,6 @@ import type { ConnectionReduxState } from './connection/reducers';
import type { SettingsReduxState } from './settings/reducers';
import type { SupportReduxState } from './support/reducers';
import type { VersionReduxState } from './version/reducers';
-import type { DaemonReduxState } from './daemon/reducers';
import type { WindowReduxState } from './window/reducers';
import type { AccountAction } from './account/actions';
@@ -33,7 +30,6 @@ import type { ConnectionAction } from './connection/actions';
import type { SettingsAction } from './settings/actions';
import type { SupportAction } from './support/actions';
import type { VersionAction } from './version/actions';
-import type { DaemonAction } from './daemon/actions';
import type { WindowAction } from './window/actions';
export type ReduxState = {
@@ -42,7 +38,6 @@ export type ReduxState = {
settings: SettingsReduxState,
support: SupportReduxState,
version: VersionReduxState,
- daemon: DaemonReduxState,
window: WindowReduxState,
};
@@ -52,7 +47,6 @@ export type ReduxAction =
| SettingsAction
| SupportAction
| VersionAction
- | DaemonAction
| WindowAction;
export type ReduxStore = Store<ReduxState, ReduxAction, ReduxDispatch>;
export type ReduxGetState = () => ReduxState;
@@ -70,7 +64,6 @@ export default function configureStore(
...settingsActions,
...supportActions,
...versionActions,
- ...daemonActions,
...windowActions,
pushRoute: (route) => push(route),
replaceRoute: (route) => replace(route),
@@ -82,7 +75,6 @@ export default function configureStore(
settings: settingsReducer,
support: supportReducer,
version: versionReducer,
- daemon: daemonReducer,
window: windowReducer,
};
diff --git a/gui/packages/desktop/src/renderer/routes.js b/gui/packages/desktop/src/renderer/routes.js
index 9a93a76936..54e6cf8295 100644
--- a/gui/packages/desktop/src/renderer/routes.js
+++ b/gui/packages/desktop/src/renderer/routes.js
@@ -1,7 +1,7 @@
// @flow
import * as React from 'react';
-import { Switch, Route, Redirect } from 'react-router';
+import { Switch, Route } from 'react-router';
import TransitionContainer from './components/TransitionContainer';
import PlatformWindowContainer from './containers/PlatformWindowContainer';
import LaunchPage from './containers/LaunchPage';
@@ -15,123 +15,40 @@ import SupportPage from './containers/SupportPage';
import SelectLocationPage from './containers/SelectLocationPage';
import { getTransitionProps } from './transitions';
-import type { ReduxGetState } from './redux/store';
import type App from './app';
-
export type SharedRouteProps = {
app: App,
};
-export default function makeRoutes(
- getState: ReduxGetState,
- componentProps: SharedRouteProps,
-): React.Element<*> {
- // Merge props and render component
- const renderMergedProps = (
- ComponentClass: React.ComponentType<*>,
- ...rest: Array<Object>
- ): React.Element<*> => {
- const finalProps = Object.assign({}, componentProps, ...rest);
- return <ComponentClass {...finalProps} />;
- };
-
- // Renders public route
- // example: <PublicRoute path="/" component={ MyComponent } />
- const PublicRoute = ({ component, ...otherProps }) => {
- return (
- <Route
- {...otherProps}
- render={(routeProps) => {
- return renderMergedProps(component, routeProps, otherProps);
- }}
- />
- );
- };
-
- // Renders protected route that requires authentication, otherwise redirects to /
- // example: <PrivateRoute path="/protected" component={ MyComponent } />
- const PrivateRoute = ({ component, ...otherProps }) => {
- return (
- <Route
- {...otherProps}
- render={(routeProps) => {
- const { account } = getState();
- const isLoggedIn = account.status === 'ok';
-
- if (isLoggedIn) {
- return renderMergedProps(component, routeProps, otherProps);
- } else {
- return <Redirect to={'/login'} />;
- }
- }}
- />
- );
- };
-
- // Renders login route that is only available to non-authenticated
- // users. Otherwise this route redirects user to /connect.
- // example: <LoginRoute path="/login" component={ MyComponent } />
- const LoginRoute = ({ component, ...otherProps }) => {
- return (
- <Route
- {...otherProps}
- render={(routeProps) => {
- const { account } = getState();
- const isLoggedIn = account.status === 'ok';
-
- if (isLoggedIn) {
- return <Redirect to={'/connect'} />;
- } else {
- return renderMergedProps(component, routeProps, otherProps);
- }
- }}
- />
- );
- };
-
- // Renders launch route that is only available when daemon is not connected.
- // Otherwise this route redirects user to /login.
- // example: <LaunchRoute path="/" component={ MyComponent } />
- const LaunchRoute = ({ component, ...otherProps }) => {
- return (
- <Route
- {...otherProps}
- render={(routeProps) => {
- const { daemon } = getState();
- if (daemon.isConnected) {
- return <Redirect to={'/login'} />;
- } else {
- return renderMergedProps(component, routeProps, otherProps);
- }
- }}
- />
- );
- };
+export default function makeRoutes(componentProps: SharedRouteProps) {
+ // Renders a route extended with shared props
+ const CustomRoute = ({ component: ComponentClass, ...routeProps }) => (
+ <Route {...routeProps} render={() => <ComponentClass {...componentProps} />} />
+ );
// store previous route
- let previousRoute: ?string;
+ let sourceRoute: ?string;
return (
<Route
render={({ location }) => {
- const toRoute = location.pathname;
- const fromRoute = previousRoute;
- const transitionProps = getTransitionProps(fromRoute, toRoute);
- previousRoute = toRoute;
+ const destinationRoute = location.pathname;
+ const transitionProps = getTransitionProps(sourceRoute, destinationRoute);
+ sourceRoute = destinationRoute;
return (
<PlatformWindowContainer>
<TransitionContainer {...transitionProps}>
<Switch key={location.key} location={location}>
- <LaunchRoute exact path="/" component={LaunchPage} />
- <LoginRoute exact path="/login" component={LoginPage} />
- <PrivateRoute exact path="/connect" component={ConnectPage} />
- <PublicRoute exact path="/settings" component={SettingsPage} />
- <PrivateRoute exact path="/settings/account" component={AccountPage} />
- <PublicRoute exact path="/settings/preferences" component={PreferencesPage} />
- <PublicRoute exact path="/settings/advanced" component={AdvancedSettingsPage} />
- <PublicRoute exact path="/settings/support" component={SupportPage} />
- <PrivateRoute exact path="/select-location" component={SelectLocationPage} />
+ <CustomRoute exact path="/" component={LaunchPage} />
+ <CustomRoute exact path="/login" component={LoginPage} />
+ <CustomRoute exact path="/connect" component={ConnectPage} />
+ <CustomRoute exact path="/settings" component={SettingsPage} />
+ <CustomRoute exact path="/settings/account" component={AccountPage} />
+ <CustomRoute exact path="/settings/preferences" component={PreferencesPage} />
+ <CustomRoute exact path="/settings/advanced" component={AdvancedSettingsPage} />
+ <CustomRoute exact path="/settings/support" component={SupportPage} />
+ <CustomRoute exact path="/select-location" component={SelectLocationPage} />
</Switch>
</TransitionContainer>
</PlatformWindowContainer>