Files
ohif-viewer/platform/viewer/src/OHIFStandaloneViewer.js
T

181 lines
5.2 KiB
JavaScript
Raw Normal View History

2019-05-29 08:39:54 -04:00
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withRouter, matchPath } from 'react-router';
import { Route, Switch } from 'react-router-dom';
import { NProgress } from '@tanem/react-nprogress';
import { CSSTransition } from 'react-transition-group';
import { connect } from 'react-redux';
2019-08-13 15:36:46 -04:00
import { ViewerbaseDragDropContext } from '@ohif/ui';
import { SignoutCallbackComponent } from 'redux-oidc';
import asyncComponent from './components/AsyncComponent.js';
import * as RoutesUtil from './routes/routesUtil';
import NotFound from './routes/NotFound.js';
import { Bar, Container } from './components/LoadingBar/';
2019-05-29 08:39:54 -04:00
import './OHIFStandaloneViewer.css';
import './variables.css';
import './theme-tide.css';
// Contexts
import AppContext from './context/AppContext';
const CallbackPage = asyncComponent(() =>
import(/* webpackChunkName: "CallbackPage" */ './routes/CallbackPage.js')
);
2019-01-24 10:22:40 +01:00
class OHIFStandaloneViewer extends Component {
static contextType = AppContext;
state = {
isLoading: false,
};
2019-01-24 10:22:40 +01:00
static propTypes = {
history: PropTypes.object.isRequired,
2019-04-29 08:32:49 -04:00
user: PropTypes.object,
setContext: PropTypes.func,
userManager: PropTypes.object,
location: PropTypes.object,
2019-05-29 08:39:54 -04:00
};
2019-01-24 10:22:40 +01:00
componentDidMount() {
this.unlisten = this.props.history.listen((location, action) => {
if (this.props.setContext) {
2019-05-29 08:39:54 -04:00
this.props.setContext(window.location.pathname);
2019-01-24 10:22:40 +01:00
}
2019-05-29 08:39:54 -04:00
});
2019-01-24 10:22:40 +01:00
}
2019-01-24 10:22:40 +01:00
componentWillUnmount() {
2019-05-29 08:39:54 -04:00
this.unlisten();
2019-01-24 10:22:40 +01:00
}
2019-01-24 10:22:40 +01:00
render() {
2019-05-29 08:39:54 -04:00
const { user, userManager } = this.props;
const { appConfig = {} } = this.context;
2019-05-29 08:39:54 -04:00
const userNotLoggedIn = userManager && (!user || user.expired);
2019-01-24 10:22:40 +01:00
if (userNotLoggedIn) {
2019-05-29 08:39:54 -04:00
const pathname = this.props.location.pathname;
2019-01-24 10:22:40 +01:00
if (pathname !== '/callback') {
2019-05-29 08:39:54 -04:00
sessionStorage.setItem('ohif-redirect-to', pathname);
2019-01-24 10:22:40 +01:00
}
2019-01-24 10:22:40 +01:00
return (
<Switch>
<Route
exact
path="/silent-refresh.html"
onEnter={RoutesUtil.reload}
/>
2019-08-13 15:36:46 -04:00
<Route
exact
path="/logout-redirect"
render={() => (
<SignoutCallbackComponent
userManager={userManager}
successCallback={() => console.log('Signout successful')}
errorCallback={error => {
console.warn(error);
console.warn('Signout failed');
}}
/>
)}
/>
2019-01-24 10:22:40 +01:00
<Route
path="/callback"
render={() => <CallbackPage userManager={userManager} />}
/>
<Route
component={() => {
userManager.getUser().then(user => {
if (user) {
userManager.signinSilent();
} else {
userManager.signinRedirect();
}
});
2019-05-29 08:39:54 -04:00
return null;
2019-01-24 10:22:40 +01:00
}}
/>
</Switch>
2019-05-29 08:39:54 -04:00
);
}
2019-01-24 10:22:40 +01:00
/**
* Note: this approach for routing is caused by the conflict between
* react-transition-group and react-router's <Switch> component.
*
* See http://reactcommunity.org/react-transition-group/with-react-router/
*/
const routes = RoutesUtil.getRoutes(appConfig);
const currentPath = this.props.location.pathname;
const noMatchingRoutes = !routes.find(r =>
matchPath(currentPath, {
path: r.path,
exact: true,
})
);
2019-01-24 10:22:40 +01:00
return (
<>
<NProgress isAnimating={this.state.isLoading}>
{({ isFinished, progress, animationDuration }) => (
<Container
isFinished={isFinished}
animationDuration={animationDuration}
>
<Bar progress={progress} animationDuration={animationDuration} />
</Container>
)}
</NProgress>
<Route exact path="/silent-refresh.html" onEnter={RoutesUtil.reload} />
<Route exact path="/logout-redirect.html" onEnter={RoutesUtil.reload} />
{!noMatchingRoutes &&
routes.map(({ path, Component }) => (
<Route key={path} exact path={path}>
{({ match }) => (
<CSSTransition
in={match !== null}
timeout={300}
classNames="fade"
unmountOnExit
onEnter={() => {
this.setState({
isLoading: true,
});
}}
onEntered={() => {
this.setState({
isLoading: false,
});
}}
>
2019-08-13 15:36:46 -04:00
{match === null ? (
<></>
) : (
<Component match={match} location={this.props.location} />
)}
</CSSTransition>
)}
</Route>
))}
{noMatchingRoutes && <NotFound />}
</>
2019-05-29 08:39:54 -04:00
);
2019-01-24 10:22:40 +01:00
}
}
const mapStateToProps = state => {
2019-01-24 10:22:40 +01:00
return {
2019-04-29 08:32:49 -04:00
user: state.oidc.user,
2019-05-29 08:39:54 -04:00
};
};
2019-01-24 10:22:40 +01:00
const ConnectedOHIFStandaloneViewer = connect(
mapStateToProps,
null
2019-05-29 08:39:54 -04:00
)(OHIFStandaloneViewer);
2019-01-24 10:22:40 +01:00
export default ViewerbaseDragDropContext(
withRouter(ConnectedOHIFStandaloneViewer)
2019-05-29 08:39:54 -04:00
);