Skip to content

Commit

Permalink
chore(refactor): refactor bank picker (#6263)
Browse files Browse the repository at this point in the history
  • Loading branch information
mperdomo-bc committed Feb 5, 2024
1 parent 7a05d0f commit 58c2cee
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getFiatCurrency } from 'data/components/withdraw/selectors'
import { getUserApiToken } from 'data/modules/profile/selectors'
import { BankDWStepType } from 'data/types'

import Loading from '../EnterAmount/template.loading'
import WithdrawLoading from '../WithdrawLoading'
import ConfirmData from './Steps/ConfirmData'
import { WIRE_BANK_FORM } from './Steps/constants'
import EnterIntermediaryBank from './Steps/EnterIntermediaryData'
Expand Down Expand Up @@ -120,7 +120,7 @@ const AddWireBank = () => {
return <ConfirmData onNextStep={() => onSubmit()} onClickBack={() => setStep(prevStep)} />
}
case 'LOADING':
return <Loading />
return <WithdrawLoading />
case 'SUCCESS':
return <Success bankName={formValues?.bankName ?? ''} fiatCurrency={fiatCurrency} />
case 'FAILURE':
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,51 @@
import React, { PureComponent } from 'react'
import { connect, ConnectedProps } from 'react-redux'
import { bindActionCreators, Dispatch } from 'redux'
import React, { useEffect } from 'react'
import { useDispatch } from 'react-redux'

import { Remote } from '@core'
import { BeneficiaryType, ExtractSuccess, WalletFiatType } from '@core/types'
import { BeneficiaryType, WalletFiatType } from '@core/types'
import { FlyoutOopsError } from 'components/Flyout/Errors'
import { actions } from 'data'
import { custodial } from 'data/actions'
import { fetchBankTransferAccounts } from 'data/components/brokerage/slice'
import { RootState } from 'data/rootReducer'
import { useRemote } from 'hooks'

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

class BankPicker extends PureComponent<Props> {
componentDidMount() {
if (!Remote.Success.is(this.props.data)) {
this.props.custodialActions.fetchCustodialBeneficiaries({ currency: this.props.fiatCurrency })
this.props.brokerageActions.fetchBankTransferAccounts()
}
}

render() {
return this.props.data.cata({
Failure: () => (
<FlyoutOopsError
action='close'
data-e2e='withdrawReload'
handler={this.props.handleClose}
/>
),
Loading: () => <Loading />,
NotAsked: () => <Loading />,
Success: (val) => <Success {...this.props} {...val} />
})
}
}

const mapStateToProps = (state: RootState, ownProps: OwnProps) => ({
data: getData(state, ownProps)
})
const BankPicker = ({ fiatCurrency, handleClose }: Props) => {
const dispatch = useDispatch()

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 { data, error, isLoading, isNotAsked } = useRemote((state: RootState) =>
getData(state, { fiatCurrency })
)

const connector = connect(mapStateToProps, mapDispatchToProps)
useEffect(() => {
if (!Remote.Success.is(data)) {
dispatch(custodial.fetchCustodialBeneficiaries({ currency: fiatCurrency }))
dispatch(fetchBankTransferAccounts())
}
}, [])

if (error)
return <FlyoutOopsError action='close' data-e2e='withdrawReload' handler={handleClose} />
if (!data || isLoading || isNotAsked) return <WithdrawLoading />
return (
<Success
fiatCurrency={fiatCurrency}
bankTransferAccounts={data.bankTransferAccounts}
beneficiaries={data.beneficiaries}
defaultBeneficiary={data.defaultBeneficiary}
defaultMethod={data.defaultMethod}
/>
)
}

export type OwnProps = {
export type BankPickerProps = {
beneficiary?: BeneficiaryType
fiatCurrency: WalletFiatType
handleClose: () => void
}
export type SuccessStateType = ExtractSuccess<ReturnType<typeof getData>>
export type Props = OwnProps & ConnectedProps<typeof connector>

export default connector(BankPicker)
type Props = BankPickerProps & { handleClose: () => void }

export default BankPicker
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { lift } from 'ramda'

import Remote from '@core/remote'
import { ExtractSuccess, InvitationsType } from '@core/types'
import {
BeneficiariesType,
BeneficiaryType,
ExtractSuccess,
InvitationsType,
RemoteDataType
} from '@core/types'
import { selectors } from 'data'
import { RootState } from 'data/rootReducer'
import { BankTransferAccountType } from 'data/types'

import { OwnProps } from '.'
import { BankPickerProps } from '.'

const getData = (state: RootState, ownProps: OwnProps) => {
export type BankPickerSelectorProps = {
bankTransferAccounts: BankTransferAccountType[]
beneficiaries: BeneficiariesType
defaultBeneficiary?: BeneficiaryType
defaultMethod?: BankTransferAccountType
}

const getData = (
state: RootState,
ownProps: Pick<BankPickerProps, 'fiatCurrency'>
): RemoteDataType<string, BankPickerSelectorProps> => {
let bankTransferAccountsR = selectors.components.brokerage.getBankTransferAccounts(state)
let defaultMethodR = selectors.components.brokerage.getAccount(state)
// TODO: Remove this when ach deposits withdrawals gets rolled out hundo P
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import React, { ReactElement, useState } from 'react'
import { FormattedMessage } from 'react-intl'
import { useDispatch } from 'react-redux'
import { AlertCard } from '@blockchain-com/constellation'
import styled from 'styled-components'

import { BeneficiaryType } from '@core/types'
import { Icon, Image, Text } from 'blockchain-info-components'
import { AddNewButton } from 'components/Brokerage'
import { FlyoutWrapper } from 'components/Flyout'
import { Bank, BankWire } from 'components/Flyout/model'
import { WithdrawStepEnum } from 'data/types'
import { brokerage, withdraw } from 'data/components/actions'
import { BankTransferAccountType, WithdrawStepEnum } from 'data/types'
import { getBankLogoImageName } from 'services/images'

import { Props as OwnProps, SuccessStateType } from '.'
import { BankPickerProps } from '.'
import { BankPickerSelectorProps } from './selectors'

const Top = styled.div`
display: flex;
Expand All @@ -22,25 +26,46 @@ const AlertWrapper = styled(FlyoutWrapper)`
}
`

const getLinkedBankIcon = (bankName: string): ReactElement => (
type Props = BankPickerProps & BankPickerSelectorProps

const getLinkedBankIcon = (bankName: string) => (
<Image name={getBankLogoImageName(bankName)} height='48px' />
)

const Success = (props: Props) => {
const {
bankTransferAccounts,
beneficiaries,
brokerageActions,
defaultMethod,
fiatCurrency,
withdrawActions
} = props
const Success = ({
bankTransferAccounts,
beneficiaries,
beneficiary,
defaultMethod,
fiatCurrency
}: Props) => {
const [showAlert, setShowAlert] = useState(true)
const dispatch = useDispatch()

const withdrawalDisabled = bankTransferAccounts.find(
(account) => account.capabilities?.withdrawal?.enabled === false
)

const changeStep = (account?: BankTransferAccountType, beneficiary?: BeneficiaryType) => {
dispatch(brokerage.setBankDetails({ account }))
dispatch(
withdraw.setStep({
beneficiary,
fiatCurrency,
step: WithdrawStepEnum.ENTER_AMOUNT
})
)
}

const onAddNew = () => {
dispatch(
withdraw.setStep({
fiatCurrency,
step: WithdrawStepEnum.WITHDRAWAL_METHODS
})
)
}

return (
<div>
<FlyoutWrapper>
Expand All @@ -53,12 +78,7 @@ const Success = (props: Props) => {
color='grey600'
role='button'
style={{ marginRight: '8px' }}
onClick={() =>
withdrawActions.setStep({
fiatCurrency,
step: WithdrawStepEnum.ENTER_AMOUNT
})
}
onClick={() => changeStep()}
/>
<Text color='grey800' size='20px' weight={600}>
<FormattedMessage id='scenes.settings.linked_banks' defaultMessage='Linked Banks' />
Expand Down Expand Up @@ -89,44 +109,22 @@ const Success = (props: Props) => {
isActive={account.id === defaultMethod?.id}
icon={getLinkedBankIcon(account.details.bankName)}
isDisabled={account.capabilities?.withdrawal?.enabled === false}
onClick={() => {
brokerageActions.setBankDetails({ account })
withdrawActions.setStep({
beneficiary: undefined,
fiatCurrency: props.fiatCurrency,
step: WithdrawStepEnum.ENTER_AMOUNT
})
}}
onClick={() => changeStep(account)}
/>
)
})}
{beneficiaries.map((beneficiary) => {
{beneficiaries.map((localBeneficiary) => {
return (
<BankWire
key={beneficiary.id}
beneficiary={beneficiary}
isActive={props.beneficiary?.id === beneficiary.id}
onClick={() => {
brokerageActions.setBankDetails({ account: undefined })
withdrawActions.setStep({
beneficiary,
fiatCurrency,
step: WithdrawStepEnum.ENTER_AMOUNT
})
}}
key={localBeneficiary.id}
beneficiary={localBeneficiary}
isActive={beneficiary?.id === localBeneficiary.id}
onClick={() => changeStep(undefined, localBeneficiary)}
type='WITHDRAWAL'
/>
)
})}
<AddNewButton
data-e2e='DepositAddNewPaymentMethod'
onClick={() => {
withdrawActions.setStep({
fiatCurrency,
step: WithdrawStepEnum.WITHDRAWAL_METHODS
})
}}
>
<AddNewButton data-e2e='DepositAddNewPaymentMethod' onClick={onAddNew}>
<Text color='blue600' size='16px' weight={600}>
<FormattedMessage id='buttons.add_new' defaultMessage='+ Add New' />
</Text>
Expand All @@ -135,6 +133,4 @@ const Success = (props: Props) => {
)
}

type Props = OwnProps & SuccessStateType

export default Success
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { actions } from 'data'
import { RootState } from 'data/rootReducer'
import { WithdrawStepEnum } from 'data/types'

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

class ConfirmWithdraw extends PureComponent<Props> {
Expand All @@ -33,8 +33,8 @@ class ConfirmWithdraw extends PureComponent<Props> {
Failure: () => (
<FlyoutOopsError action='retry' data-e2e='withdrawReload' handler={this.errorCallback} />
),
Loading: () => <Loading />,
NotAsked: () => <Loading />,
Loading: () => <WithdrawLoading />,
NotAsked: () => <WithdrawLoading />,
Success: (val) => <Success {...val} {...this.props} />
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import {
WithdrawStepEnum
} from 'data/types'

import WithdrawLoading from '../WithdrawLoading'
import getData from './selectors'
import Loading from './template.loading'

const EnterAmountContainer = (props: Props) => {
useEffect(() => {
Expand Down Expand Up @@ -133,8 +133,8 @@ const EnterAmountContainer = (props: Props) => {
Failure: () => (
<FlyoutOopsError action='retry' data-e2e='withdrawReload' handler={errorCallback} />
),
Loading: () => <Loading />,
NotAsked: () => <Loading />,
Loading: () => <WithdrawLoading />,
NotAsked: () => <WithdrawLoading />,
Success: (val) => {
const { crossBorderLimits, formErrors } = val
const bankTransferMethod = val.paymentMethods.methods.find((method) => {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Wrapper = styled.div`
flex-direction: column;
`

const Loading = () => {
const WithdrawLoading = () => {
return (
<Wrapper>
<SpinningLoader />
Expand All @@ -24,4 +24,4 @@ const Loading = () => {
)
}

export default Loading
export default WithdrawLoading

0 comments on commit 58c2cee

Please sign in to comment.