Skip to content

Commit

Permalink
fix - performances (reselect) on homepage.
Browse files Browse the repository at this point in the history
feat - TextArea debounced
  • Loading branch information
Lyncee59 committed May 30, 2018
1 parent bb57fe9 commit 130af1c
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const selectBorderColor = (state) => {
const TextAreaInput = props => {
const { errorState, ...rest } = props
const borderColor = selectBorderColor(errorState)

return <BaseTextAreaInput borderColor={borderColor} {...rest} />
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React from 'react'
import styled from 'styled-components'

import { equals } from 'ramda'
import { Text, TextAreaInput } from 'blockchain-info-components'

const Container = styled.div`
position: relative;
display: flex;
flex-direction: column;
justify-content: flex-end;
align-items: flex-start;
width: 100%;
`
const Error = styled(Text)`
position: absolute;
display: block;
height: 15px;
top: ${props => props.errorBottom ? '40px' : '-20px'};
right: 0;
`
const getErrorState = (meta) => {
return meta.touched && meta.invalid ? 'invalid' : 'initial'
}

class TextAreaDebounced extends React.Component {
static getDerivedStateFromProps (nextProps, prevState) {
if (!equals(nextProps.input.value, prevState)) {
return { value: nextProps.input.value }
}
return null
}

constructor (props) {
super(props)
this.state = { value: props.input.value }
this.timeout = undefined
this.handleChange = this.handleChange.bind(this)
this.handleBlur = this.handleBlur.bind(this)
this.handleFocus = this.handleFocus.bind(this)
}

componentWillUnmount () {
clearTimeout(this.timeout)
}

handleChange (e) {
e.preventDefault()
console.log('lol')
const value = e.target.value
this.setState({ value })
if (this.timeout) clearTimeout(this.timeout)
this.timeout = setTimeout(() => {
console.log('LOL')
this.props.input.onChange(value)
}, 500)
}

handleBlur () {
this.props.input.onBlur(this.state.value)
}

handleFocus () {
this.props.input.onFocus(this.state.value)
}

render () {
const { meta, disabled, placeholder, rows, ...rest } = this.props
const errorState = getErrorState(meta)

return (
<Container>
<TextAreaInput
value={this.state.value}
errorState={errorState}
disabled={disabled}
placeholder={placeholder}
rows={rows}
onChange={this.handleChange}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
{...rest}
/>
{meta.touched && meta.error && <Error size='12px' weight={300} color='error'>{meta.error}</Error>}
</Container>
)
}
}

export default TextAreaDebounced
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import SelectBoxUSState from './SelectBoxUSState'
import TabMenuBuySellStatus from './TabMenuBuySellStatus'
import TabMenuTransactionStatus from './TabMenuTransactionStatus'
import TextArea from './TextArea'
import TextAreaDebounced from './TextAreaDebounced'
import TextBox from './TextBox'
import TextBoxDebounced from './TextBoxDebounced'

Expand Down Expand Up @@ -61,6 +62,7 @@ export {
TabMenuBuySellStatus,
TabMenuTransactionStatus,
TextArea,
TextAreaDebounced,
TextBox,
TextBoxDebounced
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Remote } from 'blockchain-wallet-v4/src'
import { selectors } from 'data'
import { lift } from 'ramda'
import { createDeepEqualSelector } from 'services/ReselectHelper'

export const getData = (state) => {
const ethContextR = selectors.core.kvStore.ethereum.getContext(state)
const bchContextR = Remote.of(selectors.core.kvStore.bch.getContext(state))
const btcContextR = Remote.of(selectors.core.wallet.getWalletContext(state))

const path = state.router.location.pathname
const transform = lift((btcContext, ethContext, bchContext) => ({btcContext, ethContext, bchContext, path}))
return transform(btcContextR, ethContextR, bchContextR)
}
export const getData = createDeepEqualSelector(
[
selectors.core.wallet.getWalletContext,
selectors.core.kvStore.bch.getContext,
selectors.core.kvStore.ethereum.getContext,
selectors.router.getPathname
],
(btcContext, bchContext, ethContextR, path) => {
const transform = (ethContext) => ({
btcContext,
ethContext,
bchContext,
path
})
return ethContextR.map(transform)
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Field, reduxForm } from 'redux-form'

import { required, validBitcoinCashAddress } from 'services/FormHelper'
import { Button, Icon, Tooltip } from 'blockchain-info-components'
import { FiatConvertor, Form, FormGroup, FormItem, FormLabel, SelectBoxBitcoinAddresses, SelectBoxCoin, TextBox, TextArea } from 'components/Form'
import { FiatConvertor, Form, FormGroup, FormItem, FormLabel, SelectBoxBitcoinAddresses, SelectBoxCoin, TextBox, TextAreaDebounced } from 'components/Form'
import ComboDisplay from 'components/Display/ComboDisplay'
import { shouldValidate, insufficientFunds, maximumAmount, invalidAmount } from './validation'
import QRCodeCapture from 'components/QRCodeCapture'
Expand Down Expand Up @@ -80,7 +80,7 @@ const FirstStep = props => {
<FormattedMessage id='modals.sendBch.firststep.share_tooltip' defaultMessage='Add a note to remind yourself what this transaction relates to. This note will be private and only seen by you.' />
</Tooltip>
</FormLabel>
<Field name='message' component={TextArea} placeholder="What's this transaction for?" fullwidth />
<Field name='message' component={TextAreaDebounced} placeholder="What's this transaction for?" fullwidth />
</FormItem>
</FormGroup>
<FormGroup inline margin={'30px'}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Field, reduxForm } from 'redux-form'

import { required, validBitcoinAddress, validBitcoinPrivateKey } from 'services/FormHelper'
import { Button, Icon, Link, Text, Tooltip } from 'blockchain-info-components'
import { FiatConvertor, Form, FormGroup, FormItem, FormLabel, NumberBox, SelectBoxBitcoinAddresses, SelectBoxCoin, SelectBox, TextBox, TextArea } from 'components/Form'
import { FiatConvertor, Form, FormGroup, FormItem, FormLabel, NumberBox, SelectBoxBitcoinAddresses, SelectBoxCoin, SelectBox, TextBox, TextAreaDebounced } from 'components/Form'
import { shouldValidate, isAddressDerivedFromPriv, insufficientFunds, minimumAmount, maximumAmount, minimumFeePerByte, maximumFeePerByte, invalidAmount } from './validation'
import QRCodeCapture from 'components/QRCodeCapture'
import RegularFeeLink from './RegularFeeLink'
Expand Down Expand Up @@ -131,7 +131,7 @@ const FirstStep = props => {
<FormattedMessage id='modals.sendbtc.firststep.share_tooltip' defaultMessage='Add a note to remind yourself what this transaction relates to. This note will be private and only seen by you.' />
</Tooltip>
</FormLabel>
<Field name='description' component={TextArea} placeholder="What's this transaction for?" fullwidth />
<Field name='description' component={TextAreaDebounced} placeholder="What's this transaction for?" rows={3} />
</FormItem>
</FormGroup>
<FormGroup inline margin={'10px'}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Field, reduxForm } from 'redux-form'

import { required, validEtherAddress } from 'services/FormHelper'
import { Button, Text, Tooltip } from 'blockchain-info-components'
import { FiatConvertor, Form, FormGroup, FormItem, FormLabel, SelectBoxCoin, TextBox, TextArea } from 'components/Form'
import { FiatConvertor, Form, FormGroup, FormItem, FormLabel, SelectBoxCoin, TextBox, TextAreaDebounced } from 'components/Form'
import { invalidAmount, insufficientFunds, maximumAmount } from './validation'
import QRCodeCapture from 'components/QRCodeCapture'
import ComboDisplay from 'components/Display/ComboDisplay'
Expand Down Expand Up @@ -61,7 +61,7 @@ const FirstStep = props => {
<FormattedMessage id='modals.sendether.firststep.share_tooltip' defaultMessage='Add a note to remind yourself what this transaction relates to. This note will be private and only seen by you.' />
</Tooltip>
</FormLabel>
<Field name='message' component={TextArea} placeholder="What's this transaction for?" fullwidth />
<Field name='message' component={TextAreaDebounced} placeholder="What's this transaction for?" fullwidth />
</FormItem>
</FormGroup>
<FormGroup margin={'30px'}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { concat, equals, isNil, take, map, lift, prop, curry, compose, descend, reduce, sort, unapply } from 'ramda'
import { concat, isNil, take, map, lift, prop, curry, compose, descend, reduce, sort, unapply } from 'ramda'
import { selectors } from 'data'
import { createSelectorCreator, defaultMemoize } from 'reselect'
import { Remote } from 'blockchain-wallet-v4/src'

export const createDeepEqualSelector = createSelectorCreator(
defaultMemoize,
equals
)
import { createDeepEqualSelector } from 'services/ReselectHelper'

export const transform = curry((coin, transaction) => ({
type: 'transaction',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,101 @@ import { selectors } from 'data'
import { length, lift } from 'ramda'
import { Exchange } from 'blockchain-wallet-v4/src'
import { Color } from 'blockchain-info-components'
import { createDeepEqualSelector } from 'services/ReselectHelper'

export const getData = (state) => {
const btcBalanceR = selectors.core.data.bitcoin.getBalance(state)
const ethBalanceR = selectors.core.data.ethereum.getBalance(state)
const bchBalanceR = selectors.core.data.bch.getBalance(state)
const btcBalance = btcBalanceR.getOrElse(0)
const ethBalance = ethBalanceR.getOrElse(0)
const bchBalance = bchBalanceR.getOrElse(0)
const btcRates = selectors.core.data.bitcoin.getRates(state)
const ethRates = selectors.core.data.ethereum.getRates(state)
const bchRates = selectors.core.data.bch.getRates(state)
const settings = selectors.core.settings.getSettings(state)
export const getBtcBalances = createDeepEqualSelector(
[
selectors.core.data.bitcoin.getBalance,
selectors.core.data.bitcoin.getRates,
selectors.core.settings.getCurrency
],
(btcBalanceR, btcRatesR, currencyR) => {
const transform = (btcBalance, btcRates, currency) => ({
coin: btcBalance,
fiat: Exchange.convertBitcoinToFiat({ value: btcBalance, fromUnit: 'SAT', toCurrency: currency, rates: btcRates })
})
return lift(transform)(btcBalanceR, btcRatesR, currencyR)
}
)

export const getBchBalances = createDeepEqualSelector(
[
selectors.core.data.bch.getBalance,
selectors.core.data.bch.getRates,
selectors.core.settings.getCurrency
],
(bchBalanceR, bchRatesR, currencyR) => {
const transform = (bchBalance, bchRates, currency) => ({
coin: bchBalance,
fiat: Exchange.convertBchToFiat({ value: bchBalance, fromUnit: 'SAT', toCurrency: currency, rates: bchRates })
})
return lift(transform)(bchBalanceR, bchRatesR, currencyR)
}
)

const btcAccountsLength = length(selectors.core.common.bitcoin.getActiveHDAccounts(state).getOrElse([]))
const bchAccountsLength = length(selectors.core.kvStore.bch.getAccounts(state).getOrElse([]))
export const getEthBalances = createDeepEqualSelector(
[
selectors.core.data.ethereum.getBalance,
selectors.core.data.ethereum.getRates,
selectors.core.settings.getCurrency
],
(ethBalanceR, ethRatesR, currencyR) => {
const transform = (ethBalance, ethRates, currency) => ({
coin: ethBalance,
fiat: Exchange.convertEtherToFiat({ value: ethBalance, fromUnit: 'WEI', toCurrency: currency, rates: ethRates })
})
return lift(transform)(ethBalanceR, ethRatesR, currencyR)
}
)

const transform = (btcRates, ethRates, bchRates, settings) => {
const btcFiatBalance = Exchange.convertBitcoinToFiat({ value: btcBalance, fromUnit: 'SAT', toCurrency: settings.currency, rates: btcRates })
const ethFiatBalance = Exchange.convertEtherToFiat({ value: ethBalance, fromUnit: 'WEI', toCurrency: settings.currency, rates: ethRates })
const bchFiatBalance = Exchange.convertBchToFiat({ value: bchBalance, fromUnit: 'SAT', toCurrency: settings.currency, rates: bchRates })
export const getData = createDeepEqualSelector(
[
getBtcBalances,
getBchBalances,
getEthBalances,
selectors.core.common.bitcoin.getActiveHDAccounts,
selectors.core.kvStore.bch.getAccounts
],
(btcBalancesR, bchBalancesR, ethBalancesR, btcAccountsR, bchAccountsR) => {
const btcAccountsLength = length(btcAccountsR.getOrElse([]))
const bchAccountsLength = length(bchAccountsR.getOrElse([]))

const chartData = [{
y: Number(btcFiatBalance.value),
color: Color('brand-primary'),
fiat: btcFiatBalance.value,
name: 'Bitcoin',
id: 'btc'
}, {
y: Number(ethFiatBalance.value),
color: Color('brand-secondary'),
fiat: ethFiatBalance.value,
name: 'Ether',
id: 'eth'
}, {
y: Number(bchFiatBalance.value),
color: Color('brand-tertiary'),
fiat: bchFiatBalance.value,
name: 'Bitcoin Cash',
id: 'bch'
}]
const transform = (btcBalances, bchBalances, ethBalances) => {
const symbol = btcBalances.fiat.unit.symbol
const btcBalance = btcBalances.coin
const bchBalance = bchBalances.coin
const ethBalance = ethBalances.coin

return ({ btcBalance, ethBalance, bchBalance, chartData, symbol: btcFiatBalance.unit.symbol, btcAccountsLength, bchAccountsLength })
}
const chartData = [{
y: Number(btcBalances.fiat.value),
color: Color('brand-primary'),
fiat: btcBalances.fiat.value,
name: 'Bitcoin',
id: 'btc'
}, {
y: Number(ethBalances.fiat.value),
color: Color('brand-secondary'),
fiat: ethBalances.fiat.value,
name: 'Ether',
id: 'eth'
}, {
y: Number(bchBalances.fiat.value),
color: Color('brand-tertiary'),
fiat: bchBalances.fiat.value,
name: 'Bitcoin Cash',
id: 'bch'
}]

return lift(transform)(btcRates, ethRates, bchRates, settings)
}
return {
btcBalance,
ethBalance,
bchBalance,
chartData,
symbol,
btcAccountsLength,
bchAccountsLength
}
}
return lift(transform)(btcBalancesR, bchBalancesR, ethBalancesR)
}
)

0 comments on commit 130af1c

Please sign in to comment.