/
index.tsx
658 lines (616 loc) · 22.8 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
import React, { PureComponent } from 'react'
import { FormattedMessage } from 'react-intl'
import { connect, ConnectedProps } from 'react-redux'
import { bindActionCreators, compose, Dispatch } from 'redux'
import { clearSubmitErrors, Form, InjectedFormProps, reduxForm } from 'redux-form'
import styled from 'styled-components'
import { Exchange } from '@core'
import { coinToString, formatFiat } from '@core/exchange/utils'
import { BSOrderActionType, BSPairType, CoinType, PaymentValue, RatesType } from '@core/types'
import {
Button,
HeartbeatLoader,
Icon,
Link,
SkeletonRectangle,
Text,
TextGroup
} from 'blockchain-info-components'
import { ErrorCartridge } from 'components/Cartridge'
import { FlyoutWrapper, Row, Value } from 'components/Flyout'
import { GenericNabuErrorFlyout } from 'components/GenericNabuErrorFlyout'
import { actions, model, selectors } from 'data'
import { getFiatFromPair } from 'data/components/buySell/model'
import { convertBaseToStandard } from 'data/components/exchange/services'
import { getInputFromPair, getOutputFromPair } from 'data/components/swap/model'
import { RootState } from 'data/rootReducer'
import {
Analytics,
BSCheckoutFormValuesType,
SwapAccountType,
SwapBaseCounterTypes
} from 'data/types'
import { isNabuError } from 'services/errors'
import { Border, TopText } from '../../Swap/components'
import { ErrorCodeMappings } from '../model'
import { QuoteCountDown } from '../QuoteCountDown'
import Loading from '../template.loading'
import { SellButton } from './SellButton'
const { FORM_BS_CHECKOUT, FORM_BS_PREVIEW_SELL } = model.components.buySell
const CustomForm = styled(Form)`
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
`
const RowItem = styled(Row)`
display: flex;
justify-content: space-between;
`
const TopRow = styled.div`
display: flex;
justify-content: space-between;
`
const RowIcon = styled.div`
display: flex;
`
const RowItemContainer = styled.div`
display: flex;
flex-direction: column;
width: 100%;
`
const RowTextWrapper = styled.div`
text-align: right;
`
const RowText = styled(Text)`
font-size: 16px;
font-weight: 500;
color: ${(props) => props.theme.grey900};
display: flex;
flex-direction: column;
justify-content: center;
`
const AdditionalText = styled(Text)`
font-weight: 500;
color: ${(props) => props.theme.grey400};
text-align: right;
font-size: 14px;
`
const ToolTipText = styled.div`
display: flex;
border-radius: 8px;
margin-top: 8px;
padding: 16px;
background-color: ${(props) => props.theme.grey000};
animation: fadeIn 0.3s ease-in-out;
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
`
const IconWrapper = styled.div`
display: flex;
flex-direction: column;
justify-content: center;
cursor: pointer;
margin-left: 4px;
`
const QuoteCountDownWrapper = styled.div`
margin-top: 28px;
`
const Amount = styled.div`
display: flex;
flex-direction: column;
margin-top: 8px;
> div {
display: flex;
flex-direction: row;
}
`
const Bottom = styled(FlyoutWrapper)`
display: flex;
flex-direction: column;
padding-top: 30px;
height: 100%;
`
const BottomActions = styled.div`
display: flex;
flex-direction: column;
justify-content: flex-end;
flex: 1;
`
const DisclaimerText = styled(Text)`
display: flex;
font-size: 14px;
font-weight: 500;
margin-top: 24px;
text-align: left;
a {
color: ${(props) => props.theme.blue600};
cursor: pointer;
text-decoration: none;
display: contents;
}
`
const getNetworkValue = (value: PaymentValue) =>
value.coin === 'BTC' || value.coin === 'BCH' ? value.selection?.fee : value.fee
class PreviewSell extends PureComponent<
InjectedFormProps<{ destroyOnUnmount: boolean; form: string }, Props> & Props,
State
> {
constructor(props) {
super(props)
this.state = { isSetCoinToolTip: false, isSetNetworkFee: false }
}
componentDidMount() {
this.props.analyticsActions.trackEvent({
key: Analytics.SELL_CHECKOUT_VIEWED,
properties: {}
})
}
handleSubmit = (e) => {
e.preventDefault()
this.props.analyticsActions.trackEvent({
key: Analytics.SELL_CHECKOUT_SCREEN_SUBMITTED,
properties: {}
})
this.props.buySellActions.createOrder({})
}
handleOnClickBack = (BASE: string) => {
this.props.analyticsActions.trackEvent({
key: Analytics.SELL_CHECKOUT_SCREEN_BACK_CLICKED,
properties: {}
})
this.props.buySellActions.setStep({
cryptoCurrency: BASE,
fiatCurrency: getFiatFromPair(this.props.pair.pair),
orderType: this.props.orderType,
pair: this.props.pair,
step: 'SELL_ENTER_AMOUNT',
swapAccount: this.props.account
})
}
networkFee = (value: PaymentValue | undefined) => (value ? getNetworkValue(value) : 0)
displayAmount = (formValues, account) => {
return coinToString({
unit: {
symbol: account.coin
},
value: formValues?.cryptoAmount
})
}
getFeeInFiat = (account: SwapAccountType, BASE, COUNTER) => {
const { payment, rates, ratesEth } = this.props
const isErc20 = window.coins[account.coin].coinfig.type.erc20Address
return (
(account.type === SwapBaseCounterTypes.ACCOUNT &&
Number(
Exchange.convertCoinToFiat({
coin: BASE,
currency: COUNTER,
isStandard: true,
rates: isErc20 ? ratesEth : rates,
value: convertBaseToStandard(account.baseCoin, this.networkFee(payment))
})
)) ||
0
)
}
toggleCoinToolTip = () => {
this.props.analyticsActions.trackEvent({
key: Analytics.SELL_PRICE_TOOLTIP_CLICKED,
properties: {}
})
this.setState((prevState) => ({
isSetCoinToolTip: !prevState.isSetCoinToolTip
}))
}
toggleNetworkFeeToolTip = () => {
this.props.analyticsActions.trackEvent({
key: Analytics.SELL_CHECKOUT_NETWORK_FEES_CLICKED,
properties: {}
})
this.setState((prevState) => ({
isSetNetworkFee: !prevState.isSetNetworkFee
}))
}
render() {
const { error } = this.props
if (isNabuError(error)) {
return <GenericNabuErrorFlyout error={error} onDismiss={this.props.clearErrors} />
}
return this.props.quoteR.cata({
Failure: () => null,
Loading: () => <Loading />,
NotAsked: () => <Loading />,
Success: (val) => {
const { account, formValues } = this.props
if (!formValues) return null
if (!account) return null
const BASE = getInputFromPair(val.quote.pair)
const COUNTER = getOutputFromPair(val.quote.pair)
const feeInFiat = this.getFeeInFiat(account, BASE, COUNTER)
const counterCoinTicker = COUNTER
const baseCoinTicker = BASE
const { rates, ratesEth } = this.props
const fiatCurrency = getFiatFromPair(this.props.pair.pair)
const isErc20 = window.coins[COUNTER].coinfig.type.erc20Address
const incomingCoinName = window.coins[counterCoinTicker]?.coinfig.name ?? counterCoinTicker
return (
<CustomForm onSubmit={this.handleSubmit}>
<FlyoutWrapper>
<TopText spaceBetween={false} marginBottom>
<Icon
role='button'
data-e2e='backToEnterAmount'
name='arrow-back'
cursor
size='24px'
color='grey600'
onClick={() => this.handleOnClickBack(BASE)}
/>{' '}
<Text size='20px' color='grey900' weight={600} style={{ marginLeft: '24px' }}>
<FormattedMessage
id='modals.simplebuy.checkoutconfirm'
defaultMessage='Checkout'
/>
</Text>
</TopText>
<QuoteCountDownWrapper>
<QuoteCountDown date={val.refreshConfig.date} totalMs={val.refreshConfig.totalMs} />
</QuoteCountDownWrapper>
<Amount data-e2e='sbTotalAmount'>
<div>
<Text size='32px' weight={600} color='grey800'>
{this.displayAmount(formValues, account)}
</Text>
</div>
<div>
<Text size='20px' weight={600} color='grey600' style={{ marginTop: '8px' }}>
{this.props.incomingAmountR.cata({
Failure: () => (
<Text size='14px' color='red600'>
<FormattedMessage
id='copy.oops'
defaultMessage='Oops. Something went wrong.'
/>
</Text>
),
Loading: () => <SkeletonRectangle height='18px' width='70px' />,
NotAsked: () => <SkeletonRectangle height='18px' width='70px' />,
Success: (success) => {
return (
<>
{counterCoinTicker}
{formatFiat(Number(success.amt) + Number(feeInFiat))}
</>
)
}
})}
</Text>
</div>
</Amount>
</FlyoutWrapper>
<RowItem>
<RowItemContainer>
<TopRow>
<RowIcon>
<RowText>
<FormattedMessage
id='modals.simplebuy.confirm.coin_price'
defaultMessage='{coin} Price'
values={{ coin: account.coin }}
/>
</RowText>
<IconWrapper>
<Icon
name='question-in-circle-filled'
size='16px'
color={this.state.isSetCoinToolTip ? 'blue600' : 'grey300'}
onClick={() => this.toggleCoinToolTip()}
/>
</IconWrapper>
</RowIcon>
<RowText data-e2e='sbExchangeRate'>
{this.props.quoteR.cata({
Failure: () => (
<Text size='14px' color='red600'>
<FormattedMessage
id='copy.oops'
defaultMessage='Oops. Something went wrong.'
/>
</Text>
),
Loading: () => <SkeletonRectangle height='18px' width='70px' />,
NotAsked: () => <SkeletonRectangle height='18px' width='70px' />,
Success: (success) => (
<>
{counterCoinTicker}
{formatFiat(convertBaseToStandard('FIAT', success.rate))}
</>
)
})}
</RowText>
</TopRow>
{this.state.isSetCoinToolTip && (
<ToolTipText>
<Text size='12px' weight={500} color='grey600'>
<TextGroup inline>
<Text size='14px'>
<FormattedMessage
id='modals.simplebuy.confirm.coin_tooltip'
defaultMessage='Blockchain.com provides the best market price we receive and applies a spread.'
/>
</Text>
<Link
href='https://support.blockchain.com/hc/en-us/articles/360061672651-Wallet-Pricing'
size='14px'
rel='noopener noreferrer'
target='_blank'
>
<FormattedMessage defaultMessage='Learn more' id='copy.learn_more' />
</Link>
</TextGroup>
</Text>
</ToolTipText>
)}
</RowItemContainer>
</RowItem>
<RowItem>
<RowText>
<FormattedMessage id='copy.deposit_to' defaultMessage='Deposit To' />
</RowText>
<Value data-e2e='sbIncomingAccount'>{incomingCoinName}</Value>
</RowItem>
{account.type !== SwapBaseCounterTypes.CUSTODIAL && (
<>
<RowItem>
<RowText>
<FormattedMessage
id='modals.simplebuy.confirm.sale_amount'
defaultMessage='Sale Amount'
/>
</RowText>
<RowText>
<RowTextWrapper>
{this.props.incomingAmountR.cata({
Failure: () => (
<Text size='14px' color='red600'>
<FormattedMessage
id='copy.oops'
defaultMessage='Oops. Something went wrong.'
/>
</Text>
),
Loading: () => <SkeletonRectangle height='18px' width='70px' />,
NotAsked: () => <SkeletonRectangle height='18px' width='70px' />,
Success: (success) => {
const saleAmount = formatFiat(Number(success.amt))
const saleInCoin = Exchange.convertFiatToCoin({
coin: BASE,
currency: fiatCurrency,
rates: window.coins[BASE].coinfig.type.erc20Address ? ratesEth : rates,
value: Number(saleAmount)
})
return (
<>
<Value data-e2e='sbSaleAccount'>
{counterCoinTicker}
{saleAmount}
</Value>
<AdditionalText>
{coinToString({
unit: {
symbol: account.coin
},
value: saleInCoin
})}
</AdditionalText>
</>
)
}
})}
</RowTextWrapper>
</RowText>
</RowItem>
<RowItem>
<RowItemContainer>
<TopRow>
<RowIcon>
<RowText>
<FormattedMessage id='copy.network_fee' defaultMessage='Network Fee' />
</RowText>
<IconWrapper>
<Icon
name='question-in-circle-filled'
size='16px'
color={this.state.isSetNetworkFee ? 'blue600' : 'grey300'}
onClick={() => this.toggleNetworkFeeToolTip()}
/>
</IconWrapper>
</RowIcon>
<RowText data-e2e='sbTransactionFee'>
<RowTextWrapper>
{counterCoinTicker}
{formatFiat(feeInFiat)}
<AdditionalText>
{coinToString({
unit: {
symbol: account.baseCoin
},
value: convertBaseToStandard(
account.baseCoin,
this.networkFee(this.props.payment)
)
})}
</AdditionalText>
</RowTextWrapper>
</RowText>
</TopRow>
{this.state.isSetNetworkFee && (
<ToolTipText>
<Text size='12px' weight={500} color='grey600'>
<TextGroup inline>
<Text size='14px'>
<FormattedMessage
id='modals.simplebuy.confirm.network_fees'
defaultMessage='Network fees are set by the {coin} network.'
values={{ coin: baseCoinTicker }}
/>
</Text>
<Link
href={
isErc20
? 'https://support.blockchain.com/hc/en-us/articles/360061258732'
: 'https://support.blockchain.com/hc/en-us/articles/360061672651'
}
size='14px'
rel='noopener noreferrer'
target='_blank'
>
<FormattedMessage
id='modals.simplebuy.confirm.learn_more_about_fees'
defaultMessage='Learn more about fees'
/>
</Link>
</TextGroup>
</Text>
</ToolTipText>
)}
</RowItemContainer>
</RowItem>
</>
)}
<RowItem>
<RowText>
<FormattedMessage id='copy.total' defaultMessage='Total' />
</RowText>
<RowText>
<RowTextWrapper>
<Value data-e2e='sbIncomingAmount'>
{this.props.incomingAmountR.cata({
Failure: () => (
<Text size='14px' color='red600'>
<FormattedMessage
id='copy.oops'
defaultMessage='Oops. Something went wrong.'
/>
</Text>
),
Loading: () => <SkeletonRectangle height='18px' width='70px' />,
NotAsked: () => <SkeletonRectangle height='18px' width='70px' />,
Success: (success) => {
return (
<>
{counterCoinTicker}
{formatFiat(Number(success.amt) + Number(feeInFiat))}
</>
)
}
})}
</Value>
<AdditionalText>{this.displayAmount(formValues, account)}</AdditionalText>
</RowTextWrapper>
</RowText>
</RowItem>
<Border />
<FlyoutWrapper>
<DisclaimerText>
<FormattedMessage
id='modals.simplebuy.confirm.sell_description'
defaultMessage='Final amount may change due to market activity.{linebreak} By approving this Sell you agree to Blockchain.com’s {linebreak}<a>Refund Policy.</a>'
values={{
a: (msg: string) => (
<a
href='https://www.blockchain.com/legal/terms'
rel='noopener noreferrer'
target='_blank'
>
{msg}
</a>
),
linebreak: <br />
}}
/>
</DisclaimerText>
</FlyoutWrapper>
<Bottom>
<BottomActions>
<SellButton
isSubmitting={this.props.submitting}
refreshConfig={val.refreshConfig}
/>
<Text
size='12px'
weight={500}
color='grey600'
style={{ marginTop: '16px', textAlign: 'center' }}
>
<FormattedMessage
id='copy.buy_amount_change_disclaimer'
defaultMessage='The amounts you send and receive may change slightly due to market activity. Once an order starts, we are unable to stop it.'
/>
</Text>
{this.props.error && (
<ErrorCartridge style={{ marginTop: '16px' }} data-e2e='checkoutError'>
<Icon name='alert-filled' color='red600' style={{ marginRight: '4px' }} />
{Number(this.props.error) ? (
<ErrorCodeMappings code={this.props.error} />
) : (
`Error: ${this.props.error}`
)}
</ErrorCartridge>
)}
</BottomActions>
</Bottom>
</CustomForm>
)
}
})
}
}
const mapStateToProps = (state: RootState) => {
const coin = selectors.components.buySell.getCryptoCurrency(state) as CoinType
const payment = selectors.components.buySell.getPayment(state).getOrElse(undefined)
return {
account: selectors.components.buySell.getSwapAccount(state),
coin,
formValues: selectors.form.getFormValues(FORM_BS_CHECKOUT)(state) as BSCheckoutFormValuesType,
incomingAmountR: selectors.components.buySell.getIncomingAmount(state),
pair: selectors.components.buySell.getBSPair(state),
payment,
quoteR: selectors.components.buySell.getSellQuote(state),
rates: selectors.core.data.misc.getRatesSelector(coin, state).getOrElse({} as RatesType),
ratesEth: selectors.core.data.misc.getRatesSelector('ETH', state).getOrElse({} as RatesType)
}
}
const mapDispatchToProps = (dispatch: Dispatch) => ({
analyticsActions: bindActionCreators(actions.analytics, dispatch),
buySellActions: bindActionCreators(actions.components.buySell, dispatch),
clearErrors: () => dispatch(clearSubmitErrors(FORM_BS_PREVIEW_SELL))
})
const connector = connect(mapStateToProps, mapDispatchToProps)
const enhance = compose(
reduxForm<{ destroyOnUnmount: boolean; form: string }, Props>({
destroyOnUnmount: false,
form: FORM_BS_PREVIEW_SELL
}),
connector
)
type State = {
isSetCoinToolTip: boolean
isSetNetworkFee: boolean
}
type OwnProps = {
handleClose: () => void
orderType: BSOrderActionType
pair: BSPairType
}
export type Props = OwnProps & ConnectedProps<typeof connector>
export default enhance(PreviewSell) as React.ComponentClass<OwnProps>