Created
January 8, 2020 17:43
-
-
Save juancarlosjr97/fa56bee6ddb3336176704194551a0bb9 to your computer and use it in GitHub Desktop.
Network Detector - React - Typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component } from 'react'; | |
| import { connect } from "react-redux"; | |
| import { withSnackbar } from "notistack"; | |
| import { enqueueNotification } from "@components/notifications/store/actions"; | |
| import { networkStatusChange } from "./store/actions" | |
| interface IStoreProps { | |
| enqueueNotification: typeof enqueueNotification; | |
| networkStatusChange: typeof networkStatusChange; | |
| } | |
| interface INetworkState { | |
| networkStatus: boolean; | |
| } | |
| export default function (NetworkComposedComponent: any) { | |
| class NetworkDetector extends Component<IStoreProps> { | |
| state: INetworkState = { | |
| networkStatus: true | |
| } | |
| componentDidMount() { | |
| this.handleConnectionChange(); | |
| window.addEventListener('online', this.handleConnectionChange); | |
| window.addEventListener('offline', this.handleConnectionChange); | |
| } | |
| componentWillUnmount() { | |
| window.removeEventListener('online', this.handleConnectionChange); | |
| window.removeEventListener('offline', this.handleConnectionChange); | |
| } | |
| componentDidUpdate(prevProps: any, prevState: any) { | |
| const { networkStatus } = this.state; | |
| let options: any; | |
| if (networkStatus && !prevState['networkStatus']) { | |
| this.props.networkStatusChange({ networkStatus }) | |
| options = { | |
| message: 'YOU ARE ONLINE', | |
| variant: 'success', | |
| } | |
| } else if (!networkStatus) { | |
| this.props.networkStatusChange({ networkStatus }) | |
| options = { | |
| message: 'YOU ARE OFFLINE', | |
| variant: 'error', | |
| } | |
| } | |
| if (options) { | |
| this.props.enqueueNotification({ | |
| ...options, | |
| duration: 3000, | |
| verticalPosition: 'top', | |
| horizontalPosition: 'center', | |
| closeButtonText: 'OK', | |
| }); | |
| } | |
| } | |
| handleConnectionChange = () => { | |
| const condition = navigator.onLine ? 'online' : 'offline'; | |
| if (condition === 'online') { | |
| const webPing = setInterval( | |
| () => { | |
| fetch('https://google.com', { | |
| mode: 'no-cors', | |
| }) | |
| .then(() => { | |
| this.setState({ networkStatus: true }, () => { | |
| return clearInterval(webPing) | |
| }); | |
| }).catch(() => { | |
| this.setState({ networkStatus: false }) | |
| }) | |
| }, 2000); | |
| return; | |
| } | |
| return this.setState({ networkStatus: false }); | |
| } | |
| render() { | |
| return ( | |
| <NetworkComposedComponent {...this.props} /> | |
| ); | |
| } | |
| } | |
| return withSnackbar(connect( | |
| null, | |
| { enqueueNotification, networkStatusChange } | |
| )(NetworkDetector)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment