-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathAuctionBidRoute.tsx
More file actions
306 lines (279 loc) · 8.25 KB
/
AuctionBidRoute.tsx
File metadata and controls
306 lines (279 loc) · 8.25 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
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
import {
Button,
Join,
ModalDialog,
Select,
Spacer,
Text,
useDidMount,
} from "@artsy/palette"
import { ArtworkSidebarAuctionTimerFragmentContainer } from "Apps/Artwork/Components/ArtworkSidebar/ArtworkSidebarAuctionTimer"
import { AddressFormWithCreditCard } from "Apps/Auction/Components/Form/AddressFormWithCreditCard"
import { ConditionsOfSaleCheckbox } from "Apps/Auction/Components/Form/ConditionsOfSaleCheckbox"
import { ErrorStatus } from "Apps/Auction/Components/Form/ErrorStatus"
import { getSelectedBid } from "Apps/Auction/Components/Form/Utils/getSelectedBid"
import { initialValuesForBidding } from "Apps/Auction/Components/Form/Utils/initialValues"
import { biddingValidationSchemas } from "Apps/Auction/Components/Form/Utils/validationSchemas"
import { useAuctionTracking } from "Apps/Auction/Hooks/useAuctionTracking"
import { CreditCardInputProvider } from "Components/CreditCardInput"
import { useRouter } from "System/Hooks/useRouter"
import type { AuctionBidRoute_artwork$data } from "__generated__/AuctionBidRoute_artwork.graphql"
import type { AuctionBidRoute_me$data } from "__generated__/AuctionBidRoute_me.graphql"
import type { AuctionBidRoute_sale$data } from "__generated__/AuctionBidRoute_sale.graphql"
import { Form, Formik } from "formik"
import type { Match } from "found"
import dropWhile from "lodash/dropWhile"
import { useEffect } from "react"
import {
type RelayRefetchProp,
createRefetchContainer,
graphql,
} from "react-relay"
import { AuctionLotInfoFragmentContainer } from "./Components/AuctionLotInfo"
import { PricingTransparencyQueryRenderer } from "./Components/PricingTransparency"
import { useSubmitBid } from "./useSubmitBid"
interface AuctionBidRouteProps {
artwork: AuctionBidRoute_artwork$data
me: AuctionBidRoute_me$data
relay: RelayRefetchProp
sale: AuctionBidRoute_sale$data
}
const AuctionBidRoute: React.FC<
React.PropsWithChildren<AuctionBidRouteProps>
> = ({ artwork, me, relay, sale }) => {
const { match, router } = useRouter()
const { tracking } = useAuctionTracking()
const mounted = useDidMount()
const {
artworkSlug,
bidderID,
displayIncrements,
modalWidth,
requiresCheckbox,
requiresPaymentInformation,
selectedBid,
validationSchema,
} = computeProps({ artwork, match, me })
const { submitBid } = useSubmitBid({
artwork,
bidderID,
me,
relay,
requiresPaymentInformation,
sale,
})
const handleBidSubmit = async (values, helpers) => {
try {
await submitBid(values, helpers)
} catch (error) {
// TODO: Connect to system errors
console.error("handleBidSubmit", error)
}
}
const handleModalClose = () => {
router.push(`/auction/${sale.slug}`)
}
// Track initial load
useEffect(() => {
tracking.bidPageView({ artwork, me })
}, [artwork, me, tracking])
if (!mounted) return null
return (
<ModalDialog
title="Confirm Your Bid"
onClose={handleModalClose}
width={modalWidth}
>
<Formik
validateOnMount
initialValues={{
...initialValuesForBidding,
agreeToTerms: requiresPaymentInformation ? false : true,
creditCard: requiresPaymentInformation ? false : true,
selectedBid,
}}
validationSchema={validationSchema}
onSubmit={handleBidSubmit}
>
{({
values,
touched,
errors,
isSubmitting,
isValid,
setFieldError,
setFieldValue,
setFieldTouched,
}) => {
return (
<Form>
<Join separator={<Spacer y={2} />}>
<AuctionLotInfoFragmentContainer
saleArtwork={artwork.saleArtwork!}
/>
<ArtworkSidebarAuctionTimerFragmentContainer
artwork={artwork}
/>
<Text variant="lg-display">Set Your Max Bid</Text>
<Select
selected={values.selectedBid}
onSelect={value => {
tracking.maxBidSelected({
bidderID: bidderID!,
maxBid: value,
})
setFieldError("selectedBid", undefined)
setFieldValue("selectedBid", value)
setFieldTouched("selectedBid")
}}
options={displayIncrements}
error={touched.selectedBid && (errors.selectedBid as string)}
/>
<PricingTransparencyQueryRenderer
saleId={artwork.saleArtwork?.sale?.slug!}
artworkId={artworkSlug}
/>
{requiresPaymentInformation && <AddressFormWithCreditCard />}
{requiresCheckbox && <ConditionsOfSaleCheckbox />}
<Button
width="100%"
loading={isSubmitting}
disabled={!isValid}
type="submit"
>
Confirm Bid
</Button>
<ErrorStatus />
</Join>
</Form>
)
}}
</Formik>
</ModalDialog>
)
}
export const AuctionBidRouteFragmentContainer = createRefetchContainer(
(props: AuctionBidRouteProps) => {
return (
// Wrap the provider down here as we need it for our hooks
<CreditCardInputProvider>
<AuctionBidRoute {...props} />
</CreditCardInputProvider>
)
},
{
sale: graphql`
fragment AuctionBidRoute_sale on Sale {
internalID
slug
}
`,
artwork: graphql`
fragment AuctionBidRoute_artwork on Artwork {
slug
internalID
...ArtworkSidebarAuctionTimer_artwork
saleArtwork {
...AuctionLotInfo_saleArtwork
@arguments(imageWidth: 150, imageHeight: 150)
minimumNextBid {
cents
}
increments(useMyMaxBid: true) {
cents
display
}
sale {
internalID
bidder {
id
}
slug
registrationStatus {
qualifiedForBidding
}
}
}
}
`,
me: graphql`
fragment AuctionBidRoute_me on Me {
internalID
hasQualifiedCreditCards
}
`,
},
graphql`
query AuctionBidRouteQuery($artworkID: String!, $saleID: String!) {
artwork(id: $artworkID) {
...AuctionBidRoute_artwork
}
me {
...AuctionBidRoute_me
}
sale(id: $saleID) {
...AuctionBidRoute_sale
}
}
`,
)
const computeProps = ({
artwork,
match,
me,
}: {
artwork: AuctionBidRoute_artwork$data
match: Match
me: AuctionBidRoute_me$data
}) => {
const artworkSlug = match.params.artworkSlug
const bidder = artwork.saleArtwork?.sale?.bidder
const bidderID = bidder?.id
const displayIncrements = dropWhile(
artwork.saleArtwork?.increments,
increment => {
// @ts-ignore
return increment.cents < artwork.saleArtwork?.minimumNextBid!.cents!
},
).map(increment => {
return {
value: increment!.cents!.toString(),
text: increment!.display!,
}
})
const initialSelectedBid = match?.location?.query?.bid
const selectedBid = getSelectedBid({
initialSelectedBid,
displayIncrements,
})
const requiresCheckbox = !bidder
const requiresPaymentInformation =
requiresCheckbox && !me.hasQualifiedCreditCards
const {
validationSchemaForRegisteredUsers,
validationSchemaForUnregisteredUsersWithCreditCard,
validationSchemaForUnregisteredUsersWithoutCreditCard,
} = biddingValidationSchemas
const validationSchema = (() => {
if (requiresCheckbox || requiresPaymentInformation) {
if (requiresPaymentInformation) {
return validationSchemaForUnregisteredUsersWithoutCreditCard
} else {
return validationSchemaForUnregisteredUsersWithCreditCard
}
} else {
return validationSchemaForRegisteredUsers
}
})()
const modalWidth = requiresPaymentInformation ? ["100%", 600] : null
return {
artworkSlug,
bidderID: bidderID!,
displayIncrements,
modalWidth,
requiresCheckbox,
requiresPaymentInformation,
selectedBid,
validationSchema,
}
}