Skip to content

Commit

Permalink
Merge pull request #615 from gufranmirza/network-status-indicator
Browse files Browse the repository at this point in the history
Network status indicator
  • Loading branch information
Sly777 committed Feb 3, 2019
2 parents db78fa2 + 3bf5881 commit 550c813
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
44 changes: 44 additions & 0 deletions components/NetworkStatus/index.js
@@ -0,0 +1,44 @@
// @flow
import * as React from 'react';
import * as styles from './styles';

type Props = {};
type State = {
online: boolean
};

class NetworkStatus extends React.Component<Props, State> {
state = {
online: true
};

componentDidMount() {
if (typeof window !== 'undefined') {
window.addEventListener('online', this.updateOnlineStatus);
window.addEventListener('offline', this.updateOnlineStatus);
}
}

updateOnlineStatus = () => {
const condition = navigator.onLine ? 'online' : 'offline';
if (condition === 'offline') {
this.setState({ online: false });
} else if (condition === 'online') {
this.setState({ online: true });
}
};

render() {
return (
<div>
{!this.state.online && (
<styles.Root>
<styles.Label> Offline </styles.Label>
</styles.Root>
)}
</div>
);
}
}

export default NetworkStatus;
17 changes: 17 additions & 0 deletions components/NetworkStatus/styles.js
@@ -0,0 +1,17 @@
import styled from 'styled-components';

const Root = styled.div`
position: fixed;
left: 30px;
bottom: 30px;
`;

const Label = styled.div`
background: red;
color: #fff;
font-weight: bold;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
padding: 10px;
`;

export { Root, Label };
2 changes: 2 additions & 0 deletions containers/Default.js
Expand Up @@ -6,6 +6,7 @@ import PropTypes from 'prop-types';
import App from '../components/App';
import Header from '../components/Header';
import ProjectInfo from '../components/ProjectInfo';
import NetworkStatus from '../components/NetworkStatus';

type Props = {
title?: string,
Expand All @@ -25,6 +26,7 @@ const Default = (props: Props) => (
<Header pathname={props.router.url.pathname} />
<ProjectInfo />
{props.children}
<NetworkStatus />
</App>
);

Expand Down
27 changes: 17 additions & 10 deletions libraries/withData.js
Expand Up @@ -41,7 +41,7 @@ export default (
};

static defaultProps = {
accessToken: null
accessToken: ''
};

constructor(props: Props) {
Expand All @@ -67,17 +67,24 @@ export default (
};

if (!process.browser) {
const client = apolloClient(headers || {}, token || '', {});
const client = apolloClient(headers || {}, token || '', {}, ctx);
const store = reduxStore();

const app = (
<ApolloProvider client={client}>
<ReduxProvider store={store}>
<Component {...props} />
</ReduxProvider>
</ApolloProvider>
);
await getDataFromTree(app);
try {
const app = (
<ApolloProvider client={client}>
<ReduxProvider store={store}>
<Component {...props} />
</ReduxProvider>
</ApolloProvider>
);
await getDataFromTree(app);
} catch (error) {
// Prevent Apollo Client GraphQL errors from crashing SSR.
// Handle them in components via the data.error prop:
// https://github.com/apollographql/react-apollo/issues/406
// http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error
}

apolloState = client.cache.extract();
serverState = store.getState();
Expand Down

0 comments on commit 550c813

Please sign in to comment.