on master help sign in
feat: implement online status in walletgen
commit3bd9a57f36f7061cf7e394946e7584b576187c31 authorMatt Condon (shrugs) <mattgcondon@gmail.com> dateTue 11 Jun 2019, 14:24 (7 years ago) parent955c817
5 files changed+72−75
src/Root.js+1−1 src/bridge/store/online.js+16−2 src/walletgen/Walletgen.js+21−42 src/walletgen/components/Header.js+34−23 src/walletgen/lib/constants.js−7
src/Root.js+1−1
@@ -15,7 +15,7 @@ class Root extends React.Component {
1515 super(props);
1616
1717 this.state = {
18− appView: 'bridge',
18+ appView: 'walletgen',
1919 };
2020
2121 this.setView = this.setView.bind(this);
src/bridge/store/online.js+16−2
@@ -6,12 +6,26 @@ import React, {
66 useState,
77} from 'react';
88
9+export const ONLINE_STATUS = {
10+ ONLINE: Symbol('ONLINE'),
11+ OFFLINE: Symbol('OFFLINE'),
12+ UNKNOWN: Symbol('UNKNOWN'),
13+};
14+
15+const getNetworkState = () => {
16+ if ('onLine' in navigator) {
17+ return navigator.onLine ? ONLINE_STATUS.ONLINE : ONLINE_STATUS.OFFLINE;
18+ }
19+
20+ return ONLINE_STATUS.UNKNOWN;
21+};
22+
923function _useOnline() {
10− const [online, setOnline] = useState(window.navigator.onLine);
24+ const [online, setOnline] = useState(getNetworkState());
1125
1226 useEffect(() => {
1327 function handleOnlineStatus(event) {
14− setOnline(window.navigator.onLine);
28+ setOnline(getNetworkState());
1529 }
1630
1731 window.addEventListener('online', handleOnlineStatus);
src/walletgen/Walletgen.js+21−42
@@ -4,7 +4,7 @@ import * as lodash from 'lodash';
44import Header from './components/Header';
55import Footer from './components/Footer';
66
7−import { NETWORK_STATES, PROFILE_STATES, GEN_STATES } from './lib/constants';
7+import { PROFILE_STATES, GEN_STATES } from './lib/constants';
88
99import PrivacyPolicy from './views/PrivacyPolicy';
1010import Welcome from './views/Welcome';
@@ -18,9 +18,7 @@ import Download from './views/Download';
1818import Done from './views/Done';
1919
2020import './style/index.css';
21−
22−const checkNetwork = () =>
23− navigator.onLine ? NETWORK_STATES.ONLINE : NETWORK_STATES.OFFLINE;
21+import { OnlineProvider } from '../bridge/store/online';
2422
2523const ROUTES = [
2624 { path: '/Welcome', Component: Welcome },
@@ -41,7 +39,6 @@ class Walletgen extends React.Component {
4139 constructor(props) {
4240 super(props);
4341 this.state = {
44− network: checkNetwork(),
4542 'profile.state': PROFILE_STATES.NOSTART,
4643 'profile.value': null,
4744 'profile.shipCount': 0,
@@ -65,18 +62,9 @@ class Walletgen extends React.Component {
6562 };
6663 }
6764
68− componentDidMount = () => {
69− window.addEventListener('online', () =>
70− this.setState({ network: NETWORK_STATES.ONLINE })
71− );
72− window.addEventListener('offline', () =>
73− this.setState({ network: NETWORK_STATES.OFFLINE })
74− );
75− };
76−
7765 setGlobalState = state => this.setState(state);
7866
79− router = (network, path) => {
67+ router = path => {
8068 const route = key => {
8169 const { Component } = lodash.find(ROUTES, { path: key });
8270 const globals = {
@@ -87,39 +75,30 @@ class Walletgen extends React.Component {
8775 };
8876
8977 return route(path);
90−
91− // return network === NETWORK_STATES.ONLINE
92− // ? <NetworkWarning />
93− // : !(network === NETWORK_STATES.OFFLINE)
94− // ? <h1>Detecting your network state.</h1>
95− // : route(path)
9678 };
9779
9880 render() {
99− const { network, route, currentStep, lastRoute } = this.state;
81+ const { route, currentStep, lastRoute } = this.state;
10082
101− const routed = this.router(network, route); // hacky prod
102− // const routed = this.router(NETWORK_STATES.OFFLINE, true, route) // hacky dev
83+ const routed = this.router(route);
10384
10485 return (
105− <main className={'container'}>
106− <Header
107− network={network}
108− currentStep={currentStep}
109− totalSteps={TOTAL_STEPS}
110− />
111−
112− <div className={'row wrapper'}>
113− <div className={'col-'}>{routed}</div>
114− <div className={'push'} />
115− </div>
116−
117− <Footer
118− route={route}
119− lastRoute={lastRoute}
120− setGlobalState={this.setGlobalState}
121− />
122− </main>
86+ <OnlineProvider>
87+ <main className={'container'}>
88+ <Header currentStep={currentStep} totalSteps={TOTAL_STEPS} />
89+
90+ <div className={'row wrapper'}>
91+ <div className={'col-'}>{routed}</div>
92+ <div className={'push'} />
93+ </div>
94+
95+ <Footer
96+ route={route}
97+ lastRoute={lastRoute}
98+ setGlobalState={this.setGlobalState}
99+ />
100+ </main>
101+ </OnlineProvider>
123102 );
124103 }
125104}
src/walletgen/components/Header.js+34−23
@@ -1,7 +1,7 @@
11import React from 'react';
22import { seq } from '../lib/lib';
33
4−import { NETWORK_STATES } from '../lib/constants';
4+import { useOnline, ONLINE_STATUS } from '../../bridge/store/online';
55
66const StatusIndicator = props => (
77 <div className={`flex h-9 align-center ${props.c2} flex-shrink-0`}>
@@ -12,27 +12,38 @@ const StatusIndicator = props => (
1212 </div>
1313);
1414
15−const ChooseIndicator = props =>
16− props.network === NETWORK_STATES.ONLINE ? (
17− <StatusIndicator
18− message={'You are online.'}
19− c1={'bg-red'}
20− c2={'bg-red'}
21− error
22− />
23− ) : props.network === NETWORK_STATES.OFFLINE ? (
24− <StatusIndicator
25− message={'You are offline.'}
26− c1={'bg-green'}
27− c2={'bg-green'}
28− />
29− ) : (
30− <StatusIndicator
31− message={'Checking network connection..'}
32− c1={'bg-orange'}
33− c2={'bg-orange'}
34− />
35− );
15+function ChooseIndicator(props) {
16+ const online = useOnline();
17+
18+ switch (online) {
19+ case ONLINE_STATUS.ONLINE:
20+ return (
21+ <StatusIndicator
22+ message={'You are online.'}
23+ c1={'bg-red'}
24+ c2={'bg-red'}
25+ error
26+ />
27+ );
28+ case ONLINE_STATUS.OFFLINE:
29+ return (
30+ <StatusIndicator
31+ message={'You are offline.'}
32+ c1={'bg-green'}
33+ c2={'bg-green'}
34+ />
35+ );
36+ case ONLINE_STATUS.UNKNOWN:
37+ default:
38+ return (
39+ <StatusIndicator
40+ message={'Checking network connection..'}
41+ c1={'bg-orange'}
42+ c2={'bg-orange'}
43+ />
44+ );
45+ }
46+}
3647
3748const ProgressIndicator = ({ currentStep, totalSteps }) => (
3849 <div className={'relative h-9 col- mb-0'}>
@@ -61,7 +72,7 @@ const Header = props => (
6172 totalSteps={props.totalSteps}
6273 currentStep={props.currentStep}
6374 />
64− <ChooseIndicator network={props.network} />
75+ <ChooseIndicator />
6576 </div>
6677 <div className={'col-md-8 h-15 flex align-center'}>
6778 <h1 className={'m-0 p-0'}>{'Wallet Generator'}</h1>
src/walletgen/lib/constants.js−7
@@ -11,12 +11,6 @@ const GALAXY_ENTROPY_BITS = 384;
1111
1212const SEED_ENTROPY_BITS = 128;
1313
14−const NETWORK_STATES = {
15− ONLINE: Symbol('ONLINE'),
16− OFFLINE: Symbol('OFFLINE'),
17− UNKNOWN: Symbol('UNKNOWN'),
18−};
19−
2014const GEN_STATES = {
2115 ENY_NOSTART: Symbol('ENY_NOSTART'),
2216 ENY_PENDING: Symbol('ENY_PENDING'),
@@ -48,7 +42,6 @@ const PROFILE_STATES = {
4842};
4943
5044export {
51− NETWORK_STATES,
5245 GEN_STATES,
5346 BUTTON_STATES,
5447 PROFILE_STATES,