/
index.tsx
308 lines (271 loc) · 9.68 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import React, { useEffect, useRef } from 'react'
import { connect, ConnectedProps, useSelector } from 'react-redux'
import { find, isEmpty, pathOr, propEq, propOr } from 'ramda'
import { bindActionCreators } from 'redux'
import {
BSPaymentTypes,
CrossBorderLimitsPayload,
ExtractSuccess,
FiatType,
OrderType,
WalletAccountEnum
} from '@core/types'
import { FlyoutOopsError } from 'components/Flyout/Errors'
import { GenericNabuErrorFlyout } from 'components/GenericNabuErrorFlyout'
import { actions, model, selectors } from 'data'
import { PartialClientErrorProperties } from 'data/analytics/types/errors'
import { getValidPaymentMethod } from 'data/components/buySell/model'
import { RootState } from 'data/rootReducer'
import {
Analytics,
BSCheckoutFormValuesType,
ModalName,
RecurringBuyPeriods,
SwapBaseCounterTypes
} from 'data/types'
import { useRemote } from 'hooks'
import { isNabuError } from 'services/errors'
import Loading from '../../template.loading'
import {
OwnProps as EnterAmountOwnProps,
SuccessStateType as EnterAmountSuccessStateType
} from '../index'
import getData from './selectors'
import Success from './template.success'
const { FORM_BS_CHECKOUT } = model.components.buySell
const Checkout = (props: Props) => {
const { data, error, isLoading, isNotAsked } = useRemote<
string | PartialClientErrorProperties | undefined,
ExtractSuccess<ReturnType<typeof getData>>,
RootState
>((state) => getData(state, props))
const formValues = useSelector((state: RootState) =>
selectors.form.getFormValues(FORM_BS_CHECKOUT)(state)
) as BSCheckoutFormValuesType
const goals = useSelector((state: RootState) => selectors.goals.getGoals(state))
const isPristine = useSelector((state: RootState) =>
selectors.form.isPristine(FORM_BS_CHECKOUT)(state)
)
const preferences = useSelector((state: RootState) =>
selectors.preferences.getBSCheckoutPreferences(state)
)
const methodRef = useRef<string>()
const handleSubmit = () => {
if (!data) return
props.analyticsActions.trackEvent({
key: Analytics.SELL_AMOUNT_SCREEN_NEXT_CLICKED,
properties: {}
})
// if the user is < tier 2 go to kyc but save order info
// if the user is tier 2 try to submit order, let BE fail
const { hasPaymentAccount, isSddFlow, userData } = data
const buySellGoal = find(propEq('name', 'buySell'), goals)
const id = propOr('', 'id', buySellGoal)
if (!isEmpty(id)) {
props.deleteGoal(String(id))
}
const method = props.method || props.defaultMethod
// TODO: sell
// need to do kyc check
// SELL
if (formValues?.orderType === OrderType.SELL) {
return props.buySellActions.setStep({
sellOrderType: props.swapAccount?.type,
step: 'PREVIEW_SELL'
})
}
// BUY
if (isSddFlow) {
const currentTier = userData?.tiers?.current ?? 0
if (currentTier === 2 || currentTier === 1) {
// user in SDD but already completed eligibility check, continue to payment
props.buySellActions.createOrder({ paymentType: BSPaymentTypes.PAYMENT_CARD })
} else {
// user in SDD but needs to confirm KYC and SDD eligibility
props.identityVerificationActions.verifyIdentity({
checkSddEligibility: true,
needMoreInfo: false,
onCompletionCallback: () =>
props.buySellActions.createOrder({ paymentType: BSPaymentTypes.PAYMENT_CARD }),
origin: 'BuySell',
tier: 2
})
}
} else if (!method) {
const nextStep = hasPaymentAccount ? 'LINKED_PAYMENT_ACCOUNTS' : 'PAYMENT_METHODS'
props.buySellActions.setStep({
cryptoCurrency: props.cryptoCurrency,
fiatCurrency: props.fiatCurrency,
order: props.order,
pair: props.pair,
step: nextStep
})
} else if (userData.tiers.current < 2) {
props.buySellActions.createOrder({ paymentMethodId: getValidPaymentMethod(method.type) })
} else if (formValues && method) {
switch (method.type) {
case BSPaymentTypes.PAYMENT_CARD:
if (props.mobilePaymentMethod) {
props.buySellActions.createOrder({
mobilePaymentMethod: props.mobilePaymentMethod,
paymentMethodId: method.id,
paymentType: BSPaymentTypes.PAYMENT_CARD
})
break
}
props.buySellActions.setStep({
step: 'DETERMINE_CARD_PROVIDER'
})
break
case BSPaymentTypes.USER_CARD:
props.buySellActions.createOrder({
paymentMethodId: method.id,
paymentType: BSPaymentTypes.PAYMENT_CARD
})
break
case BSPaymentTypes.FUNDS:
props.buySellActions.createOrder({ paymentType: BSPaymentTypes.FUNDS })
break
case BSPaymentTypes.BANK_TRANSFER:
props.buySellActions.createOrder({
paymentMethodId: method.id,
paymentType: BSPaymentTypes.BANK_TRANSFER
})
break
case BSPaymentTypes.BANK_ACCOUNT:
break
default:
break
}
}
}
const errorCallback = () => {
props.buySellActions.setStep({
fiatCurrency: props.fiatCurrency,
step: 'CRYPTO_SELECTION'
})
}
useEffect(() => {
const dataGoal = find(propEq('name', 'buySell'), goals)
const goalAmount = pathOr('', ['data', 'amount'], dataGoal)
const amount = goalAmount || formValues?.amount
const cryptoAmount = formValues?.cryptoAmount
const period = formValues?.period || RecurringBuyPeriods.ONE_TIME
props.buySellActions.initializeCheckout({
account: props.swapAccount,
amount,
cryptoAmount,
fix: preferences[props.orderType].fix,
orderType: props.orderType,
pair: props.pair,
pairs: props.pairs,
period
})
// If no method was given but we have a default method, set it in redux
// and the rest of the BS flow works much better
if (!props.method && props.defaultMethod) {
props.buySellActions.setMethod(props.defaultMethod)
}
if (!data) {
props.buySellActions.fetchSDDEligibility()
props.brokerageActions.fetchBankTransferAccounts()
}
// we fetch limits as part of home banners logic at that point we had only fiatCurrency
// here we have to re-fetch for crypto currency and order type
props.buySellActions.fetchLimits({
cryptoCurrency: props.cryptoCurrency,
currency: props.fiatCurrency,
side: props.orderType || OrderType.BUY
})
const swapFromAccount =
props.swapAccount?.type === SwapBaseCounterTypes.ACCOUNT
? WalletAccountEnum.NON_CUSTODIAL
: WalletAccountEnum.CUSTODIAL
// fetch cross border limits
props.buySellActions.fetchCrossBorderLimits({
fromAccount:
props.orderType === OrderType.BUY ? WalletAccountEnum.CUSTODIAL : swapFromAccount,
inputCurrency: props.orderType === OrderType.BUY ? props.fiatCurrency : props.cryptoCurrency,
outputCurrency: props.orderType === OrderType.BUY ? props.cryptoCurrency : props.fiatCurrency,
toAccount: WalletAccountEnum.CUSTODIAL
} as CrossBorderLimitsPayload)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
useEffect(() => {
// check to see if there was no default method and now there is, then fire analytics event
// we only really want this analytics event to trigger when the user opens the buy modal and
// a default payment method is automatically used
if (props.defaultMethod && props.defaultMethod?.id !== methodRef.current) {
methodRef.current = props.defaultMethod?.id
props.buySellActions.defaultMethodEvent(props.defaultMethod)
}
}, [props.buySellActions, props.defaultMethod])
if (error) {
if (isNabuError(error)) {
return <GenericNabuErrorFlyout error={error} onDismiss={errorCallback} />
}
return (
<FlyoutOopsError
action='retry'
data-e2e='sbTryCurrencySelectionAgain'
handler={errorCallback}
errorMessage={error}
/>
)
}
if (isNotAsked) {
return null
}
if (isLoading) {
return <Loading />
}
if (!data) {
return null
}
return (
<Success
formValues={formValues}
isPristine={isPristine}
preferences={preferences}
{...props}
{...data}
onSubmit={handleSubmit}
/>
)
}
const mapDispatchToProps = (dispatch) => ({
analyticsActions: bindActionCreators(actions.analytics, dispatch),
brokerageActions: bindActionCreators(actions.components.brokerage, dispatch),
buySellActions: bindActionCreators(actions.components.buySell, dispatch),
deleteGoal: (id: string) => dispatch(actions.goals.deleteGoal(id)),
identityVerificationActions: bindActionCreators(
actions.components.identityVerification,
dispatch
),
profileActions: bindActionCreators(actions.modules.profile, dispatch),
recurringBuyActions: bindActionCreators(actions.components.recurringBuy, dispatch),
showUpgradeModal: () => {
dispatch(
actions.modals.showModal(ModalName.UPGRADE_NOW_SILVER_MODAL, {
origin: 'BuySellInit'
})
)
dispatch(
actions.analytics.trackEvent({
key: Analytics.ONBOARDING_GET_MORE_ACCESS_WHEN_YOU_VERIFY,
properties: {
flow_step: 'BUY'
}
})
)
}
})
const connector = connect(null, mapDispatchToProps)
export type OwnProps = EnterAmountOwnProps &
EnterAmountSuccessStateType & { cryptoCurrency: string; fiatCurrency: FiatType }
export type SuccessStateType = ReturnType<typeof getData>['data'] & {
formErrors: { amount?: 'ABOVE_MAX' | 'BELOW_MIN' | boolean }
hasPaymentAccount: boolean
}
export type Props = OwnProps & ConnectedProps<typeof connector>
export default connector(Checkout)