Skip to content

Commit

Permalink
fix(withdraw): reset amount if bank changes (#6273)
Browse files Browse the repository at this point in the history
  • Loading branch information
mperdomo-bc committed Feb 5, 2024
1 parent 3e8c046 commit 2ccf17d
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'

This comment has been minimized.

Copy link
@Markietorres

Markietorres Mar 1, 2024

gcash

import React, { useEffect } from 'react'
import { FormattedMessage } from 'react-intl'
import { useDispatch } from 'react-redux'
import { FormErrors, InjectedFormProps, reduxForm, stopAsyncValidation } from 'redux-form'
Expand Down Expand Up @@ -211,7 +211,7 @@ const ErrorMessage = ({ error, orderType }) => {
return <>{error?.amount}</>
}

return <></>
return null
}

const Amount = memoizer((props: AmountProps) => {
Expand Down Expand Up @@ -283,7 +283,6 @@ const PreviewButton = ({ invalid, orderType, paymentAccount, pristine, submittin
type='submit'
fullwidth
disabled={invalid || pristine || submitting || !paymentAccount}
onClick={() => {}}
>
{submitting ? (
<HeartbeatLoader height='16px' width='16px' color='white' />
Expand All @@ -298,6 +297,7 @@ const PreviewButton = ({ invalid, orderType, paymentAccount, pristine, submittin
)

const EnterAmount = ({
change,
crossBorderLimits,
fee,
fiatCurrency,
Expand Down Expand Up @@ -326,6 +326,11 @@ const EnterAmount = ({

const showError = !!formErrors

// If fees changes, reset just in case previous amount would be now be over any limit
useEffect(() => {
change('amount', undefined)
}, [fee])

return (
<CustomForm onSubmit={handleSubmit}>
<FlyoutContainer>
Expand Down Expand Up @@ -370,7 +375,7 @@ const EnterAmount = ({
formActions.change(
'brokerageTx',
'amount',
convertBaseToStandard('FIAT', withdrawableBalance || paymentMethod.limits.max)
convertBaseToStandard('FIAT', minMaxLimits.max)
)
// record max click withdrawal
onMaxButtonClicked?.()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ const getBrokerageLimits = (props: DepositBrokerageLimits | WithdrawalBrokerageL
props.withdrawableBalance &&
props.minWithdrawAmount
? {
max: (Number(props.withdrawableBalance) - Number(props.fee || '0')).toString(),
max: (Number(props.withdrawableBalance) - Number(props.fee ?? 0)).toString(),
min: props.minWithdrawAmount
}
: { max: '0', min: '0' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ const BankPicker = ({ fiatCurrency, handleClose }: Props) => {
fiatCurrency={fiatCurrency}
bankTransferAccounts={data.bankTransferAccounts}
beneficiaries={data.beneficiaries}
defaultBeneficiary={data.defaultBeneficiary}
defaultMethod={data.defaultMethod}
/>
)
}

export type BankPickerProps = {
beneficiary?: BeneficiaryType
fiatCurrency: WalletFiatType
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { BankPickerProps } from '.'
export type BankPickerSelectorProps = {
bankTransferAccounts: BankTransferAccountType[]
beneficiaries: BeneficiariesType
defaultBeneficiary?: BeneficiaryType
defaultMethod?: BankTransferAccountType
}

Expand All @@ -37,24 +36,19 @@ const getData = (
bankTransferAccountsR = Remote.Success([])
}
const beneficiariesR = selectors.custodial.getBeneficiaries(state)
const defaultBeneficiaryR = selectors.custodial.getDefaultBeneficiary(
ownProps.fiatCurrency,
state
)

return lift(
(
bankTransferAccounts: ExtractSuccess<typeof bankTransferAccountsR>,
beneficiaries: ExtractSuccess<typeof beneficiariesR>,
defaultBeneficiary: ExtractSuccess<typeof defaultBeneficiaryR>
beneficiaries: ExtractSuccess<typeof beneficiariesR>
) => ({
bankTransferAccounts: bankTransferAccounts.filter(
(value) => value.currency === ownProps.fiatCurrency
),
beneficiaries: beneficiaries.filter((value) => value.currency === ownProps.fiatCurrency),
defaultBeneficiary,
defaultMethod: defaultMethodR
})
)(bankTransferAccountsR, beneficiariesR, defaultBeneficiaryR)
)(bankTransferAccountsR, beneficiariesR)
}

export default getData
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { ReactElement, useState } from 'react'
import React, { useState } from 'react'
import { FormattedMessage } from 'react-intl'
import { useDispatch } from 'react-redux'
import { useDispatch, useSelector } from 'react-redux'
import { AlertCard } from '@blockchain-com/constellation'
import styled from 'styled-components'

Expand All @@ -10,6 +10,7 @@ import { AddNewButton } from 'components/Brokerage'
import { FlyoutWrapper } from 'components/Flyout'
import { Bank, BankWire } from 'components/Flyout/model'
import { brokerage, withdraw } from 'data/components/actions'
import { getBeneficiary } from 'data/components/withdraw/selectors'
import { BankTransferAccountType, WithdrawStepEnum } from 'data/types'
import { getBankLogoImageName } from 'services/images'

Expand All @@ -32,20 +33,16 @@ const getLinkedBankIcon = (bankName: string) => (
<Image name={getBankLogoImageName(bankName)} height='48px' />
)

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

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

const beneficiary = useSelector(getBeneficiary)

const changeStep = (account?: BankTransferAccountType, beneficiary?: BeneficiaryType) => {
dispatch(brokerage.setBankDetails({ account }))
dispatch(
Expand Down Expand Up @@ -106,7 +103,7 @@ const Success = ({
key={account.id}
bankDetails={account.details}
text={account.details.bankName}
isActive={account.id === defaultMethod?.id}
isActive={!beneficiary && account.id === defaultMethod?.id}
icon={getLinkedBankIcon(account.details.bankName)}
isDisabled={account.capabilities?.withdrawal?.enabled === false}
onClick={() => changeStep(account)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ const EnterAmountContainer = (props: Props) => {
step: WithdrawStepEnum.BANK_PICKER
})
} else {
props.buySellActions.setStep({
step: 'KYC_REQUIRED'
})
props.buySellActions.setStep({ step: 'KYC_REQUIRED' })
}
}

Expand Down Expand Up @@ -171,23 +169,24 @@ const EnterAmountContainer = (props: Props) => {
// Connecting the paymentAccount to the submit handler here because there's some nasty logic
// above here to determine the account being used to withdraw to. This should all ideally be refactored
const submitter = handleSubmit(paymentAccount)

return (
<EnterAmount
onSubmit={submitter}
initialValues={{ currency: props.fiatCurrency }}
crossBorderLimits={crossBorderLimits}
fee={val.fees.minorValue}
fiatCurrency={props.fiatCurrency}
formActions={props.formActions}
formErrors={formErrors}
handleBack={props.handleClose}
handleMethodClick={handleBankSelection}
initialValues={{ currency: props.fiatCurrency }}
minWithdrawAmount={val.minAmount.minorValue}
onMaxButtonClicked={handleMaxButtonClicked}
onSubmit={submitter}
orderType={BrokerageOrderType.WITHDRAW}
paymentAccount={paymentAccount}
paymentMethod={selectedPaymentMethod}
withdrawableBalance={val.withdrawableBalance}
minWithdrawAmount={val.minAmount.minorValue}
crossBorderLimits={crossBorderLimits}
formErrors={formErrors}
formActions={props.formActions}
onMaxButtonClicked={handleMaxButtonClicked}
/>
)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const Success = ({
size='20px'
color='grey600'
role='button'
onClick={handleClose}
onClick={() => handleClose()}
/>
</TopText>
</WrapperHeader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,18 @@ const Withdraw = ({ close, position, total, userClickedOutside }: ModalPropsType
const step = useSelector(selectors.components.withdraw.getStep)
const kycState = useSelector(getUserKYCState).getOrElse('NONE')

const [show, setShow] = useState(false)

useEffect(() => {
setShow(true)
}, [])

const handleClose = () => {
setShow(false)
setTimeout(() => {
close()
}, duration)
}

const isUserRejectedOrExpired = kycState === 'REJECTED' || kycState === 'EXPIRED'
if (isUserRejectedOrExpired) {
return (
<Flyout
position={position}
userClickedOutside={userClickedOutside}
total={total}
onClose={handleClose}
isOpen={show}
onClose={close}
isOpen
data-e2e='custodyWithdrawModal'
>
<Rejected handleClose={handleClose} />
<Rejected handleClose={close} />
</Flyout>
)
}
Expand All @@ -60,37 +47,29 @@ const Withdraw = ({ close, position, total, userClickedOutside }: ModalPropsType
position={position}
userClickedOutside={userClickedOutside}
total={total}
onClose={handleClose}
isOpen={show}
onClose={close}
isOpen
data-e2e='custodyWithdrawModal'
>
<FlyoutChild>
{step === WithdrawStepEnum.LOADING && <WithdrawLoading />}
{step === WithdrawStepEnum.ENTER_AMOUNT && (
<EnterAmount
fiatCurrency={fiatCurrency}
beneficiary={beneficiary}
handleClose={handleClose}
/>
<EnterAmount fiatCurrency={fiatCurrency} beneficiary={beneficiary} handleClose={close} />
)}
{step === WithdrawStepEnum.WITHDRAWAL_METHODS && (
<WithdrawalMethods fiatCurrency={fiatCurrency} handleClose={handleClose} />
<WithdrawalMethods fiatCurrency={fiatCurrency} handleClose={close} />
)}
{step === WithdrawStepEnum.BANK_PICKER && (
<BankPicker
fiatCurrency={fiatCurrency}
beneficiary={beneficiary}
handleClose={handleClose}
/>
<BankPicker fiatCurrency={fiatCurrency} handleClose={close} />
)}
{step === WithdrawStepEnum.CONFIRM_WITHDRAW && (
<ConfirmWithdraw beneficiary={beneficiary} fiatCurrency={fiatCurrency} />
)}
{step === WithdrawStepEnum.WITHDRAWAL_DETAILS && (
<WithdrawalDetails fiatCurrency={fiatCurrency} handleClose={handleClose} />
<WithdrawalDetails fiatCurrency={fiatCurrency} handleClose={close} />
)}
{step === WithdrawStepEnum.INELIGIBLE && <DataError message={INELIGIBLE_ERROR} />}
{step === WithdrawStepEnum.ON_HOLD && <OnHold handleClose={handleClose} />}
{step === WithdrawStepEnum.ON_HOLD && <OnHold handleClose={close} />}
</FlyoutChild>
</Flyout>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,29 @@ import { Col, Title, Value } from 'components/Flyout'
import { BankDetails } from 'data/types'
import { formatTextAmount } from 'services/forms'

export const ActiveToggle = ({ isActive }: { isActive: boolean }): ReactElement => {
export const ActiveToggle = ({ isActive }: { isActive: boolean }) => {
if (isActive) {
return (
<Icon
name='checkmark-circle-filled'
size='24px'
color='green600'
role='button'
style={{ justifyContent: 'flex-start' }}
/>
)
}
return (
<>
{isActive ? (
<Icon
name='checkmark-circle-filled'
size='24px'
color='green600'
role='button'
style={{ justifyContent: 'flex-start' }}
/>
) : (
<Image
name='circle-empty'
width='24px'
height='24px'
style={{ justifyContent: 'flex-start' }}
/>
)}
</>
<Image
name='circle-empty'
width='24px'
height='24px'
style={{ justifyContent: 'flex-start' }}
/>
)
}

const RightArrowIcon = styled(Icon)<{
disabled?: boolean
}>`
const RightArrowIcon = styled(Icon)<{ disabled?: boolean }>`
transform: rotate(180deg);
${(props) =>
props.disabled &&
Expand Down Expand Up @@ -191,8 +188,7 @@ const DepositOrWithdrawal = (props: {
}

const normalizeAmount = (value, prevValue) => {
// eslint-disable-next-line no-restricted-globals
if (isNaN(Number(value)) && value !== '.' && value !== '') return prevValue
if (Number.isNaN(Number(value)) && value !== '.' && value !== '') return prevValue
return formatTextAmount(value, true)
}

Expand Down

0 comments on commit 2ccf17d

Please sign in to comment.