2018-12-11 04:13:45 -08:00
|
|
|
// @flow
|
2019-02-04 20:41:45 -08:00
|
|
|
|
2018-12-11 04:13:45 -08:00
|
|
|
import React, { type ComponentType, Component } from 'react';
|
|
|
|
|
2018-12-19 07:59:31 -08:00
|
|
|
import { LoadingScreen } from './loading-screen';
|
|
|
|
|
2018-12-11 04:13:45 -08:00
|
|
|
import rpc from '../../services/api';
|
|
|
|
|
2019-01-02 12:34:32 -08:00
|
|
|
type Props = {};
|
|
|
|
|
2018-12-11 04:13:45 -08:00
|
|
|
type State = {
|
|
|
|
isRunning: boolean,
|
2019-01-02 12:34:32 -08:00
|
|
|
progress: number,
|
2019-02-13 07:44:14 -08:00
|
|
|
message: string,
|
2018-12-11 04:13:45 -08:00
|
|
|
};
|
|
|
|
|
2018-12-15 07:10:39 -08:00
|
|
|
/* eslint-disable max-len */
|
2018-12-11 04:13:45 -08:00
|
|
|
export const withDaemonStatusCheck = <PassedProps: {}>(
|
|
|
|
WrappedComponent: ComponentType<PassedProps>,
|
|
|
|
): ComponentType<$Diff<PassedProps, Props>> => class extends Component<PassedProps, State> {
|
|
|
|
timer: ?IntervalID = null;
|
|
|
|
|
|
|
|
state = {
|
|
|
|
isRunning: false,
|
2019-01-02 12:34:32 -08:00
|
|
|
progress: 0,
|
2019-02-13 07:44:14 -08:00
|
|
|
message: 'ZEC Wallet Starting',
|
2018-12-11 04:13:45 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.runTest();
|
|
|
|
|
2018-12-11 14:28:05 -08:00
|
|
|
this.timer = setInterval(this.runTest, 2000);
|
2018-12-11 04:13:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.timer) {
|
|
|
|
clearInterval(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 14:28:05 -08:00
|
|
|
runTest = () => {
|
2019-01-02 12:34:32 -08:00
|
|
|
rpc
|
|
|
|
.getinfo()
|
|
|
|
.then((response) => {
|
|
|
|
if (response) {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.setState(() => ({ isRunning: true }));
|
|
|
|
}, 500);
|
|
|
|
this.setState(() => ({ progress: 100 }));
|
|
|
|
|
|
|
|
if (this.timer) {
|
|
|
|
clearInterval(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
2018-12-11 14:28:05 -08:00
|
|
|
}
|
2019-01-02 12:34:32 -08:00
|
|
|
})
|
2019-02-13 07:44:14 -08:00
|
|
|
.catch((error) => {
|
|
|
|
const statusMessage = error.message === 'Something went wrong' ? 'ZEC Wallet Starting' : error.message;
|
|
|
|
|
2019-01-02 12:34:32 -08:00
|
|
|
this.setState((state) => {
|
|
|
|
const newProgress = state.progress > 70 ? state.progress + 2.5 : state.progress + 5;
|
2019-02-13 07:44:14 -08:00
|
|
|
return { progress: newProgress > 95 ? 95 : newProgress, message: statusMessage };
|
2019-01-02 12:34:32 -08:00
|
|
|
});
|
|
|
|
});
|
2018-12-11 14:28:05 -08:00
|
|
|
};
|
2018-12-11 04:13:45 -08:00
|
|
|
|
|
|
|
render() {
|
2019-02-13 07:44:14 -08:00
|
|
|
const { isRunning, progress, message } = this.state;
|
2018-12-12 10:59:13 -08:00
|
|
|
|
|
|
|
if (isRunning) {
|
2018-12-11 04:13:45 -08:00
|
|
|
return <WrappedComponent {...this.props} {...this.state} />;
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:44:14 -08:00
|
|
|
return <LoadingScreen progress={progress} message={message} />;
|
2018-12-11 04:13:45 -08:00
|
|
|
}
|
|
|
|
};
|