Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Refactoring of ladger recover
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Mar 4, 2017
1 parent 93688c2 commit 80d22a1
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 163 deletions.
193 changes: 193 additions & 0 deletions app/renderer/components/preferences/payment/ledgerRecovery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const React = require('react')
const {StyleSheet, css} = require('aphrodite')

// util
const {btcToCurrencyString} = require('../../../../common/lib/ledgerUtil')

// components
const ImmutableComponent = require('../../../../../js/components/immutableComponent')
const Button = require('../../../../../js/components/button')
const {RecoveryKeyTextbox} = require('../../textbox')
const {SettingsList, SettingItem} = require('../../settings')

// style
const globalStyles = require('../../styles/global')
const commonStyles = require('../../styles/commonStyles')
const {paymentCommon} = require('../../styles/payment')

// other
const aboutActions = require('../../../../../js/about/aboutActions')

class LedgerRecoveryContent extends ImmutableComponent {
constructor () {
super()
this.handleFirstRecoveryKeyChange = this.handleFirstRecoveryKeyChange.bind(this)
this.handleSecondRecoveryKeyChange = this.handleSecondRecoveryKeyChange.bind(this)
}

handleFirstRecoveryKeyChange (e) {
this.props.handleFirstRecoveryKeyChange(e.target.value)
}

handleSecondRecoveryKeyChange (e) {
this.props.handleSecondRecoveryKeyChange(e.target.value)
}

clearRecoveryStatus () {
aboutActions.clearRecoveryStatus()
this.props.hideAdvancedOverlays()
}

render () {
const l10nDataArgs = {
balance: btcToCurrencyString(this.props.ledgerData.get('balance'), this.props.ledgerData)
}
const recoverySucceeded = this.props.ledgerData.get('recoverySucceeded')
const recoveryError = this.props.ledgerData.getIn(['error', 'error'])
const isNetworkError = typeof recoveryError === 'object'

return <div className={css(paymentCommon.board)}>
{
recoverySucceeded === true
? <div className={css(styles.recoveryOverlay)}>
<h1 className={css(styles.recoveryOverlay__h1)} data-l10n-id='ledgerRecoverySucceeded' />
<p className={css(styles.recoveryOverlay__p, styles.spaceAround)}
data-l10n-id='balanceRecovered'
data-l10n-args={JSON.stringify(l10nDataArgs)}
/>
<Button l10nId='ok'
className={css(commonStyles.whiteButton, commonStyles.inlineButton)}
onClick={this.clearRecoveryStatus.bind(this)}
/>
</div>
: null
}
{
(recoverySucceeded === false && recoveryError && isNetworkError)
? <div className={css(styles.recoveryOverlay)}>
<h1 className={css(styles.recoveryOverlay__h1)} data-l10n-id='ledgerRecoveryNetworkFailedTitle' />
<p className={css(styles.recoveryOverlay__p, styles.spaceAround)}
data-l10n-id='ledgerRecoveryNetworkFailedMessage'
/>
<Button l10nId='ok'
className={css(commonStyles.whiteButton, commonStyles.inlineButton)}
onClick={this.clearRecoveryStatus.bind(this)}
/>
</div>
: null
}
{
(recoverySucceeded === false && recoveryError && !isNetworkError)
? <div className={css(styles.recoveryOverlay)}>
<h1 className={css(styles.recoveryOverlay__h1)} data-l10n-id='ledgerRecoveryFailedTitle' />
<p className={css(styles.recoveryOverlay__p, styles.spaceAround)}
data-l10n-id='ledgerRecoveryFailedMessage'
/>
<Button l10nId='ok'
className={css(commonStyles.whiteButton, commonStyles.inlineButton)}
onClick={this.clearRecoveryStatus.bind(this)}
/>
</div>
: null
}
<div className={css(paymentCommon.panel, styles.recoveryContent)}>
<h4 className={css(styles.recoveryContent__h4)} data-l10n-id='ledgerRecoverySubtitle' />
<div className={css(styles.ledgerRecoveryContent)} data-l10n-id='ledgerRecoveryContent' />
<SettingsList className={css(commonStyles.noMarginBottom)}>
<SettingItem>
<h3 data-l10n-id='firstRecoveryKey' />
<RecoveryKeyTextbox id='firstRecoveryKey' onChange={this.handleFirstRecoveryKeyChange} />
<h3 className={css(styles.recoveryContent__h3)} data-l10n-id='secondRecoveryKey' />
<RecoveryKeyTextbox id='secondRecoveryKey' onChange={this.handleSecondRecoveryKeyChange} />
</SettingItem>
</SettingsList>
</div>
</div>
}
}

class LedgerRecoveryFooter extends ImmutableComponent {
constructor () {
super()
this.recoverWallet = this.recoverWallet.bind(this)
}

recoverWallet () {
aboutActions.ledgerRecoverWallet(this.props.state.FirstRecoveryKey, this.props.state.SecondRecoveryKey)
}

recoverWalletFromFile () {
aboutActions.ledgerRecoverWalletFromFile()
}

render () {
return <div className={css(paymentCommon.advanceFooter)}>
<div className={css(styles.recoveryFooterButtons)}>
<Button l10nId='recover'
className={css(commonStyles.buttonPrimary)}
onClick={this.recoverWallet}
/>
<Button l10nId='recoverFromFile'
className={css(commonStyles.buttonPrimary, paymentCommon.marginButtons)}
onClick={this.recoverWalletFromFile}
/>
<Button l10nId='cancel'
className={css(commonStyles.whiteButton, commonStyles.inlineButton, paymentCommon.marginButtons)}
onClick={this.props.hideOverlay.bind(this, 'ledgerRecovery')}
/>
</div>
</div>
}
}

const styles = StyleSheet.create({
recoveryContent: {
paddingLeft: '50px',
paddingRight: '50px',
marginTop: 0,
marginBottom: 0
},
recoveryContent__h4: {
marginBottom: globalStyles.spacing.paymentsMargin
},
recoveryContent__h3: {
marginBottom: globalStyles.spacing.modalPanelHeaderMarginBottom
},
ledgerRecoveryContent: {
marginBottom: globalStyles.spacing.paymentsMargin
},

recoveryOverlay: {
backgroundColor: globalStyles.color.black75,
border: `1px solid ${globalStyles.color.black75}`,
position: 'absolute',
top: '-1px',
left: '-1px',
width: '100%',
height: '100%',
textAlign: 'center',
zIndex: 999
},
recoveryOverlay__h1: {
color: '#fff',
marginTop: '120px'
},
recoveryOverlay__p: {
color: '#fff'
},
spaceAround: {
margin: '50px auto'
},
recoveryFooterButtons: {
float: 'right'
}
})

module.exports = {
LedgerRecoveryContent,
LedgerRecoveryFooter
}
127 changes: 26 additions & 101 deletions app/renderer/components/preferences/paymentsTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,28 @@

// Note that these are webpack requires, not CommonJS node requiring requires
const React = require('react')
const {StyleSheet, css} = require('aphrodite/no-important')

// util
const {btcToCurrencyString} = require('../../../common/lib/ledgerUtil')
// const {StyleSheet, css} = require('aphrodite/no-important')

// Components
const ImmutableComponent = require('../../../../js/components/immutableComponent')
const Button = require('../../../../js/components/button')
const ModalOverlay = require('../../../../js/components/modalOverlay')
const {RecoveryKeyTextbox} = require('../textbox')
const {SettingsList, SettingItem, SettingCheckbox} = require('../settings')
const {SettingCheckbox} = require('../settings')
const DisabledContent = require('./payment/disabledContent')
const EnabledContent = require('./payment/enabledContent')
const {AdvanceSettingsContent, AdvanceSettingsFooter} = require('./payment/advanceSettings')
const BitcoinDashboard = require('./payment/bitcoinDashboard')
const {AdvanceSettingsContent, AdvanceSettingsFooter} = require('./payment/advanceSettings')
const {HistoryContent, HistoryFooter} = require('./payment/history')
const {LedgerBackupContent, LedgerBackupFooter} = require('./payment/ledgerBackup')
const {LedgerRecoveryContent, LedgerRecoveryFooter} = require('./payment/ledgerRecovery')

// style
const globalStyles = require('../styles/global')
// const globalStyles = require('../styles/global')

// other
const getSetting = require('../../../../js/settings').getSetting
const settings = require('../../../../js/constants/settings')
const coinbaseCountries = require('../../../../js/constants/coinbaseCountries')
const aboutActions = require('../../../../js/about/aboutActions')

class PaymentsTab extends ImmutableComponent {
constructor () {
Expand All @@ -38,45 +34,33 @@ class PaymentsTab extends ImmutableComponent {
FirstRecoveryKey: '',
SecondRecoveryKey: ''
}
this.recoverWallet = this.recoverWallet.bind(this)
this.handleFirstRecoveryKeyChange = this.handleFirstRecoveryKeyChange.bind(this)
this.handleSecondRecoveryKeyChange = this.handleSecondRecoveryKeyChange.bind(this)
}

handleFirstRecoveryKeyChange (e) {
this.setState({FirstRecoveryKey: e.target.value})
}

handleSecondRecoveryKeyChange (e) {
this.setState({SecondRecoveryKey: e.target.value})
handleFirstRecoveryKeyChange (key) {
this.setState({FirstRecoveryKey: key})
this.forceUpdate()
}

recoverWallet () {
aboutActions.ledgerRecoverWallet(this.state.FirstRecoveryKey, this.state.SecondRecoveryKey)
}

recoverWalletFromFile () {
aboutActions.ledgerRecoverWalletFromFile()
}

clearRecoveryStatus () {
aboutActions.clearRecoveryStatus()
this.props.hideAdvancedOverlays()
handleSecondRecoveryKeyChange (key) {
this.setState({SecondRecoveryKey: key})
this.forceUpdate()
}

get enabled () {
return getSetting(settings.PAYMENTS_ENABLED, this.props.settings)
}

get overlayTitle () {
overlayTitle () {
if (coinbaseCountries.indexOf(this.props.ledgerData.get('countryCode')) > -1) {
return 'addFunds'
} else {
return 'addFundsAlternate'
}
}

get overlayContent () {
overlayContent () {
return <BitcoinDashboard ledgerData={this.props.ledgerData}
settings={this.props.settings}
bitcoinOverlayVisible={this.props.bitcoinOverlayVisible}
Expand All @@ -88,67 +72,6 @@ class PaymentsTab extends ImmutableComponent {
hideParentOverlay={this.props.hideOverlay.bind(this, 'addFunds')} />
}

get ledgerRecoveryContent () {
const l10nDataArgs = {
balance: btcToCurrencyString(this.props.ledgerData.get('balance'), this.props.ledgerData)
}
const recoverySucceeded = this.props.ledgerData.get('recoverySucceeded')
const recoveryError = this.props.ledgerData.getIn(['error', 'error'])
const isNetworkError = typeof recoveryError === 'object'

return <div className='board'>
{
recoverySucceeded === true
? <div className='recoveryOverlay'>
<h1 data-l10n-id='ledgerRecoverySucceeded' />
<p className='spaceAround' data-l10n-id='balanceRecovered' data-l10n-args={JSON.stringify(l10nDataArgs)} />
<Button l10nId='ok' className='whiteButton inlineButton' onClick={this.clearRecoveryStatus.bind(this)} />
</div>
: null
}
{
(recoverySucceeded === false && recoveryError && isNetworkError)
? <div className='recoveryOverlay'>
<h1 data-l10n-id='ledgerRecoveryNetworkFailedTitle' className='recoveryError' />
<p data-l10n-id='ledgerRecoveryNetworkFailedMessage' className='spaceAround' />
<Button l10nId='ok' className='whiteButton inlineButton' onClick={this.clearRecoveryStatus.bind(this)} />
</div>
: null
}
{
(recoverySucceeded === false && recoveryError && !isNetworkError)
? <div className='recoveryOverlay'>
<h1 data-l10n-id='ledgerRecoveryFailedTitle' />
<p data-l10n-id='ledgerRecoveryFailedMessage' className='spaceAround' />
<Button l10nId='ok' className='whiteButton inlineButton' onClick={this.clearRecoveryStatus.bind(this)} />
</div>
: null
}
<div className='panel recoveryContent'>
<h4 className={css(styles.recoveryContent__h4)} data-l10n-id='ledgerRecoverySubtitle' />
<div className={css(styles.ledgerRecoveryContent)} data-l10n-id='ledgerRecoveryContent' />
<SettingsList>
<SettingItem>
<h3 data-l10n-id='firstRecoveryKey' />
<RecoveryKeyTextbox id='firstRecoveryKey' onChange={this.handleFirstRecoveryKeyChange} />
<h3 data-l10n-id='secondRecoveryKey' />
<RecoveryKeyTextbox id='secondRecoveryKey' onChange={this.handleSecondRecoveryKeyChange} />
</SettingItem>
</SettingsList>
</div>
</div>
}

get ledgerRecoveryFooter () {
return <div className='panel advancedSettingsFooter'>
<div className='recoveryFooterButtons'>
<Button l10nId='recover' className='primaryButton' onClick={this.recoverWallet} />
<Button l10nId='recoverFromFile' className='primaryButton' onClick={this.recoverWalletFromFile} />
<Button l10nId='cancel' className='whiteButton' onClick={this.props.hideOverlay.bind(this, 'ledgerRecovery')} />
</div>
</div>
}

render () {
return <div className='paymentsContainer'>
{
Expand Down Expand Up @@ -204,7 +127,19 @@ class PaymentsTab extends ImmutableComponent {
}
{
this.enabled && this.props.ledgerRecoveryOverlayVisible
? <ModalOverlay title={'ledgerRecoveryTitle'} content={this.ledgerRecoveryContent} footer={this.ledgerRecoveryFooter} onHide={this.props.hideOverlay.bind(this, 'ledgerRecovery')} />
? <ModalOverlay title={'ledgerRecoveryTitle'}
content={<LedgerRecoveryContent
ledgerData={this.props.ledgerData}
hideAdvancedOverlays={this.props.hideAdvancedOverlays.bind(this)}
handleFirstRecoveryKeyChange={this.handleFirstRecoveryKeyChange.bind(this)}
handleSecondRecoveryKeyChange={this.handleSecondRecoveryKeyChange.bind(this)}
/>}
footer={<LedgerRecoveryFooter
state={this.state}
hideOverlay={this.props.hideOverlay}
/>}
onHide={this.props.hideOverlay.bind(this, 'ledgerRecovery')}
/>
: null
}
<div className='advancedSettingsWrapper'>
Expand Down Expand Up @@ -252,14 +187,4 @@ class PaymentsTab extends ImmutableComponent {
}
}

const styles = StyleSheet.create({
recoveryContent__h4: {
marginBottom: globalStyles.spacing.paymentsMargin
},

ledgerRecoveryContent: {
marginBottom: globalStyles.spacing.paymentsMargin
}
})

module.exports = PaymentsTab
Loading

0 comments on commit 80d22a1

Please sign in to comment.