Implemented offline indicator
This commit is contained in:
43
frontend/components/Offline/Offline.js
Normal file
43
frontend/components/Offline/Offline.js
Normal file
@@ -0,0 +1,43 @@
|
||||
import React from 'react';
|
||||
|
||||
class Offline extends React.Component {
|
||||
static propTypes = {
|
||||
children: React.PropTypes.node,
|
||||
};
|
||||
|
||||
static childContextTypes = {
|
||||
offline: React.PropTypes.bool,
|
||||
};
|
||||
|
||||
state = {
|
||||
offline: false,
|
||||
}
|
||||
|
||||
getChildContext() {
|
||||
return {
|
||||
offline: this.state.offline,
|
||||
};
|
||||
}
|
||||
|
||||
componentDidMount = () => {
|
||||
window.addEventListener('offline', this.handleConnectionState);
|
||||
window.addEventListener('online', this.handleConnectionState);
|
||||
}
|
||||
|
||||
componentWillUnmount = () => {
|
||||
window.removeEventListener('offline', this.handleConnectionState);
|
||||
window.removeEventListener('online', this.handleConnectionState);
|
||||
};
|
||||
|
||||
handleConnectionState = () => {
|
||||
this.setState({
|
||||
offline: !navigator.onLine,
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
return React.Children.only(this.props.children);
|
||||
}
|
||||
}
|
||||
|
||||
export default Offline;
|
||||
7
frontend/components/Offline/index.js
Normal file
7
frontend/components/Offline/index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import Offline from './Offline';
|
||||
import injectOffline from './injectOffline';
|
||||
|
||||
export {
|
||||
Offline,
|
||||
injectOffline,
|
||||
}
|
||||
19
frontend/components/Offline/injectOffline.js
Normal file
19
frontend/components/Offline/injectOffline.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import React from 'react';
|
||||
|
||||
const injectOffline = (WrappedComponent) => {
|
||||
return class OfflineWrapper extends React.Component {
|
||||
static contextTypes = {
|
||||
offline: React.PropTypes.bool,
|
||||
};
|
||||
|
||||
render() {
|
||||
const newProps = {
|
||||
offline: this.context.offline,
|
||||
};
|
||||
|
||||
return (<WrappedComponent { ...this.props } { ...newProps } />);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export default injectOffline;
|
||||
Reference in New Issue
Block a user