|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 | 17 |
|
18 |
| -import { Component } from 'react'; |
19 |
| -import { getStatus, TStatus } from '../../../api/bitbox02'; |
| 18 | +import { getStatus } from '../../../api/bitbox02'; |
20 | 19 | import { statusChanged } from '../../../api/devicessync';
|
21 |
| -import { UnsubscribeList, unsubscribe } from '../../../utils/subscriptions'; |
| 20 | +import { useSync } from '../../../hooks/api'; |
22 | 21 | import { BB02Settings } from '../../settings/bb02-settings';
|
23 | 22 |
|
24 |
| -type Props = { |
| 23 | +type TProps = { |
25 | 24 | deviceID: string;
|
26 | 25 | deviceIDs: string[];
|
27 | 26 | hasAccounts: boolean;
|
28 | 27 | }
|
29 | 28 |
|
30 |
| -type State = { |
31 |
| - status: '' | TStatus; |
32 |
| -} |
33 |
| - |
34 |
| -export class BitBox02 extends Component<Props, State> { |
35 |
| - constructor(props: Props) { |
36 |
| - super(props); |
37 |
| - this.state = { |
38 |
| - status: '', |
39 |
| - }; |
40 |
| - } |
| 29 | +export const BitBox02 = ({ deviceID, deviceIDs, hasAccounts }: TProps) => { |
| 30 | + const status = useSync( |
| 31 | + () => getStatus(deviceID), |
| 32 | + cb => statusChanged(deviceID, () => { |
| 33 | + getStatus(deviceID).then(cb); |
| 34 | + }) |
| 35 | + ); |
41 | 36 |
|
42 |
| - private unsubscribeList: UnsubscribeList = []; |
43 |
| - |
44 |
| - public componentDidMount() { |
45 |
| - const { deviceID } = this.props; |
46 |
| - this.onStatusChanged(); |
47 |
| - this.unsubscribeList = [ |
48 |
| - statusChanged(deviceID, this.onStatusChanged), |
49 |
| - ]; |
| 37 | + if (status !== 'initialized') { |
| 38 | + return null; |
50 | 39 | }
|
51 |
| - |
52 |
| - private onStatusChanged = () => { |
53 |
| - getStatus(this.props.deviceID).then(status => { |
54 |
| - this.setState({ status }); |
55 |
| - }); |
56 |
| - }; |
57 |
| - |
58 |
| - public componentWillUnmount() { |
59 |
| - unsubscribe(this.unsubscribeList); |
60 |
| - } |
61 |
| - |
62 |
| - public render() { |
63 |
| - const { deviceID, hasAccounts, deviceIDs } = this.props; |
64 |
| - const { |
65 |
| - status, |
66 |
| - } = this.state; |
67 |
| - |
68 |
| - if (status !== 'initialized') { |
69 |
| - return null; |
70 |
| - } |
71 |
| - return <BB02Settings deviceID={deviceID} deviceIDs={deviceIDs} hasAccounts={hasAccounts} />; |
72 |
| - } |
73 |
| -} |
| 40 | + return <BB02Settings deviceID={deviceID} deviceIDs={deviceIDs} hasAccounts={hasAccounts} />; |
| 41 | +}; |
0 commit comments