feature: add withDaemonStatusCheck HOC
This commit is contained in:
parent
e921230333
commit
a5ec6ae4c2
|
@ -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 = <PassedProps: {}>(
|
||||
WrappedComponent: ComponentType<PassedProps>,
|
||||
): ComponentType<$Diff<PassedProps, Props>> => class extends Component<PassedProps, State> {
|
||||
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 <WrappedComponent {...this.props} {...this.state} />;
|
||||
}
|
||||
|
||||
return 'Daemon is starting...';
|
||||
}
|
||||
};
|
|
@ -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<Props> {
|
||||
export class Dashboard extends React.Component<Props> {
|
||||
componentDidMount() {
|
||||
this.props.getSummary();
|
||||
}
|
||||
|
@ -42,3 +43,5 @@ export class DashboardView extends React.Component<Props> {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
export const DashboardView = withDaemonStatusCheck(Dashboard);
|
||||
|
|
Loading…
Reference in New Issue