Skip to content

Commit 9491058

Browse files
committed
feat: add toastr on connection status change
1 parent b5409e6 commit 9491058

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

public/app/config/messages.js

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const ERROR_CLEARING_USER_INPUT_MESSAGE =
4747
'There was an error clearing the user input.';
4848
const SUCCESS_CLEARING_USER_INPUT_MESSAGE =
4949
'User input was successfully cleared.';
50+
const CONNECTION_MESSAGE_HEADER = 'Connection Status';
51+
const CONNECTION_OFFLINE_MESSAGE = 'You are offline';
52+
const CONNECTION_ONLINE_MESSAGE = 'You are online';
5053

5154
module.exports = {
5255
ERROR_GETTING_DEVELOPER_MODE,
@@ -83,4 +86,7 @@ module.exports = {
8386
ERROR_SYNCING_MESSAGE,
8487
ERROR_CLEARING_USER_INPUT_MESSAGE,
8588
SUCCESS_CLEARING_USER_INPUT_MESSAGE,
89+
CONNECTION_MESSAGE_HEADER,
90+
CONNECTION_OFFLINE_MESSAGE,
91+
CONNECTION_ONLINE_MESSAGE,
8692
};

src/App.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import { connect } from 'react-redux';
4-
import ReduxToastr from 'react-redux-toastr';
4+
import ReduxToastr, { toastr } from 'react-redux-toastr';
55
import { HashRouter as Router, Route, Switch } from 'react-router-dom';
66
import { MuiThemeProvider } from '@material-ui/core/styles';
7+
import { withStyles } from '@material-ui/core';
78
import { withTranslation } from 'react-i18next';
89
import { Detector } from 'react-detect-offline';
10+
import WifiIcon from '@material-ui/icons/Wifi';
11+
import WifiOffIcon from '@material-ui/icons/WifiOff';
912
import Home from './Home';
1013
import VisitSpace from './components/VisitSpace';
1114
import SpacesNearby from './components/SpacesNearby';
@@ -31,8 +34,17 @@ import {
3134
getGeolocationEnabled,
3235
} from './actions/user';
3336
import { DEFAULT_LANGUAGE } from './config/constants';
37+
import {
38+
CONNECTION_MESSAGE_HEADER,
39+
CONNECTION_OFFLINE_MESSAGE,
40+
CONNECTION_ONLINE_MESSAGE,
41+
} from './config/messages';
3442
import './App.css';
3543

44+
const styles = () => ({
45+
toastrIcon: { marginBottom: '-20px', fontSize: '45px' },
46+
});
47+
3648
export class App extends Component {
3749
state = { height: 0 };
3850

@@ -47,6 +59,9 @@ export class App extends Component {
4759
changeLanguage: PropTypes.func.isRequired,
4860
}).isRequired,
4961
geolocationEnabled: PropTypes.bool.isRequired,
62+
classes: PropTypes.shape({
63+
toastrIcon: PropTypes.string.isRequired,
64+
}).isRequired,
5065
};
5166

5267
static defaultProps = {
@@ -97,13 +112,28 @@ export class App extends Component {
97112
this.setState({ height: window.innerHeight });
98113
};
99114

115+
triggerConnectionToastr = online => {
116+
const { classes } = this.props;
117+
118+
if (online) {
119+
toastr.light(CONNECTION_MESSAGE_HEADER, CONNECTION_ONLINE_MESSAGE, {
120+
icon: <WifiIcon className={classes.toastrIcon} />,
121+
});
122+
} else {
123+
toastr.light(CONNECTION_MESSAGE_HEADER, CONNECTION_OFFLINE_MESSAGE, {
124+
icon: <WifiOffIcon className={classes.toastrIcon} />,
125+
});
126+
}
127+
};
128+
100129
render() {
101130
const { height } = this.state;
102131

103132
return (
104133
<Detector
105134
render={({ online }) => (
106135
<MuiThemeProvider theme={online ? OnlineTheme : OfflineTheme}>
136+
{this.triggerConnectionToastr(online)}
107137
<div>
108138
<ReduxToastr
109139
transitionIn="fadeIn"
@@ -154,6 +184,8 @@ const mapDispatchToProps = {
154184

155185
const ConnectedApp = connect(mapStateToProps, mapDispatchToProps)(App);
156186

157-
const TranslatedApp = withTranslation()(ConnectedApp);
187+
const StyledApp = withStyles(styles, { withTheme: true })(ConnectedApp);
188+
189+
const TranslatedApp = withTranslation()(StyledApp);
158190

159191
export default TranslatedApp;

src/config/messages.js

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const ERROR_CLEARING_USER_INPUT_MESSAGE =
4747
'There was an error clearing the user input.';
4848
const SUCCESS_CLEARING_USER_INPUT_MESSAGE =
4949
'User input was successfully cleared.';
50+
const CONNECTION_MESSAGE_HEADER = 'Connection Status';
51+
const CONNECTION_OFFLINE_MESSAGE = 'You are offline';
52+
const CONNECTION_ONLINE_MESSAGE = 'You are online';
5053

5154
module.exports = {
5255
ERROR_GETTING_DEVELOPER_MODE,
@@ -83,4 +86,7 @@ module.exports = {
8386
ERROR_SYNCING_MESSAGE,
8487
ERROR_CLEARING_USER_INPUT_MESSAGE,
8588
SUCCESS_CLEARING_USER_INPUT_MESSAGE,
89+
CONNECTION_MESSAGE_HEADER,
90+
CONNECTION_OFFLINE_MESSAGE,
91+
CONNECTION_ONLINE_MESSAGE,
8692
};

0 commit comments

Comments
 (0)