Skip to content

Commit

Permalink
chore(settings): refactor linked banks (#6162)
Browse files Browse the repository at this point in the history
* chore(settings): refactor linked banks

* chore(refactor): addres linked banks pr comment
  • Loading branch information
mperdomo-bc committed Dec 12, 2023
1 parent 1045e11 commit ea1e7bb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -1,102 +1,35 @@
import React, { PureComponent } from 'react'
import { connect, ConnectedProps } from 'react-redux'
import { path } from 'ramda'
import { bindActionCreators, Dispatch } from 'redux'
import React, { useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'

import { ExtractSuccess, RemoteDataType, WalletFiatType } from '@core/types'
import { actions, selectors } from 'data'
import { RootState } from 'data/rootReducer'
import {
AddBankStepType,
BankTransferAccountType,
BrokerageModalOriginType,
UserDataType
} from 'data/types'
import { BSPaymentMethodsType } from '@core/types'
import { brokerage, buySell } from 'data/components/actions'
import { getTradingCurrency } from 'data/modules/profile/selectors'
import { BankTransferAccountType } from 'data/types'
import { useRemote } from 'hooks'

import getData from './selectors'
import Loading from './template.loading'
import Success from './template.success'

class LinkedBanks extends PureComponent<Props> {
componentDidMount() {
this.props.brokerageActions.fetchBankTransferAccounts()
this.props.buySellActions.fetchPaymentMethods(this.props.tradingCurrency)
}

handleBankClick = () => {
const { plaidEnabled, tradingCurrency } = this.props
const ACHProvider = plaidEnabled ? 'ADD_BANK_PLAID_MODAL' : 'ADD_BANK_YODLEE_MODAL'
this.props.brokerageActions.showModal({
modalType: tradingCurrency === 'USD' ? ACHProvider : 'ADD_BANK_YAPILY_MODAL',
origin: BrokerageModalOriginType.ADD_BANK_SETTINGS
})

this.props.brokerageActions.setAddBankStep({
addBankStep: AddBankStepType.ADD_BANK
})
}

handleShowBankClick = (account: BankTransferAccountType) => {
this.props.brokerageActions.showModal({
modalType: 'BANK_DETAILS_MODAL',
origin: BrokerageModalOriginType.BANK
})
this.props.brokerageActions.setBankDetails({
account
})
}

handleDeleteBank = (account: BankTransferAccountType) => {
this.props.brokerageActions.showModal({
modalType: 'REMOVE_BANK_MODAL',
origin: BrokerageModalOriginType.BANK
})
this.props.brokerageActions.setBankDetails({
account
})
}

render() {
return this.props.data.cata({
Failure: () => null,
Loading: () => <Loading />,
NotAsked: () => <Loading />,
Success: (val) => (
<Success
{...this.props}
{...val}
handleBankClick={this.handleBankClick}
handleShowBankClick={this.handleShowBankClick}
handleDeleteBank={this.handleDeleteBank}
/>
)
})
}
type DataType = {
bankAccounts: BankTransferAccountType[]
paymentMethods: BSPaymentMethodsType
}

const mapStateToProps = (state: RootState): LinkStatePropsType => ({
data: getData(state),
plaidEnabled: selectors.core.walletOptions.getAddPlaidPaymentProvider(state).getOrElse(false),
tradingCurrency: selectors.modules.profile.getTradingCurrency(state).getOrElse('USD'),
userData: selectors.modules.profile.getUserData(state).getOrElse({} as UserDataType)
})
const LinkedBanks = () => {
const { data, hasError, isLoading, isNotAsked } = useRemote(getData)
const tradingCurrency = useSelector(getTradingCurrency).getOrElse('USD')

const mapDispatchToProps = (dispatch: Dispatch) => ({
brokerageActions: bindActionCreators(actions.components.brokerage, dispatch),
buySellActions: bindActionCreators(actions.components.buySell, dispatch),
custodialActions: bindActionCreators(actions.custodial, dispatch),
withdrawActions: bindActionCreators(actions.components.withdraw, dispatch)
})
const dispatch = useDispatch()

const connector = connect(mapStateToProps, mapDispatchToProps)
useEffect(() => {
dispatch(brokerage.fetchBankTransferAccounts())
dispatch(buySell.fetchPaymentMethods(tradingCurrency))
}, [])

type LinkStatePropsType = {
data: RemoteDataType<string, SuccessStateType>
plaidEnabled: unknown | boolean
tradingCurrency: WalletFiatType
userData: UserDataType
if (isLoading || isNotAsked) return <Loading />
if (hasError || !data) return null
return <Success tradingCurrency={tradingCurrency} {...(data as DataType)} />
}
export type SuccessStateType = ExtractSuccess<ReturnType<typeof getData>>
export type Props = ConnectedProps<typeof connector>

export default connector(LinkedBanks)
export default LinkedBanks
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import React, { useMemo } from 'react'
import React from 'react'
import { FormattedMessage } from 'react-intl'
import { useDispatch, useSelector } from 'react-redux'
import { IconBank, PaletteColors } from '@blockchain-com/constellation'
import { any } from 'ramda'
import { InjectedFormProps, reduxForm } from 'redux-form'
import styled from 'styled-components'

import { fiatToString } from '@core/exchange/utils'
import { BSPaymentMethodType, BSPaymentTypes, WalletFiatEnum } from '@core/types'
import { getAddPlaidPaymentProvider } from '@core/redux/walletOptions/selectors'
import { BSPaymentMethodsType, BSPaymentTypes, FiatType, WalletFiatEnum } from '@core/types'
import { Coin } from '@core/utils'
import { Box, Button, Image, Text } from 'blockchain-info-components'
import { Expanded, Flex } from 'components/Flex'
import { StandardRow } from 'components/Rows'
import { SettingComponent, SettingContainer, SettingSummary } from 'components/Setting'
import { selectors } from 'data'
import { brokerage } from 'data/components/actions'
import { convertBaseToStandard } from 'data/components/exchange/services'
import { BankTransferAccountType } from 'data/types'
import { useRemote } from 'hooks'
import { AddBankStepType, BankTransferAccountType, BrokerageModalOriginType } from 'data/types'
import { getBankLogoImageName } from 'services/images'
import { media } from 'services/styles'

import { CustomSettingHeader, RemoveButton } from '../styles'
import { Props as OwnProps, SuccessStateType } from '.'

const CustomSettingComponent = styled(SettingComponent)`
margin-top: 36px;
Expand All @@ -33,25 +32,60 @@ const StyledSettingsContainer = styled(SettingContainer)`
border-bottom: none;
`

const Success: React.FC<InjectedFormProps<{}, Props> & Props> = (props) => {
const { data: paymentMethods, isLoading: isLoadingPaymentMethods } = useRemote(
selectors.components.buySell.getBSPaymentMethods
)
const Success: React.FC<InjectedFormProps<{}, Props> & Props> = ({
bankAccounts,
paymentMethods,
submitting,
tradingCurrency
}) => {
const plaidEnabled = useSelector(getAddPlaidPaymentProvider).getOrElse(false)

const dispatch = useDispatch()

const handleBankClick = () => {
const ACHProvider = plaidEnabled ? 'ADD_BANK_PLAID_MODAL' : 'ADD_BANK_YODLEE_MODAL'
dispatch(
brokerage.showModal({
modalType: tradingCurrency === 'USD' ? ACHProvider : 'ADD_BANK_YAPILY_MODAL',
origin: BrokerageModalOriginType.ADD_BANK_SETTINGS
})
)
dispatch(
brokerage.setAddBankStep({
addBankStep: AddBankStepType.ADD_BANK
})
)
}

const bankLimit = useMemo(() => {
if (isLoadingPaymentMethods) return
const handleShowBankClick = (account: BankTransferAccountType) => {
dispatch(
brokerage.showModal({
modalType: 'BANK_DETAILS_MODAL',
origin: BrokerageModalOriginType.BANK
})
)
dispatch(brokerage.setBankDetails({ account }))
}

return paymentMethods?.methods.find((method) => method.type === BSPaymentTypes.BANK_TRANSFER)
?.limits
}, [paymentMethods, isLoadingPaymentMethods])
const handleDeleteBank = (account: BankTransferAccountType) => {
dispatch(
brokerage.showModal({
modalType: 'REMOVE_BANK_MODAL',
origin: BrokerageModalOriginType.BANK
})
)
dispatch(brokerage.setBankDetails({ account }))
}

const walletBeneficiaries = props.bankAccounts.filter(
(account) => account.currency in WalletFiatEnum
)
const bankLimit = paymentMethods?.methods.find(
(method) => method.type === BSPaymentTypes.BANK_TRANSFER
)?.limits

const isEligible = any(
(method: BSPaymentMethodType) => method.type === BSPaymentTypes.BANK_TRANSFER
)(props.paymentMethods.methods)
const walletBeneficiaries = bankAccounts.filter((account) => account.currency in WalletFiatEnum)

const isEligible = paymentMethods.methods.some(
(method) => method.type === BSPaymentTypes.BANK_TRANSFER
)

return (
<StyledSettingsContainer>
Expand Down Expand Up @@ -83,7 +117,7 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = (props) => {
return (
<Box
key={account.id}
onClick={() => props.handleShowBankClick(account)}
onClick={() => handleShowBankClick(account)}
data-e2e={`bankAccountRow-${account.id}`}
isMethod
isMobile={media.mobile}
Expand Down Expand Up @@ -142,12 +176,12 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = (props) => {
<RemoveButton
data-e2e={`removeBankAccount-${account.id}`}
nature='light-red'
disabled={props.submitting}
disabled={submitting}
style={{ minWidth: 'auto' }}
// @ts-ignore
onClick={(e: SyntheticEvent) => {
e.stopPropagation()
props.handleDeleteBank(account)
handleDeleteBank(account)
}}
>
<FormattedMessage id='buttons.remove' defaultMessage='Remove' />
Expand All @@ -161,7 +195,7 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = (props) => {
</SettingSummary>
{isEligible && (
<CustomSettingComponent>
<Button nature='primary' data-e2e='addBankFromSettings' onClick={props.handleBankClick}>
<Button nature='primary' data-e2e='addBankFromSettings' onClick={handleBankClick}>
<FormattedMessage id='buttons.add_bank' defaultMessage='Add a Bank' />
</Button>
</CustomSettingComponent>
Expand All @@ -170,10 +204,10 @@ const Success: React.FC<InjectedFormProps<{}, Props> & Props> = (props) => {
)
}

type Props = OwnProps &
SuccessStateType & {
handleBankClick: () => void
handleDeleteBank: (account: BankTransferAccountType) => void
handleShowBankClick: (account: BankTransferAccountType) => void
}
type Props = {
bankAccounts: BankTransferAccountType[]
paymentMethods: BSPaymentMethodsType
tradingCurrency: FiatType
}

export default reduxForm<{}, Props>({ form: 'linkedBanks' })(Success)

0 comments on commit ea1e7bb

Please sign in to comment.