-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathCheckoutButton.tsx
79 lines (76 loc) · 2.19 KB
/
CheckoutButton.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
import { useMemo } from 'react';
import { Spinner } from '../../internal/components/Spinner';
import { useIcon } from '../../internal/hooks/useIcon';
import {
border,
cn,
color,
pressable,
text as styleText,
} from '../../styles/theme';
import { CHECKOUT_LIFECYCLESTATUS } from '../constants';
import type { CheckoutButtonReact } from '../types';
import { useCheckoutContext } from './CheckoutProvider';
export function CheckoutButton({
className,
coinbaseBranded,
disabled,
icon,
text = 'Pay',
}: CheckoutButtonReact) {
if (coinbaseBranded) {
icon = 'coinbasePay';
text = 'Pay with Crypto';
}
const { lifecycleStatus, onSubmit } = useCheckoutContext();
const iconSvg = useIcon({ icon });
const isLoading =
lifecycleStatus?.statusName === CHECKOUT_LIFECYCLESTATUS.PENDING;
const isFetchingData =
lifecycleStatus?.statusName === CHECKOUT_LIFECYCLESTATUS.FETCHING_DATA;
const isDisabled = disabled || isLoading || isFetchingData;
const buttonText = useMemo(() => {
if (lifecycleStatus?.statusName === CHECKOUT_LIFECYCLESTATUS.SUCCESS) {
return 'View payment details';
}
return text;
}, [lifecycleStatus?.statusName, text]);
const shouldRenderIcon = buttonText === text && iconSvg;
return (
<button
className={cn(
coinbaseBranded ? pressable.coinbaseBranding : pressable.primary,
border.radius,
isDisabled && pressable.disabled,
styleText.headline,
'mt-4 w-full px-4 py-3',
className,
)}
onClick={onSubmit}
type="button"
disabled={isDisabled}
>
<div className="flex items-center justify-center whitespace-nowrap">
{isLoading ? (
<Spinner className="h-5 w-5" />
) : (
<>
{shouldRenderIcon && (
<div className="mr-2 flex h-5 w-5 shrink-0 items-center justify-center">
{iconSvg}
</div>
)}
<span
className={cn(
styleText.headline,
coinbaseBranded ? 'text-gray-50' : color.inverse,
)}
>
{buttonText}
</span>
</>
)}
</div>
</button>
);
}