Skip to content

Commit 1081e3a

Browse files
committed
[209] Check Internet Connection
1 parent 3df1644 commit 1081e3a

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
3+
export default class NetworkStatus extends React.Component {
4+
render() {
5+
return (
6+
<div className="network-status">
7+
<img className="img-fluid logo" src="images/gitify-logo-outline-dark.png" />
8+
<h4>No Internet Connection</h4>
9+
<div className="alert alert-danger">Couldn't establish an internet connection.</div>
10+
</div>
11+
);
12+
}
13+
};

src/js/containers/app.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,56 @@
11
import React from 'react';
2+
import { connect } from 'react-redux';
23

4+
import { fetchNotifications } from '../actions';
35
import Navigation from '../components/navigation';
6+
import NetworkStatus from '../components/network-status';
47
import SearchBar from '../components/search';
58

6-
export default class App extends React.Component {
9+
export class App extends React.Component {
710
constructor(props) {
811
super(props);
912

1013
this.state = {
11-
showSearch: false
14+
showSearch: false,
15+
networkConnected: navigator.onLine
1216
};
1317
}
1418

19+
componentDidMount() {
20+
const self = this;
21+
window.addEventListener('offline', function(e) { self.handleNetworkStatus(); });
22+
window.addEventListener('online', function(e) { self.handleNetworkStatus(); });
23+
}
24+
25+
componentWillUnmount() {
26+
window.removeEventListener('offline', this.handleNetworkStatus);
27+
window.removeEventListener('online', this.handleNetworkStatus);
28+
}
29+
1530
toggleSearch() {
1631
this.setState({
1732
showSearch: !this.state.showSearch
1833
});
1934
}
2035

36+
handleNetworkStatus() {
37+
if (navigator.onLine) {
38+
this.setState({
39+
networkConnected: true
40+
});
41+
this.props.fetchNotifications();
42+
} else {
43+
this.setState({
44+
networkConnected: false
45+
});
46+
}
47+
}
48+
2149
render() {
50+
if (!this.state.networkConnected) {
51+
return <NetworkStatus />;
52+
}
53+
2254
return (
2355
<div>
2456
<Navigation
@@ -31,3 +63,5 @@ export default class App extends React.Component {
3163
);
3264
};
3365
};
66+
67+
export default connect(null, { fetchNotifications })(App);

src/scss/app.scss

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,35 @@ input {
524524
}
525525

526526
/* @end Component / Search */
527+
528+
529+
/* @group Network Status */
530+
531+
.network-status {
532+
position: absolute;
533+
top: 0;
534+
right: 0;
535+
bottom: 0;
536+
left: 0;
537+
background-color: $gray-lighter;
538+
padding: 3rem 2rem;
539+
overflow: auto;
540+
541+
.logo {
542+
margin: 0 auto 20px;
543+
max-width: 115px;
544+
}
545+
546+
h4 {
547+
margin-bottom: 1.5rem;
548+
text-align: center;
549+
}
550+
551+
.emoji {
552+
margin: 20px 0;
553+
text-align: center;
554+
font-size: 55px;
555+
}
556+
}
557+
558+
/* @end Network Status */

0 commit comments

Comments
 (0)