-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathOrder2OfferStep.tsx
More file actions
418 lines (374 loc) · 11.5 KB
/
Order2OfferStep.tsx
File metadata and controls
418 lines (374 loc) · 11.5 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
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
import { ContextModule } from "@artsy/cohesion"
import { Box, Button, Flex, Spacer, Text, TextArea } from "@artsy/palette"
import { validateAndExtractOrderResponse } from "Apps/Order/Components/ExpressCheckout/Util/mutationHandling"
import { SectionHeading } from "Apps/Order2/Components/SectionHeading"
import {
CheckoutStepName,
CheckoutStepState,
} from "Apps/Order2/Routes/Checkout/CheckoutContext/types"
import {
CheckoutErrorBanner,
type CheckoutErrorBannerMessage,
fallbackError,
} from "Apps/Order2/Routes/Checkout/Components/CheckoutErrorBanner"
import { OfferInput } from "Apps/Order2/Routes/Checkout/Components/OfferStep/Components/OfferInput"
import { Order2OfferOptions } from "Apps/Order2/Routes/Checkout/Components/OfferStep/Components/Order2OfferOptions"
import {
Order2OfferCompletedView,
type Order2OfferCompletedViewProps,
} from "Apps/Order2/Routes/Checkout/Components/OfferStep/Order2OfferCompletedView"
import type { OfferNoteValue } from "Apps/Order2/Routes/Checkout/Components/OfferStep/types"
import { useCompleteOfferData } from "Apps/Order2/Routes/Checkout/Components/OfferStep/useCompleteOfferData"
import { useCheckoutContext } from "Apps/Order2/Routes/Checkout/Hooks/useCheckoutContext"
import { useScrollToErrorBanner } from "Apps/Order2/Routes/Checkout/Hooks/useScrollToErrorBanner"
import { useOrder2AddInitialOfferMutation } from "Apps/Order2/Routes/Checkout/Mutations/useOrder2AddInitialOfferMutation"
import { useOrder2UnsetOrderFulfillmentOptionMutation } from "Apps/Order2/Routes/Checkout/Mutations/useOrder2UnsetOrderFulfillmentOptionMutation"
import createLogger from "Utils/logger"
import type {
Order2OfferStep_order$data,
Order2OfferStep_order$key,
} from "__generated__/Order2OfferStep_order.graphql"
import type { useOrder2AddInitialOfferMutation$data } from "__generated__/useOrder2AddInitialOfferMutation.graphql"
import { Formik, type FormikConfig, useFormikContext } from "formik"
import { useState } from "react"
import { graphql, useFragment } from "react-relay"
import * as yup from "yup"
const logger = createLogger(
"Order2/Routes/Checkout/Components/OfferStep/Order2OfferStep.tsx",
)
const offerError = (code?: string): CheckoutErrorBannerMessage => {
if (code) {
return fallbackError("selecting your offer amount", code)
}
return {
title: "Offer amount required",
message: "Select an offer amount or enter your own to continue.",
}
}
interface OfferFormValues {
offerValue: number
offerNote: string
}
interface Order2OfferStepProps {
order: Order2OfferStep_order$key
}
interface Order2OfferStepFormContentProps {
orderData: Order2OfferStep_order$data
offerAmountError?: CheckoutErrorBannerMessage | null
currentStep: CheckoutStepState | undefined
completedViewProps: Order2OfferCompletedViewProps | null
isSubmittingOffer: boolean
}
export const Order2OfferStep: React.FC<Order2OfferStepProps> = ({ order }) => {
const orderData = useFragment(FRAGMENT, order)
const {
steps,
setOfferAmountComplete,
checkoutTracking,
messages,
setSectionErrorMessage,
} = useCheckoutContext()
const offerAmountError = messages[CheckoutStepName.OFFER_AMOUNT]?.error
const { submitMutation: submitOfferMutation } =
useOrder2AddInitialOfferMutation()
const unsetOrderFulfillmentOption =
useOrder2UnsetOrderFulfillmentOptionMutation()
const { pendingOffer } = orderData
const [isSubmittingOffer, setIsSubmittingOffer] = useState(false)
const currentStep = steps?.find(
step => step.name === CheckoutStepName.OFFER_AMOUNT,
)?.state
const completedViewProps = useCompleteOfferData(orderData)
const validationSchema = yup.object().shape({
offerValue: yup
.number()
.test(
"is-required",
"Offer amount is required",
value => value !== undefined && value !== 0,
),
offerNote: yup.string().max(1000, "Note cannot exceed 1000 characters"),
})
const handleSubmit: FormikConfig<OfferFormValues>["onSubmit"] = async ({
offerValue,
offerNote,
}) => {
const handleSubmitError = (error: { code: string }) => {
logger.error(error)
setSectionErrorMessage({
section: CheckoutStepName.OFFER_AMOUNT,
error: offerError(error.code),
})
}
try {
setIsSubmittingOffer(true)
checkoutTracking.clickedOrderProgression(ContextModule.ordersOffer)
// Unset the current fulfillment option if it exists
if (orderData.selectedFulfillmentOption?.type) {
const unsetFulfillmentOptionResult =
await unsetOrderFulfillmentOption.submitMutation({
variables: {
input: {
id: orderData.internalID,
},
},
})
validateAndExtractOrderResponse(
unsetFulfillmentOptionResult.unsetOrderFulfillmentOption
?.orderOrError,
)
}
const response: useOrder2AddInitialOfferMutation$data =
await submitOfferMutation({
variables: {
input: {
amountMinor: offerValue * 100,
note: offerNote,
orderID: orderData.internalID,
},
},
})
const offerOrError = response.createBuyerOffer?.offerOrError
if (offerOrError && "mutationError" in offerOrError) {
handleSubmitError({
code: offerOrError.mutationError?.code || "unknown",
})
return
}
if (offerOrError && "offer" in offerOrError) {
setOfferAmountComplete()
return
}
throw new Error("Unexpected response from offer mutation")
} catch (error) {
handleSubmitError({ code: "unknown" })
} finally {
setIsSubmittingOffer(false)
}
}
return (
<Formik<OfferFormValues>
initialValues={{
offerValue: pendingOffer?.amount?.major || 0,
offerNote: pendingOffer?.note || "",
}}
validationSchema={validationSchema}
onSubmit={handleSubmit}
validateOnChange={false}
validateOnBlur={false}
>
<Order2OfferStepFormContent
orderData={orderData}
offerAmountError={offerAmountError}
currentStep={currentStep}
completedViewProps={completedViewProps}
isSubmittingOffer={isSubmittingOffer}
/>
</Formik>
)
}
const Order2OfferStepFormContent: React.FC<Order2OfferStepFormContentProps> = ({
orderData,
offerAmountError,
currentStep,
completedViewProps,
isSubmittingOffer,
}) => {
const { values, errors, setFieldValue, submitForm } =
useFormikContext<OfferFormValues>()
const { setSectionErrorMessage, checkoutTracking } = useCheckoutContext()
const errorBannerRef = useScrollToErrorBanner(CheckoutStepName.OFFER_AMOUNT)
const clearOfferError = () => {
setSectionErrorMessage({
section: CheckoutStepName.OFFER_AMOUNT,
error: null,
})
}
const trackOfferOption = ({
value,
description,
}: {
value: number
description?: string
}) => {
if (value !== undefined && value > 0) {
checkoutTracking.clickedOfferOption(
orderData.currencyCode,
orderData.internalID,
value,
description,
)
}
}
const onOfferOptionSelected = (value: number, description?: string) => {
setFieldValue("offerValue", value)
clearOfferError()
trackOfferOption({ value, description })
}
const onCustomOfferBlur = (value: number) => {
trackOfferOption({ value, description: "Custom amount" })
}
const onOfferNoteChange = (noteValue: OfferNoteValue) => {
setFieldValue("offerNote", noteValue.value)
}
const onContinueButtonPressed = async () => {
if (values.offerValue === undefined || values.offerValue === 0) {
setFieldValue("offerValue", values.offerValue, true)
setSectionErrorMessage({
section: CheckoutStepName.OFFER_AMOUNT,
error: offerError(),
})
return
}
if (errors.offerNote) {
return
}
submitForm()
}
const isPriceHidden = orderData.lineItems?.[0]?.artwork?.isPriceHidden
return (
<Flex
flexDirection="column"
data-testid="offer-step-active"
backgroundColor="mono0"
>
<Box
py={2}
px={[2, 2, 4]}
hidden={currentStep !== CheckoutStepState.UPCOMING}
>
<Flex flexDirection="column">
<SectionHeading>Offer</SectionHeading>
<Text variant="sm" color="mono60">
If accepted, your payment will be processed. All offers are binding
once submitted.
</Text>
</Flex>
</Box>
<Box
py={2}
px={[2, 2, 4]}
hidden={currentStep !== CheckoutStepState.COMPLETED}
>
{completedViewProps && (
<Order2OfferCompletedView {...completedViewProps} />
)}
</Box>
<Box
pt={2}
px={[2, 2, 4]}
hidden={currentStep !== CheckoutStepState.ACTIVE}
>
<Flex flexDirection="column">
<SectionHeading>Offer</SectionHeading>
<Text variant="sm" color="mono100">
If accepted, your payment will be processed. All offers are binding
once submitted.
</Text>
<Spacer y={1} />
{offerAmountError && (
<CheckoutErrorBanner
ref={errorBannerRef}
error={offerAmountError}
analytics={{ flow: "User setting offer" }}
/>
)}
</Flex>
</Box>
<Box
pt={0}
pb={2}
px={[2, 2, 4]}
hidden={currentStep !== CheckoutStepState.ACTIVE}
>
{isPriceHidden ? (
<>
<OfferInput
name="offerValue"
onBlur={onCustomOfferBlur}
order={orderData}
showCurrencySymbol
/>
<Spacer y={4} />
</>
) : (
<>
<Order2OfferOptions
order={orderData}
onOfferOptionSelected={onOfferOptionSelected}
onCustomOfferBlur={onCustomOfferBlur}
/>
<Spacer y={2} />
</>
)}
<Flex flexDirection="column">
<SectionHeading>Offer note</SectionHeading>
<Text variant="sm" color="mono100">
Additional context to help the gallery evaluate your offer.
</Text>
<TextArea
title="Note (recommended)"
maxLength={1000}
placeholder="Share what draws you to this work or artist, or add any context about your offer"
onChange={onOfferNoteChange}
value={values.offerNote}
/>
<Spacer y={4} />
<Button
variant="primaryBlack"
width="100%"
onClick={onContinueButtonPressed}
loading={isSubmittingOffer}
disabled={isSubmittingOffer}
>
Save and Continue
</Button>
</Flex>
</Box>
</Flex>
)
}
const FRAGMENT = graphql`
fragment Order2OfferStep_order on Order {
...useCompleteOfferData_order
...Order2OfferOptions_order
...OfferInput_order
internalID
mode
source
currencyCode
selectedFulfillmentOption {
type
}
pendingOffer {
amount {
display
major
}
note
}
lineItems {
artwork {
slug
isPriceHidden
listPrice {
__typename
... on Money {
major
}
... on PriceRange {
maxPrice {
major
}
minPrice {
major
}
}
}
editionSets {
internalID
}
}
}
}
`