Skip to content

Commit

Permalink
Added scrollTo to scroll top on route change
Browse files Browse the repository at this point in the history
  • Loading branch information
zanselm5 committed May 1, 2017
1 parent 8d89a3c commit f245a57
Show file tree
Hide file tree
Showing 6 changed files with 2,991 additions and 5 deletions.
2,957 changes: 2,956 additions & 1 deletion dist/js/app.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"react-ga": "^2.2.0",
"react-redux": "^5.0.4",
"react-router-dom": "^4.1.1",
"react-router-redux": "^4.0.8",
"redux": "^3.6.0",
"redux-devtools": "^3.4.0",
"redux-logger": "^3.0.1",
Expand Down
25 changes: 23 additions & 2 deletions src/components/Layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {configureAuthRoute} from '../library/authentication';
import {RedirectWithStatus} from '../library/routing';
import {Alerts} from '../library/alerts'
import {Loader} from '../library/loader';
import {scrollTo} from '../library/utilities';
import {UserActions} from '../library/authentication';
import initInterceptors from '../interceptors';
import {apiRoutes} from '../constants/apiBaseRoutes';
Expand All @@ -25,6 +26,10 @@ import Register from './pages/Register';
import NotFound from './pages/NotFound';
import Topics from './pages/Topics';

let _viewListener;

// TODO: Animation between view change is not working when wrapped around a Switch

// Initialize global interceptors such as 401, 403
initInterceptors(apiRoutes.dev, 300);

Expand All @@ -37,8 +42,20 @@ const mapDispatchToProps = (dispatch) => {
class Layout extends React.Component {
constructor() {
super();

this.onViewChange = this.onViewChange.bind(this);
}

componentWillMount() {
_viewListener = this.props.history.listen((location, action) => {
this.onViewChange(location);
});
}

onViewChange(location) {
scrollTo(0, 100);
}

render() {
return (
<div>
Expand All @@ -48,9 +65,9 @@ class Layout extends React.Component {

<Animation transitionName="view" transitionAppear={true} transitionAppearTimeout={250} transitionEnter={true} transitionEnterTimeout={250} transitionLeave={true} transitionLeaveTimeout={250} component='div' className='content-container'>
<Switch>
<Route location={this.props.location} path="/" exact component={Home}/>
<Route key={1} location={this.props.location} path="/" exact component={Home}/>
<RedirectWithStatus location={this.props.location} status={301} from="/redirect" to="/"/>
<Route location={this.props.location} path="/contacts" component={Contacts}/>
<Route key={2} location={this.props.location} path="/contacts" component={Contacts}/>
<Route location={this.props.location} path="/login" component={Login}/>
<Route location={this.props.location} path="/register" component={Register}/>
<Route location={this.props.location} path="/providers" component={Providers}/>
Expand All @@ -68,6 +85,10 @@ class Layout extends React.Component {
</div>
)
}

componentWillUnmount() {
_viewListener();
}
}

export default withRouter(connect(null, mapDispatchToProps)(Layout));
7 changes: 5 additions & 2 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import React from 'react';
import {render} from 'react-dom';
import {BrowserRouter} from 'react-router-dom';
import {Provider} from 'react-redux'
import Layout from './components/Layout';
import {createBrowserHistory} from 'history';
import {syncHistoryWithStore} from 'react-router-redux';
import Layout from './components/Layout';
import store from './store';

const history = createBrowserHistory();
// TODO: This does not seem to be working
const browserHistory = createBrowserHistory();
let history = syncHistoryWithStore(browserHistory, store);

window.onload = () => {
render(
Expand Down
2 changes: 2 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

import {combineReducers} from 'redux';
import {routerReducer as routing} from 'react-router-redux';
import {provider, providers} from './providers';
import {contact, contacts} from './contacts';
import {alerts} from '../library/alerts';
Expand All @@ -9,6 +10,7 @@ import {loader} from '../library/loader';
import {forms} from '../library/validations';

export default combineReducers({
routing,
contact,
contacts,
provider,
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4363,6 +4363,10 @@ react-router-dom@^4.1.1:
prop-types "^15.5.4"
react-router "^4.1.1"

react-router-redux@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/react-router-redux/-/react-router-redux-4.0.8.tgz#227403596b5151e182377dab835b5d45f0f8054e"

react-router@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/react-router/-/react-router-4.1.1.tgz#d448f3b7c1b429a6fbb03395099949c606b1fe95"
Expand Down

0 comments on commit f245a57

Please sign in to comment.