Updated ScrollTop component so that it gets the window object injected as a dependency

This commit is contained in:
Alejandro Celaya 2018-12-21 10:38:35 +01:00
parent 16cf30f26f
commit 047d99be6d
4 changed files with 9 additions and 11 deletions

View file

@ -4,6 +4,7 @@ module.exports = {
'src/**/*.js',
'!src/registerServiceWorker.js',
'!src/index.js',
'!src/reducers/index.js',
'!src/**/provideServices.js',
'!src/container/*.js',
],

View file

@ -1,20 +1,14 @@
import React from 'react';
import PropTypes from 'prop-types';
export default class ScrollToTop extends React.Component {
const ScrollToTop = (window) => class ScrollToTop extends React.Component {
static propTypes = {
location: PropTypes.object,
window: PropTypes.shape({
scrollTo: PropTypes.func,
}),
children: PropTypes.node,
};
static defaultProps = {
window: global.window,
};
componentDidUpdate(prevProps) {
const { location, window } = this.props;
const { location } = this.props;
if (location !== prevProps.location) {
window.scrollTo(0, 0);
@ -24,4 +18,6 @@ export default class ScrollToTop extends React.Component {
render() {
return this.props.children;
}
}
};
export default ScrollToTop;

View file

@ -5,7 +5,9 @@ import MenuLayout from '../MenuLayout';
import AsideMenu from '../AsideMenu';
const provideServices = (bottle, connect, withRouter) => {
bottle.constant('ScrollToTop', ScrollToTop);
bottle.constant('window', global.window);
bottle.serviceFactory('ScrollToTop', ScrollToTop, 'window');
bottle.decorator('ScrollToTop', withRouter);
bottle.serviceFactory('MainHeader', MainHeader, 'ServersDropdown');

View file

@ -29,7 +29,6 @@ const provideServices = (bottle, connect, withRouter) => {
// Services
bottle.constant('csvjson', csvjson);
bottle.constant('window', global.window);
bottle.service('ServersImporter', ServersImporter, 'csvjson');
bottle.service('ServersService', ServersService, 'Storage');
bottle.service('ServersExporter', ServersExporter, 'ServersService', 'window', 'csvjson');