// @flow import React, { PureComponent } from 'react' import { shell } from 'electron' import { connect } from 'react-redux' import styled from 'styled-components' import { colors } from 'styles/theme' import { updateGenuineCheck } from 'reducers/onboarding' import Box from 'components/base/Box' import TrackPage from 'analytics/TrackPage' import Button from 'components/base/Button' import RadioGroup from 'components/base/RadioGroup' import GenuineCheckModal from 'components/GenuineCheckModal' import IconCross from 'icons/Cross' import IconCheck from 'icons/Check' import { Title, Description, IconOptionRow, FixedTopContainer, StepContainerInner, GenuineCheckCardWrapper, } from '../../helperComponents' import { GenuineCheckErrorPage } from './GenuineCheckErrorPage' import { GenuineCheckUnavailableFooter, GenuineCheckUnavailableMessage, } from './GenuineCheckUnavailable' import OnboardingFooter from '../../OnboardingFooter' import type { StepProps } from '../..' const mapDispatchToProps = { updateGenuineCheck } type State = { cachedPinStepButton: string, cachedRecoveryStepButton: string, isGenuineCheckModalOpened: boolean, } const INITIAL_STATE = { cachedPinStepButton: '', cachedRecoveryStepButton: '', isGenuineCheckModalOpened: false, } class GenuineCheck extends PureComponent { state = { ...INITIAL_STATE, cachedPinStepButton: this.props.onboarding.genuine.pinStepPass ? 'yes' : '', cachedRecoveryStepButton: this.props.onboarding.genuine.recoveryStepPass ? 'yes' : '', } getButtonLabel() { const { t } = this.props return [ { label: t('app:common.yes'), key: 'yes', pass: true, }, { label: t('app:common.no'), key: 'no', pass: false, }, ] } handleButtonPass = (item: Object, step: string) => { this.props.updateGenuineCheck({ [`${step}`]: item.pass }) if (step === 'pinStepPass') { this.setState({ cachedPinStepButton: item.key }) } else { this.setState({ cachedRecoveryStepButton: item.key }) } if (!item.pass) { this.setState(INITIAL_STATE) this.props.updateGenuineCheck({ displayErrorScreen: true, pinStepPass: false, recoveryStepPass: false, isGenuineFail: false, isDeviceGenuine: false, genuineCheckUnavailable: null, }) } } handleOpenGenuineCheckModal = () => { this.setState({ isGenuineCheckModalOpened: true }) } handleCloseGenuineCheckModal = (cb?: Function) => this.setState( state => ({ ...state, isGenuineCheckModalOpened: false }), () => { // FIXME: meh if (cb && typeof cb === 'function') { cb() } }, ) handleGenuineCheckPass = () => { this.handleCloseGenuineCheckModal(() => { this.props.updateGenuineCheck({ isDeviceGenuine: true, genuineCheckUnavailable: null, }) }) } handleGenuineCheckFailed = () => { this.handleCloseGenuineCheckModal(() => { this.props.updateGenuineCheck({ isGenuineFail: true, isDeviceGenuine: false, genuineCheckUnavailable: null, displayErrorScreen: true, }) }) } handleGenuineCheckUnavailable = error => { this.handleCloseGenuineCheckModal(() => { this.props.updateGenuineCheck({ isDeviceGenuine: false, genuineCheckUnavailable: error, displayErrorScreen: false, }) }) } redoGenuineCheck = () => { this.props.updateGenuineCheck({ displayErrorScreen: false }) } contactSupport = () => { const contactSupportUrl = 'https://support.ledgerwallet.com/hc/en-us/requests/new?ticket_form_id=248165' shell.openExternal(contactSupportUrl) } handlePrevStep = () => { const { prevStep, onboarding, jumpStep } = this.props onboarding.flowType === 'initializedDevice' ? jumpStep('selectDevice') : prevStep() } renderGenuineFail = () => ( ) render() { const { nextStep, prevStep, t, onboarding } = this.props const { genuine } = onboarding const { cachedPinStepButton, cachedRecoveryStepButton, isGenuineCheckModalOpened } = this.state if (genuine.displayErrorScreen) { return this.renderGenuineFail() } return ( {t('onboarding:genuineCheck.title')} {onboarding.flowType === 'restoreDevice' ? ( {t('onboarding:genuineCheck.descRestore')} ) : ( {onboarding.isLedgerNano ? t('onboarding:genuineCheck.descNano') : t('onboarding:genuineCheck.descBlue')} )} {'1.'} {t('onboarding:genuineCheck.step1.title')} this.handleButtonPass(item, 'pinStepPass')} /> {'2.'} {t('onboarding:genuineCheck.step2.title')} {genuine.pinStepPass && ( this.handleButtonPass(item, 'recoveryStepPass')} /> )} {'3.'} {t('onboarding:genuineCheck.step3.title')} {genuine.recoveryStepPass && ( {genuine.isDeviceGenuine ? ( {t('onboarding:genuineCheck.isGenuinePassed')} ) : genuine.genuineCheckUnavailable ? ( ) : ( )} )} {genuine.genuineCheckUnavailable && ( )} {genuine.genuineCheckUnavailable ? ( ) : ( )} ) } } export default connect( null, mapDispatchToProps, )(GenuineCheck) export const CardTitle = styled(Box).attrs({ ff: 'Open Sans|SemiBold', fontSize: 4, textAlign: 'left', pl: 2, })``