-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathSavedPaymentMethodOption.tsx
More file actions
153 lines (141 loc) · 4.74 KB
/
SavedPaymentMethodOption.tsx
File metadata and controls
153 lines (141 loc) · 4.74 KB
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
import InstitutionIcon from "@artsy/icons/InstitutionIcon"
import LockIcon from "@artsy/icons/LockIcon"
import { Box, Flex, Radio, RadioGroup, Spacer, Text } from "@artsy/palette"
import { themeGet } from "@styled-system/theme-get"
import { Collapse } from "Apps/Order/Components/Collapse"
import { type Brand, BrandCreditCardIcon } from "Components/BrandCreditCardIcon"
import { FadeInBox } from "Components/FadeInBox"
import type { Order2PaymentForm_me$data } from "__generated__/Order2PaymentForm_me.graphql"
import type React from "react"
import styled from "styled-components"
interface SavedPaymentMethodOptionProps {
me: Order2PaymentForm_me$data
isSelected: boolean
selectedSavedPaymentMethod: any | null
allowedSavedBankAccounts: any[]
onSelect: () => void
onSavedPaymentMethodSelect: (paymentMethod: any) => void
}
export const SavedPaymentMethodOption: React.FC<
SavedPaymentMethodOptionProps
> = ({
me,
isSelected,
selectedSavedPaymentMethod,
allowedSavedBankAccounts,
onSelect,
onSavedPaymentMethodSelect,
}) => {
const savedCreditCards =
me.creditCards?.edges?.map(edge => edge?.node).filter(Boolean) ?? []
return (
<FadeInBox>
<Box
as="button"
type="button"
backgroundColor="mono5"
borderRadius="5px"
padding="1rem"
marginBottom="10px"
border={isSelected ? "1px solid" : "none"}
borderColor="mono10"
width="100%"
style={{ cursor: "pointer", appearance: "none", textAlign: "left" }}
onClick={onSelect}
aria-expanded={isSelected}
>
<HoverFlex alignItems="center">
<HoverIcon />
{/* Spacer has to be 31px to match Stripe's spacing */}
<Spacer x="31px" />
<HoverText variant="sm" fontWeight={isSelected ? "bold" : "normal"}>
Saved payments
</HoverText>
</HoverFlex>
<Collapse open={isSelected}>
<Spacer y={1} />
<Text variant="sm">
Select a saved payment method or add a new one.
</Text>
<Spacer y={2} />
<RadioGroup
gap={2}
defaultValue={selectedSavedPaymentMethod}
onSelect={val => {
onSavedPaymentMethodSelect(val)
}}
>
{[...savedCreditCards, ...allowedSavedBankAccounts].map(
paymentMethod => {
const formattedExpDate =
paymentMethod.__typename === "CreditCard"
? `${paymentMethod.expirationMonth
.toString()
.padStart(2, "0")}/${paymentMethod.expirationYear
.toString()
.slice(-2)}`
: null
return (
<Radio
key={paymentMethod.internalID}
value={paymentMethod}
label={
<Flex>
{paymentMethod.__typename === "CreditCard" ? (
<>
<BrandCreditCardIcon
type={paymentMethod.brand as Brand}
width="24px"
height="24px"
flexShrink={0}
mr={1}
/>
<Text variant="sm">
•••• {paymentMethod.lastDigits}
{formattedExpDate && ` Exp ${formattedExpDate}`}
</Text>
</>
) : (
<>
<InstitutionIcon
fill="mono100"
width="24px"
height="24px"
flexShrink={0}
mr={1}
/>
<Text variant="sm">
{paymentMethod.bankName &&
`${paymentMethod.bankName} `}
•••• {paymentMethod.last4}
</Text>
</>
)}
</Flex>
}
/>
)
},
)}
</RadioGroup>
</Collapse>
</Box>
</FadeInBox>
)
}
const HoverText = styled(Text)`
transition: color 0.25s;
`
const HoverIcon = styled(LockIcon)`
svg {
transition: fill 0.25s;
}
`
const HoverFlex = styled(Flex)`
&:hover ${HoverText} {
color: ${themeGet("colors.mono100")};
}
&:hover ${HoverIcon} svg {
fill: ${themeGet("colors.mono100")};
}
`