Skip to content

Commit

Permalink
refactor(Watch Only): move selectors to sagas
Browse files Browse the repository at this point in the history
  • Loading branch information
plondon committed Jun 5, 2018
1 parent 2edec44 commit c1ade41
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 67 deletions.
@@ -1,5 +1,4 @@
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'

Expand All @@ -18,12 +17,12 @@ class BchBalance extends React.PureComponent {

componentWillMount () {
if (Remote.NotAsked.is(this.props.data)) {
this.props.actions.fetchSpendableBalance(this.props.context)
this.props.actions.fetchSpendableBalance()
}
}

handleRefresh () {
this.props.actions.fetchSpendableBalance(this.props.context)
this.props.actions.fetchSpendableBalance()
}

render () {
Expand All @@ -38,10 +37,6 @@ class BchBalance extends React.PureComponent {
}
}

BchBalance.propTypes = {
context: PropTypes.array.isRequired
}

const mapStateToProps = (state) => ({
data: getData(state)
})
Expand Down
@@ -1,5 +1,4 @@
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'

Expand All @@ -18,12 +17,12 @@ class BchWatchOnlyBalance extends React.PureComponent {

componentWillMount () {
if (Remote.NotAsked.is(this.props.data)) {
this.props.actions.fetchUnspendableBalance(this.props.context)
this.props.actions.fetchUnspendableBalance()
}
}

handleRefresh () {
this.props.actions.fetchUnspendableBalance(this.props.context)
this.props.actions.fetchUnspendableBalance()
}

render () {
Expand All @@ -38,10 +37,6 @@ class BchWatchOnlyBalance extends React.PureComponent {
}
}

BchWatchOnlyBalance.propTypes = {
context: PropTypes.array.isRequired
}

const mapStateToProps = (state) => ({
data: getData(state)
})
Expand Down
@@ -1,5 +1,4 @@
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'

Expand All @@ -18,12 +17,12 @@ class BtcBalance extends React.PureComponent {

componentWillMount () {
if (Remote.NotAsked.is(this.props.data)) {
this.props.actions.fetchSpendableBalance(this.props.context)
this.props.actions.fetchSpendableBalance()
}
}

handleRefresh () {
this.props.actions.fetchSpendableBalance(this.props.context)
this.props.actions.fetchSpendableBalance()
}

render () {
Expand All @@ -38,10 +37,6 @@ class BtcBalance extends React.PureComponent {
}
}

BtcBalance.propTypes = {
context: PropTypes.array.isRequired
}

const mapStateToProps = (state) => ({
data: getData(state)
})
Expand Down
@@ -1,5 +1,4 @@
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'

Expand All @@ -18,12 +17,12 @@ class BtcWatchOnlyBalance extends React.PureComponent {

componentWillMount () {
if (Remote.NotAsked.is(this.props.data)) {
this.props.actions.fetchUnspendableBalance(this.props.context)
this.props.actions.fetchUnspendableBalance()
}
}

handleRefresh () {
this.props.actions.fetchUnspendableBalance(this.props.context)
this.props.actions.fetchUnspendableBalance()
}

render () {
Expand All @@ -38,10 +37,6 @@ class BtcWatchOnlyBalance extends React.PureComponent {
}
}

BtcWatchOnlyBalance.propTypes = {
context: PropTypes.array.isRequired
}

const mapStateToProps = (state) => ({
data: getData(state)
})
Expand Down
@@ -1,5 +1,4 @@
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'

Expand All @@ -18,12 +17,12 @@ class EthBalance extends React.PureComponent {

componentWillMount () {
if (Remote.NotAsked.is(this.props.data)) {
this.props.actions.fetchData(this.props.context)
this.props.actions.fetchData()
}
}

handleRefresh () {
this.props.actions.fetchData(this.props.context)
this.props.actions.fetchData()
}

render () {
Expand All @@ -38,10 +37,6 @@ class EthBalance extends React.PureComponent {
}
}

EthBalance.propTypes = {
context: PropTypes.string.isRequired
}

const mapStateToProps = (state) => ({
data: getData(state)
})
Expand Down
Expand Up @@ -4,25 +4,12 @@ import { bindActionCreators } from 'redux'

import { actions } from 'data'
import { getData } from './selectors'
import Error from './template.error'
import Loading from './template.loading'
import Success from './template.success'

class Balance extends React.PureComponent {
render () {
const { data } = this.props
return data.cata({
Success: (value) => <Success
btcContext={value.btcContext}
ethContext={value.ethContext}
bchContext={value.bchContext}
btcUnspendableContext={value.btcUnspendableContext}
bchUnspendableContext={value.bchUnspendableContext}
path={value.path} />,
Failure: (message) => <Error>{message}</Error>,
Loading: () => <Loading />,
NotAsked: () => <Loading />
})
return <Success btcUnspendableContext={data.btcUnspendableContext} bchUnspendableContext={data.bchUnspendableContext} path={data.path} />
}
}

Expand Down
Expand Up @@ -3,22 +3,15 @@ import { createDeepEqualSelector } from 'services/ReselectHelper'

export const getData = createDeepEqualSelector(
[
selectors.core.wallet.getSpendableContext,
selectors.core.kvStore.bch.getSpendableContext,
selectors.core.kvStore.ethereum.getContext,
selectors.core.kvStore.bch.getUnspendableContext,
selectors.core.wallet.getUnspendableContext,
selectors.router.getPathname
],
(btcContext, bchContext, ethContextR, bchUnspendableContext, btcUnspendableContext, path) => {
const transform = (ethContext) => ({
btcContext,
ethContext,
bchContext,
btcUnspendableContext,
(bchUnspendableContext, btcUnspendableContext, path) => {
return {
bchUnspendableContext,
btcUnspendableContext,
path
})
return ethContextR.map(transform)
}
}
)
Expand Up @@ -70,14 +70,14 @@ const SubItems = styled.div`
`

const Success = props => {
const { btcContext, ethContext, bchContext, btcUnspendableContext, bchUnspendableContext, path } = props
const { ethContext, btcUnspendableContext, bchUnspendableContext, path } = props

const getComponentOrder = () => {
switch (path) {
case '/btc/transactions': return [<BtcBalance large context={btcContext} />, <EthBalance context={ethContext} />, <BchBalance context={bchContext} />, <TotalBalance />]
case '/eth/transactions': return [<EthBalance large context={ethContext} />, <BtcBalance context={btcContext} />, <BchBalance context={bchContext} />, <TotalBalance />]
case '/bch/transactions': return [<BchBalance large context={bchContext} />, <BtcBalance context={btcContext} />, <EthBalance context={ethContext} />, <TotalBalance />]
default: return [<TotalBalance large />, <BtcBalance context={btcContext} />, <EthBalance context={ethContext} />, <BchBalance context={bchContext} />]
case '/btc/transactions': return [<BtcBalance large />, <EthBalance context={ethContext} />, <BchBalance />, <TotalBalance />]
case '/eth/transactions': return [<EthBalance large context={ethContext} />, <BtcBalance />, <BchBalance />, <TotalBalance />]
case '/bch/transactions': return [<BchBalance large />, <BtcBalance />, <EthBalance context={ethContext} />, <TotalBalance />]
default: return [<TotalBalance large />, <BtcBalance />, <EthBalance context={ethContext} />, <BchBalance />]
}
}

Expand Down
Expand Up @@ -10,7 +10,7 @@ export default ({ api }) => {
const fetchData = function * (action) {
try {
yield put(A.fetchDataLoading())
const { context } = action.payload
const context = yield select(selectors.core.kvStore.ethereum.getContext)
const data = yield call(api.getEthereumData, context)
const latestBlock = yield call(api.getEthereumLatestBlock)
yield call(accountSaga, data, latestBlock)
Expand Down
2 changes: 1 addition & 1 deletion packages/blockchain-wallet-v4/src/types/Wallet.js
Expand Up @@ -76,7 +76,7 @@ export const selectUnspendableAddrContext = compose(AddressMap.selectContext, Ad
export const selectContext = w => selectAddrContext(w).concat(selectXpubsContext(w))
export const selectHDAccounts = w => selectHdWallets(w).flatMap(HDWallet.selectAccounts)
export const selectSpendableContext = w => selectSpendableAddrContext(w).concat(selectXpubsContext(w))
export const selectUnspendableContext = w => selectUnspendableAddrContext(w).concat(selectXpubsContext(w))
export const selectUnspendableContext = w => selectUnspendableAddrContext(w)

const shiftWallet = compose(shiftIProp('keys', 'addresses'), shift)

Expand Down

0 comments on commit c1ade41

Please sign in to comment.