From a5ec6ae4c2dc65f45479044db90cdd69268502e3 Mon Sep 17 00:00:00 2001 From: George Lima Date: Tue, 11 Dec 2018 09:13:45 -0300 Subject: [PATCH] feature: add withDaemonStatusCheck HOC --- app/components/with-daemon-status-check.js | 54 ++++++++++++++++++++++ app/views/dashboard.js | 5 +- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 app/components/with-daemon-status-check.js diff --git a/app/components/with-daemon-status-check.js b/app/components/with-daemon-status-check.js new file mode 100644 index 0000000..26679e0 --- /dev/null +++ b/app/components/with-daemon-status-check.js @@ -0,0 +1,54 @@ +// @flow +import React, { type ComponentType, Component } from 'react'; + +import rpc from '../../services/api'; + +type State = { + isRunning: boolean, +}; + +type Props = {}; + +export const withDaemonStatusCheck = ( + WrappedComponent: ComponentType, +): ComponentType<$Diff> => class extends Component { + timer: ?IntervalID = null; + + state = { + isRunning: false, + }; + + componentDidMount() { + this.runTest(); + + this.timer = setInterval(() => this.runTest(), 3000); + } + + componentDidUpdate(prevProps: Props, prevState: State) { + if (!prevState.isRunning && this.state.isRunning && this.timer) { + clearInterval(this.timer); + this.timer = null; + } + } + + componentWillUnmount() { + if (this.timer) { + clearInterval(this.timer); + this.timer = null; + } + } + + runTest() { + rpc.getinfo().then((response) => { + if (response) this.setState(() => ({ isRunning: true })); + }); + } + + render() { + if (this.state.isRunning) { + return ; + } + + return 'Daemon is starting...'; + } + }; diff --git a/app/views/dashboard.js b/app/views/dashboard.js index 6798cf1..89fdd6a 100644 --- a/app/views/dashboard.js +++ b/app/views/dashboard.js @@ -3,6 +3,7 @@ import React from 'react'; import { WalletSummaryComponent } from '../components/wallet-summary'; +import { withDaemonStatusCheck } from '../components/with-daemon-status-check'; type Props = { getSummary: () => void, @@ -15,7 +16,7 @@ type Props = { addresses: string[], }; -export class DashboardView extends React.Component { +export class Dashboard extends React.Component { componentDidMount() { this.props.getSummary(); } @@ -42,3 +43,5 @@ export class DashboardView extends React.Component { ); } } + +export const DashboardView = withDaemonStatusCheck(Dashboard);