Skip to content

Commit

Permalink
feat(simple buy): add cards to dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip London committed Apr 23, 2020
1 parent 1f3e2e9 commit bca1363
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ export const cancelSBOrder = (order: SBOrderType) => ({
order
})

export const createSBOrder = () => ({
type: AT.CREATE_ORDER
export const createSBOrder = (paymentMethodId?: SBCardType['id']) => ({
type: AT.CREATE_ORDER,
paymentMethodId
})

export const confirmSBOrder = () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ export default ({
}
}

const createSBOrder = function * () {
const createSBOrder = function * ({
paymentMethodId
}: ReturnType<typeof A.createSBOrder>) {
try {
const values: SBCheckoutFormValuesType = yield select(
selectors.form.getFormValues('simpleBuyCheckout')
Expand All @@ -121,7 +123,8 @@ export default ({
action,
true,
{ amount, symbol: getFiatFromPair(pair.pair) },
{ symbol: getCoinFromPair(pair.pair) }
{ symbol: getCoinFromPair(pair.pair) },
paymentMethodId
)
yield put(actions.form.stopSubmit('simpleBuyCheckout'))
yield put(A.setStep({ step: 'CHECKOUT_CONFIRM', order }))
Expand Down Expand Up @@ -417,6 +420,7 @@ export default ({

while (
(card.state === 'CREATED' || card.state === 'PENDING') &&
(card.state === 'PENDING' && !card.card) &&
retryAttempts < maxRetryAttempts
) {
card = yield call(api.getSBCard, cardId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import * as Currency from 'blockchain-wallet-v4/src/exchange/currency'
import {
CARD_TYPES,
DEFAULT_CARD_SVG_LOGO
} from 'components/Form/CreditCardBox/model'
import { convertBaseToStandard } from 'data/components/exchange/services'
import { Field } from 'redux-form'
import { getFiatFromPair } from 'data/components/simpleBuy/model'
import { IcoMoonType } from 'blockchain-info-components/src/Icons/Icomoon'
import { Icon, Text } from 'blockchain-info-components'
import { Props } from '../template.success'
import { SBPaymentMethodType } from 'core/types'
import { SBCardType, SBPaymentMethodType } from 'core/types'
import { SelectBox } from 'components/Form'
import React, { PureComponent } from 'react'
import React, { PureComponent, ReactElement } from 'react'
import styled from 'styled-components'

const SelectBoxMethod = styled(SelectBox)`
Expand Down Expand Up @@ -65,52 +68,91 @@ const Limit = styled(Text)`
class MethodSelect extends PureComponent<Props> {
state = {}

getType = (type: 'BANK_TRANSFER' | 'CARD') => {
switch (type) {
getType = (value: ElementValueType) => {
switch (value.type) {
case 'BANK_TRANSFER':
return 'Bank Wire Transfer'
case 'CARD':
return 'Credit or Debit Card'
case 'USER_CARD':
return value.card.label
}
}

getIcon = (type: 'BANK_TRANSFER' | 'CARD'): keyof IcoMoonType => {
switch (type) {
getIcon = (value: ElementValueType): ReactElement => {
switch (value.type) {
case 'BANK_TRANSFER':
return 'bank-filled'
return (
<IconContainer>
<Icon size='18px' color='blue600' name='bank-filled' />
</IconContainer>
)
case 'CARD':
return 'credit-card-filled'
return (
<IconContainer>
<Icon size='18px' color='blue600' name='credit-card-filled' />
</IconContainer>
)
case 'USER_CARD':
let cardType = CARD_TYPES.find(
card => card.type === value.card.type.toLowerCase()
)
return (
<img
height='18px'
width='auto'
src={cardType ? cardType.logo : DEFAULT_CARD_SVG_LOGO}
/>
)
}
}

renderElements = () => {
const availableCards = this.props.cards.filter(({ card }) => !!card)
const defaultCardMethod = this.props.paymentMethods.methods.find(
m => m.type === 'CARD'
)
const cardMethods = availableCards.map(card => ({
text: card.card.label,
value: {
...card,
type: 'USER_CARD',
limits: defaultCardMethod
? defaultCardMethod.limits
: { min: '1000', max: '500000' }
}
}))
const defaultMethods = this.props.paymentMethods.methods.map(value => ({
text: this.getType(value),
value
}))

return [
{
group: '',
items: this.props.paymentMethods.methods.map(value => ({
text: this.getType(value.type),
value
}))
items: [...cardMethods, ...defaultMethods]
}
]
}

renderDisplay = (props: { value: SBPaymentMethodType }, children) => {
renderDisplay = (
props: {
value: ElementValueType
},
children
) => {
if (!props.value) return
if (!this.props.formValues) return
if (!this.props.formValues.pair) return

const icon = this.getIcon(props.value.type)
const fiat = getFiatFromPair(this.props.formValues.pair.pair)
const isItem = !children

return (
<DisplayContainer isItem={isItem}>
<IconContainer>
<Icon size='18px' color='blue600' name={icon} />
</IconContainer>
{this.getIcon(props.value)}
<Display>
{children || this.getType(props.value.type)}
{children || this.getType(props.value)}
<Limit>
{Currency.fiatToString({
value: convertBaseToStandard('FIAT', props.value.limits.max),
Expand Down Expand Up @@ -138,4 +180,11 @@ class MethodSelect extends PureComponent<Props> {
}
}

type ElementValueType =
| SBPaymentMethodType
| SBCardType & {
limits: SBPaymentMethodType['limits']
type: 'USER_CARD'
}

export default MethodSelect
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export default ({
input: SBMoneyType,
output: {
symbol: CoinType
}
},
paymentMethodId?: SBCardType['id']
): SBOrderType =>
authorizedPost({
url: nabuUrl,
Expand All @@ -74,7 +75,8 @@ export default ({
pair,
action,
input,
output
output,
paymentMethodId
}
})

Expand Down
15 changes: 13 additions & 2 deletions packages/blockchain-wallet-v4/src/network/api/simpleBuy/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,25 @@ export type SBCardType = {
addedAt: Date
address: NabuAddressType
attributes: {}
card: null
card: {
expireMonth: number
expireYear: number
label: string
number: string
type: 'VISA' | 'MASTERCARD'
}
currency: 'EUR'
id: string
partner: 'EVERYPAY'
state: SBCardStateType
}

export type SBCardStateType = 'PENDING' | 'CREATED' | 'ACTIVE' | 'BLOCKED'
export type SBCardStateType =
| 'PENDING'
| 'CREATED'
| 'ACTIVE'
| 'BLOCKED'
| 'FRAUD_REVIEW'

export enum SBBuyPairsType {
'BTC-EUR',
Expand Down

0 comments on commit bca1363

Please sign in to comment.