// @flow import React, { PureComponent, Fragment } from 'react' import styled from 'styled-components' import { connect } from 'react-redux' import { timeout } from 'rxjs/operators/timeout' import { DEVICE_INFOS_TIMEOUT } from 'config/constants' import { i } from 'helpers/staticPath' import { getCurrentDevice } from 'reducers/devices' import { createCancelablePolling } from 'helpers/promise' import getDeviceInfo from 'commands/getDeviceInfo' import Box from 'components/base/Box' import Text from 'components/base/Text' import Progress from 'components/base/Progress' import type { Device } from 'types/common' import type { StepProps } from '../' const Container = styled(Box).attrs({ alignItems: 'center', fontSize: 4, color: 'dark', })`` const Title = styled(Box).attrs({ ff: 'Museo Sans|Regular', fontSize: 5, mb: 3, })`` const Bullet = styled.span` font-weight: 600; color: #142533; ` const Separator = styled(Box).attrs({ color: 'fog', })` height: 1px; width: 100%; background-color: currentColor; ` const mapStateToProps = state => ({ device: getCurrentDevice(state), }) type Props = StepProps & { device?: Device } type State = { installing: boolean, } class StepFlashMcu extends PureComponent { state = { installing: false, } componentDidMount() { this.install() } componentWillUnmount() { this._unsubConnect() } waitForDeviceInBootloader = () => { const { unsubscribe, promise } = createCancelablePolling(async () => { const { device } = this.props if (!device) { throw new Error('No device') } const deviceInfo = await getDeviceInfo .send({ devicePath: device.path }) .pipe(timeout(DEVICE_INFOS_TIMEOUT)) .toPromise() if (!deviceInfo.isBootloader) { throw new Error('Device is not in bootloader') } return { device, deviceInfo } }) this._unsubConnect = unsubscribe return promise } install = async () => { await this.waitForDeviceInBootloader() const { flashMcu, installFinalFirmware, transitionTo } = this.props this.setState({ installing: true }) await flashMcu() const finalSuccess = await installFinalFirmware() if (finalSuccess) { transitionTo('finish') } } renderBody = () => { const { installing } = this.state const { t } = this.props if (installing) { return ( ) } return ( {'1.'} {t('app:manager.modal.mcuFirst')} {t('app:manager.modal.mcuFirst')} {'2.'} {t('app:manager.modal.mcuSecond')} {t('app:manager.modal.mcuFirst')} ) } _unsubConnect: * render() { const { t } = this.props return ( {t('app:manager.modal.mcuTitle')} {this.renderBody()} ) } } export default connect(mapStateToProps)(StepFlashMcu)