tf-checkout-react 1.0.103 → 1.0.106

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/dist/api/index.d.ts +3 -1
  2. package/dist/components/common/CustomField.d.ts +1 -1
  3. package/dist/components/common/DatePickerField.d.ts +14 -0
  4. package/dist/components/common/RedirectModal.d.ts +7 -0
  5. package/dist/components/countdown/index.d.ts +3 -1
  6. package/dist/components/orderDetailsContainer/ticketsTable.d.ts +2 -0
  7. package/dist/components/rsvpContainer/index.d.ts +7 -0
  8. package/dist/components/ticketResale/index.d.ts +5 -3
  9. package/dist/components/ticketsContainer/AccessCodeSection.d.ts +7 -0
  10. package/dist/components/ticketsContainer/PromoCodeSection.d.ts +5 -9
  11. package/dist/components/ticketsContainer/TicketsSection.d.ts +6 -3
  12. package/dist/components/ticketsContainer/index.d.ts +9 -2
  13. package/dist/components/timerWidget/index.d.ts +3 -3
  14. package/dist/images/cross.svg +44 -0
  15. package/dist/images/done.svg +3 -3
  16. package/dist/index.d.ts +2 -0
  17. package/dist/tf-checkout-react.cjs.development.js +639 -223
  18. package/dist/tf-checkout-react.cjs.development.js.map +1 -1
  19. package/dist/tf-checkout-react.cjs.production.min.js +1 -1
  20. package/dist/tf-checkout-react.cjs.production.min.js.map +1 -1
  21. package/dist/tf-checkout-react.esm.js +645 -231
  22. package/dist/tf-checkout-react.esm.js.map +1 -1
  23. package/dist/tf-checkout-styles.css +1 -1
  24. package/package.json +89 -89
  25. package/src/.DS_Store +0 -0
  26. package/src/.d.ts +2 -2
  27. package/src/api/index.ts +300 -278
  28. package/src/assets/images/cross.svg +44 -0
  29. package/src/assets/images/done.svg +3 -3
  30. package/src/components/.DS_Store +0 -0
  31. package/src/components/billing-info-container/index.tsx +811 -799
  32. package/src/components/billing-info-container/style.css +105 -105
  33. package/src/components/billing-info-container/utils.ts +225 -224
  34. package/src/components/common/CheckboxField.tsx +41 -41
  35. package/src/components/common/CustomField.tsx +87 -84
  36. package/src/components/common/DatePickerField.tsx +98 -0
  37. package/src/components/common/FormikPhoneNumberField.tsx +51 -51
  38. package/src/components/common/Loader.tsx +9 -9
  39. package/src/components/common/RadioField.tsx +35 -35
  40. package/src/components/common/RedirectModal.tsx +43 -0
  41. package/src/components/common/SelectField.tsx +80 -80
  42. package/src/components/common/SnackbarAlert.tsx +53 -53
  43. package/src/components/common/index.tsx +4 -4
  44. package/src/components/confirmModal/index.tsx +51 -51
  45. package/src/components/confirmModal/style.css +21 -21
  46. package/src/components/confirmationContainer/config.ts +72 -72
  47. package/src/components/confirmationContainer/index.tsx +197 -197
  48. package/src/components/confirmationContainer/social-buttons.tsx +94 -94
  49. package/src/components/confirmationContainer/style.css +202 -202
  50. package/src/components/countdown/index.tsx +98 -89
  51. package/src/components/countdown/style.css +9 -9
  52. package/src/components/index.ts +7 -7
  53. package/src/components/loginModal/index.tsx +209 -209
  54. package/src/components/loginModal/style.css +71 -71
  55. package/src/components/myTicketsContainer/index.tsx +196 -196
  56. package/src/components/myTicketsContainer/row.tsx +41 -41
  57. package/src/components/myTicketsContainer/style.css +39 -39
  58. package/src/components/myTicketsContainer/tableConfig.tsx +34 -34
  59. package/src/components/orderDetailsContainer/index.tsx +252 -249
  60. package/src/components/orderDetailsContainer/style.css +72 -72
  61. package/src/components/orderDetailsContainer/ticketsTable.tsx +130 -124
  62. package/src/components/paymentContainer/index.tsx +285 -284
  63. package/src/components/registerModal/index.tsx +190 -190
  64. package/src/components/rsvpContainer/index.tsx +126 -0
  65. package/src/components/stripePayment/index.tsx +254 -253
  66. package/src/components/stripePayment/style.css +59 -59
  67. package/src/components/ticketResale/index.tsx +74 -56
  68. package/src/components/ticketResaleModal/index.tsx +215 -210
  69. package/src/components/ticketResaleModal/style.css +28 -28
  70. package/src/components/ticketsContainer/AccessCodeSection.tsx +50 -0
  71. package/src/components/ticketsContainer/PromoCodeSection.tsx +88 -99
  72. package/src/components/ticketsContainer/ReferralLogic.tsx +31 -33
  73. package/src/components/ticketsContainer/TicketRow.tsx +83 -83
  74. package/src/components/ticketsContainer/TicketsSection.tsx +90 -81
  75. package/src/components/ticketsContainer/index.tsx +464 -430
  76. package/src/components/ticketsContainer/style.css +181 -181
  77. package/src/components/ticketsContainer/utils.ts +11 -11
  78. package/src/components/timerWidget/index.tsx +87 -70
  79. package/src/components/timerWidget/style.css +34 -26
  80. package/src/components/waitingList/index.tsx +178 -178
  81. package/src/components/waitingList/style.css +26 -26
  82. package/src/env.ts +20 -20
  83. package/src/index.ts +15 -13
  84. package/src/normalizers/index.ts +45 -45
  85. package/src/types/billing-info-data.ts +37 -37
  86. package/src/types/payment-field.ts +7 -7
  87. package/src/types/referral-promotion.ts +7 -7
  88. package/src/utils/createCheckoutDataBodyWithDefaultHolder.ts +59 -59
  89. package/src/utils/downloadPDF.tsx +30 -30
  90. package/src/utils/formikErrorFocus.ts +24 -24
  91. package/src/utils/getImage.ts +14 -14
  92. package/src/utils/getQueryVariable.ts +13 -13
  93. package/src/utils/index.ts +5 -5
  94. package/src/utils/setConfigs.ts +26 -26
  95. package/src/utils/showZero.tsx +10 -10
  96. package/src/validators/index.ts +20 -20
@@ -1,799 +1,811 @@
1
- import React, { FC, useEffect, useState, useRef } from 'react'
2
- import { AxiosError } from 'axios'
3
- import {
4
- Field,
5
- Form,
6
- Formik,
7
- FormikHelpers,
8
- FormikProps,
9
- FormikValues,
10
- } from 'formik'
11
- import Button from '@mui/material/Button'
12
- import _identity from 'lodash/identity'
13
- import _map from 'lodash/map'
14
- import _get from 'lodash/get'
15
- import _includes from 'lodash/includes'
16
- import _isEqual from 'lodash/isEqual'
17
- import _isEmpty from 'lodash/isEmpty'
18
-
19
- import './style.css'
20
- import { combineValidators, requiredValidator } from '../../validators'
21
- import { IBillingInfoData } from '../../types'
22
- import {
23
- getCart,
24
- getCountries,
25
- getProfileData,
26
- handleSetAccessToken,
27
- postOnCheckout,
28
- register,
29
- setCustomHeader,
30
- getStates,
31
- } from '../../api'
32
- import { LoginModal } from '../loginModal'
33
- import { RegisterModal } from '../registerModal'
34
- import {
35
- assingUniqueIds,
36
- createCheckoutDataBody,
37
- createRegisterFormData,
38
- getInitialValues,
39
- getValidateFunctions,
40
- setLoggedUserData,
41
- } from './utils'
42
- import { createCheckoutDataBodyWithDefaultHolder } from '../../utils'
43
- import axios from 'axios'
44
- import { CustomField, CheckboxField, SelectField, FormikPhoneNumberField } from '../common/index'
45
- import { CircularProgress, ThemeOptions } from '@mui/material'
46
- import { nanoid } from 'nanoid'
47
- import { getQueryVariable } from '../../utils/getQueryVariable'
48
- import { ErrorFocus } from '../../utils/formikErrorFocus'
49
- import { ThemeProvider, createTheme } from '@mui/material/styles'
50
- import { CSSProperties } from '@mui/styles'
51
- import Backdrop from '@mui/material/Backdrop'
52
- import TimerWidget from '../timerWidget'
53
- import SnackbarAlert from '../common/SnackbarAlert'
54
-
55
- export interface IBillingInfoPage {
56
- data?: IBillingInfoData[];
57
- ticketHoldersFields?: IBillingInfoData;
58
- handleSubmit?: (
59
- values: FormikValues,
60
- formikHelpers: FormikHelpers<FormikValues>,
61
- eventId: any,
62
- res: any
63
- ) => void;
64
- onRegisterSuccess?: (value: {
65
- accessToken: string;
66
- refreshToken: string;
67
- }) => void;
68
- onRegisterError?: (e: AxiosError, email: string) => void;
69
- onSubmitError?: (e: AxiosError) => void;
70
- onGetCartSuccess?: (res: any) => void;
71
- onGetCartError?: (e: AxiosError) => void;
72
- onGetCountriesSuccess?: (res: any) => void;
73
- onGetCountriesError?: (e: AxiosError) => void;
74
- onGetStatesSuccess?: (res: any) => void;
75
- onGetStatesError?: (e: AxiosError) => void;
76
- onGetProfileDataSuccess?: (res: any) => void;
77
- onGetProfileDataError?: (e: AxiosError) => void;
78
- onAuthorizeSuccess?: () => void;
79
- onAuthorizeError?: (e: AxiosError) => void;
80
- onLogin?: () => void;
81
- onLoginSuccess?: () => void;
82
- onErrorClose?: () => void;
83
- initialValues?: FormikValues;
84
- buttonName?: string;
85
- theme?: 'light' | 'dark';
86
- isLoggedIn?: boolean;
87
- accountInfoTitle?: string | JSX.Element;
88
- hideLogo?: boolean;
89
- themeOptions?: ThemeOptions & {
90
- input?: CSSProperties;
91
- checkbox?: CSSProperties;
92
- };
93
- hideErrorsAlertSection?: boolean;
94
- onSkipBillingPage: (data: any) => void;
95
- skipPage?: boolean;
96
- canSkipHolderNames?: boolean;
97
- onCountdownFinish?: () => void;
98
- enableTimer?: boolean;
99
- }
100
-
101
- const LogicRunner: FC<{
102
- values: any;
103
- setStates: React.Dispatch<any>;
104
- setFieldValue: any;
105
- setValues: any;
106
- setUserValues: any;
107
- onGetStatesSuccess: any;
108
- onGetStatesError: any;
109
- }> = ({
110
- values,
111
- setStates,
112
- setFieldValue,
113
- setValues,
114
- setUserValues,
115
- onGetStatesSuccess,
116
- onGetStatesError,
117
- }) => {
118
- const prevCountry = useRef(values.country)
119
- useEffect(() => {
120
- const fetchStates = async () => {
121
- try {
122
- const res = await getStates(values.country)
123
- const mappedStates = _map(_get(res, 'data.data'), (item, key) => ({
124
- label: item,
125
- value: key,
126
- }))
127
- setStates(mappedStates)
128
- if (prevCountry.current !== values.country) {
129
- setFieldValue('state', mappedStates[0]?.value ?? '')
130
- prevCountry.current = values.country
131
- }
132
- onGetStatesSuccess(res.data)
133
- } catch (e) {
134
- if (axios.isAxiosError(e)) {
135
- onGetStatesError(e)
136
- }
137
- }
138
- }
139
- fetchStates()
140
- }, [values.country, setStates, setFieldValue])
141
- const userDataEncoded =
142
- typeof window !== 'undefined'
143
- ? window.localStorage.getItem('user_data')
144
- : ''
145
- useEffect(() => {
146
- // set user data from local storage
147
- const getStoredUserData = () => {
148
- if (typeof window !== 'undefined') {
149
- if (userDataEncoded) {
150
- try {
151
- const parsedData = JSON.parse(userDataEncoded)
152
- const mappedValues = {
153
- firstName: parsedData?.first_name || '',
154
- lastName: parsedData?.last_name || '',
155
- email: parsedData?.email || '',
156
- phone: parsedData?.phone || '',
157
- confirmEmail: parsedData?.email || '',
158
- state: parsedData?.state || '',
159
- street_address: parsedData?.street_address || '',
160
- country: parsedData?.country || '1',
161
- zip: parsedData?.zip || '',
162
- brand_opt_in: parsedData?.brand_opt_in || '',
163
- city: parsedData?.city || '',
164
- confirmPassword: '',
165
- password: '',
166
- 'holderFirstName-0': parsedData?.first_name || '',
167
- 'holderLastName-0': parsedData?.last_name || '',
168
- 'holderEmail-0': parsedData?.email || '',
169
- }
170
- setValues(mappedValues)
171
- setUserValues(mappedValues)
172
- } catch (e) {}
173
- }
174
- }
175
- }
176
- getStoredUserData()
177
- }, [userDataEncoded, setValues, setUserValues])
178
- return null
179
- }
180
- export const BillingInfoContainer = ({
181
- data = [],
182
- ticketHoldersFields = {
183
- id: 1,
184
- fields: [],
185
- },
186
- initialValues = {},
187
- buttonName = 'Submit',
188
- handleSubmit = _identity,
189
- theme = 'light',
190
- onRegisterSuccess = () => {},
191
- onRegisterError = () => {},
192
- onSubmitError = () => {},
193
- onGetCartSuccess = () => {},
194
- onGetCartError = () => {},
195
- onGetCountriesSuccess = () => {},
196
- onGetCountriesError = () => {},
197
- onGetStatesSuccess = () => {},
198
- onGetStatesError = () => {},
199
- onGetProfileDataSuccess = () => {},
200
- onGetProfileDataError = () => {},
201
- onAuthorizeSuccess = () => {},
202
- onAuthorizeError = () => {},
203
- onLogin,
204
- onLoginSuccess = () => {},
205
- isLoggedIn: pIsLoggedIn = false,
206
- accountInfoTitle = '',
207
- hideLogo,
208
- themeOptions,
209
- onErrorClose = () => {},
210
- hideErrorsAlertSection = false,
211
- onSkipBillingPage = () => {},
212
- skipPage = false,
213
- canSkipHolderNames = false,
214
- onCountdownFinish = () => {},
215
- enableTimer = false
216
- }: IBillingInfoPage) => {
217
- const themeMui = createTheme(themeOptions)
218
- const isWindowDefined = typeof window !== 'undefined'
219
- const userData =
220
- isWindowDefined && window.localStorage.getItem('user_data')
221
- ? JSON.parse(window.localStorage.getItem('user_data') || '')
222
- : {}
223
- const access_token =
224
- isWindowDefined && window.localStorage.getItem('access_token')
225
- ? window.localStorage.getItem('access_token') || ''
226
- : ''
227
- const [dataWithUniqueIds, setDataWithUniqueIds] = useState<
228
- IBillingInfoData[]
229
- >(data)
230
- const [isLoggedIn, setIsLoggedIn] = useState(!!access_token)
231
- const [cartInfoData, setCartInfo] = useState<any>({})
232
- const [countries, setCountries] = useState<any>([])
233
- const [states, setStates] = useState<any>([])
234
- const [showModalLogin, setShowModalLogin] = useState(false)
235
- const [alreadyHasUser, setAlreadyHasUser] = useState(false)
236
- const [userExpired, setUserExpired] = useState(false)
237
- const [showModalRegister, setShowModalRegister] = useState(false)
238
- const [ticketsQuantity, setTicketsQuantity] = useState<string[]>([])
239
- const [userValues, setUserValues] = useState<any>({
240
- firstName: '',
241
- lastName: '',
242
- email: '',
243
- phone: '',
244
- confirmEmail: '',
245
- holderFirstName: '',
246
- holderLastName: '',
247
- holderAge: '',
248
- city: '',
249
- country: '',
250
- street_address: '',
251
- state: '',
252
- zip: '',
253
- })
254
- const [loading, setLoading] = useState(true)
255
- const [error, setError] = useState(null)
256
- const emailLogged =
257
- _get(userData, 'email', '') || _get(userValues, 'email', '')
258
- const firstNameLogged =
259
- _get(userData, 'first_name', '') || _get(userValues, 'first_name', '')
260
- const lastNameLogged =
261
- _get(userData, 'last_name', '') || _get(userValues, 'last_name', '')
262
- const showDOB = getQueryVariable('age_required') === 'true'
263
- const showTicketHolders = getQueryVariable('names_required') === 'true'
264
- const eventId = getQueryVariable('event_id')
265
- const optedInFieldValue: boolean = _get(cartInfoData, 'optedIn', false)
266
- const ttfOptIn: boolean = Boolean(_get(cartInfoData, 'ttfOptIn', false))
267
- const hideTtfOptIn: boolean = _get(cartInfoData, 'hide_ttf_opt_in', true)
268
- const expirationTime = _get(cartInfoData, 'expires_at')
269
- const flagRequirePhone = getQueryVariable('phone_required') === 'true'
270
-
271
- // Get prevProps
272
- const prevData = useRef(data)
273
-
274
- useEffect(() => {
275
- const hasUniqueId = _get(dataWithUniqueIds, '[0].uniqueId')
276
- const isEqualData = _isEqual(prevData.current, data)
277
- if (!hasUniqueId || !isEqualData) {
278
- setDataWithUniqueIds(assingUniqueIds(data))
279
- if (!isEqualData) {
280
- prevData.current = data
281
- }
282
- }
283
- }, [dataWithUniqueIds, data])
284
-
285
- const getQuantity = (cart: any = []) => {
286
- let qty: any = 0
287
- cart.forEach((item: any) => {
288
- qty += +item.quantity
289
- })
290
- return qty
291
- }
292
-
293
- useEffect(() => {
294
- if ((pIsLoggedIn || access_token) && !isLoggedIn) {
295
- setIsLoggedIn(true)
296
- }
297
- }, [pIsLoggedIn, isLoggedIn, access_token])
298
- //just once
299
- useEffect(() => {
300
- // fetch countries data
301
- const fetchCountries = async () => {
302
- try {
303
- const res = await getCountries()
304
- setCustomHeader(res)
305
- setCountries(
306
- _map(_get(res, 'data.data'), (item, key) => ({
307
- label: item,
308
- value: key,
309
- }))
310
- )
311
- onGetCountriesSuccess(res.data)
312
- } catch (e) {
313
- if (axios.isAxiosError(e)) {
314
- onGetCountriesError(e)
315
- }
316
- }
317
- }
318
- fetchCountries()
319
- fetchCart()
320
- }, [])
321
- // fetch cart data
322
- const fetchCart = async () => {
323
- try {
324
- const res = await getCart()
325
- setCustomHeader(res)
326
- const cartInfo = _get(res, 'data.data.attributes')
327
- setCartInfo(cartInfo)
328
- const { cart = [] } = cartInfo
329
- setTicketsQuantity(
330
- new Array(getQuantity(cart)).fill(null).map(() => nanoid())
331
- )
332
- onGetCartSuccess(res.data)
333
- } catch (e) {
334
- if (axios.isAxiosError(e)) {
335
- onGetCartError(e)
336
- }
337
- }
338
- }
339
- // fetch user data
340
- const fetchUserData = async (token: string) => {
341
- try {
342
- if (isWindowDefined && token) {
343
- const userDataResponse = await getProfileData(token)
344
- const profileSpecifiedData = _get(userDataResponse, 'data.data')
345
- const profileDataObj = setLoggedUserData(profileSpecifiedData)
346
- setUserValues({
347
- ...profileDataObj,
348
- firstName: profileDataObj.first_name,
349
- lastName: profileDataObj.last_name,
350
- })
351
- window.localStorage.setItem(
352
- 'user_data',
353
- JSON.stringify(profileDataObj)
354
- )
355
- onGetProfileDataSuccess(userDataResponse.data)
356
- }
357
- } catch (e) {
358
- if (axios.isAxiosError(e)) {
359
- onGetProfileDataError(e)
360
- }
361
- }
362
- }
363
- useEffect(() => {
364
- fetchUserData(access_token)
365
- }, [access_token])
366
-
367
- useEffect(() => {
368
- const collectPaymentData = async () => {
369
- if (skipPage && !_isEmpty(ticketsQuantity) && !showDOB) {
370
- setLoading(true)
371
- const checkoutBody = createCheckoutDataBodyWithDefaultHolder(
372
- ticketsQuantity.length,
373
- userData
374
- )
375
-
376
- try {
377
- const res = await postOnCheckout(checkoutBody, access_token)
378
- onSkipBillingPage(_get(res, 'data.data.attributes'))
379
- setLoading(false)
380
- } catch (e) {
381
- onSubmitError(e)
382
- }
383
- } else {
384
- setLoading(false)
385
- }
386
- }
387
- collectPaymentData()
388
- }, [skipPage, ticketsQuantity])
389
-
390
-
391
- const collectCheckoutBody = (values: object): object => {
392
- let checkoutBody = {}
393
-
394
- // Auto collect ticket holders name when it was skipped optionally
395
- if (showDOB && !showTicketHolders && canSkipHolderNames) {
396
- checkoutBody = createCheckoutDataBodyWithDefaultHolder(
397
- ticketsQuantity.length,
398
- values,
399
- true
400
- )
401
- } else {
402
- checkoutBody = createCheckoutDataBody(
403
- ticketsQuantity.length,
404
- values,
405
- { emailLogged, firstNameLogged, lastNameLogged },
406
- showDOB
407
- )
408
- }
409
-
410
- return checkoutBody
411
- }
412
-
413
- // Displaying loader
414
- if (loading) {
415
- return (
416
- <Backdrop sx={{ color: '#fff' }} open={true}>
417
- <CircularProgress color="inherit" />
418
- </Backdrop>
419
- )
420
- }
421
-
422
- return (
423
- <ThemeProvider theme={themeMui}>
424
- {expirationTime && enableTimer && (
425
- <TimerWidget
426
- expires_at={expirationTime}
427
- onCountdownFinish={onCountdownFinish}
428
- />
429
- )}
430
- <Formik
431
- initialValues={getInitialValues(
432
- dataWithUniqueIds,
433
- {
434
- ...initialValues,
435
- country: _get(userData, 'country', '') || '1',
436
- state: _get(userData, 'state', '') || '1',
437
- brand_opt_in: optedInFieldValue,
438
- ttf_opt_in: ttfOptIn,
439
- },
440
- userValues
441
- )}
442
- enableReinitialize={true}
443
- onSubmit={async (values, formikHelpers) => {
444
- try {
445
- if (isLoggedIn) {
446
- if (access_token) {
447
- const updatedUserData = await getProfileData(access_token)
448
- const profileSpecifiedData = _get(updatedUserData, 'data.data')
449
- const profileDataObj = setLoggedUserData(profileSpecifiedData)
450
- if (isWindowDefined) {
451
- window.localStorage.setItem(
452
- 'user_data',
453
- JSON.stringify(profileDataObj)
454
- )
455
- }
456
- }
457
-
458
- const checkoutBody = collectCheckoutBody(values)
459
- const res = await postOnCheckout(checkoutBody, access_token)
460
- handleSubmit(
461
- values,
462
- formikHelpers as FormikHelpers<any>,
463
- eventId,
464
- res
465
- )
466
- return
467
- }
468
- const checkoutBodyForRegistration = createCheckoutDataBody(
469
- ticketsQuantity.length,
470
- values,
471
- { emailLogged, firstNameLogged, lastNameLogged },
472
- showDOB
473
- )
474
- const bodyFormData = createRegisterFormData(
475
- values,
476
- checkoutBodyForRegistration
477
- )
478
- let access_token_register = null
479
- try {
480
- const resRegister = await register(bodyFormData)
481
- access_token_register = _get(
482
- resRegister,
483
- 'data.data.attributes.access_token'
484
- )
485
- const refreshToken = _get(
486
- resRegister,
487
- 'data.data.attributes.refresh_token'
488
- )
489
- handleSetAccessToken(access_token_register)
490
- const tokens = {
491
- accessToken: access_token_register,
492
- refreshToken,
493
- }
494
- onRegisterSuccess(tokens)
495
- } catch (e) {
496
- if (axios.isAxiosError(e)) {
497
- const error = e?.response?.data?.message
498
- if (_includes(error, 'You must be aged')) {
499
- formikHelpers.setFieldError('holderAge', error)
500
- }
501
- if (error?.password) {
502
- formikHelpers.setFieldError('password', error.password)
503
- formikHelpers.setFieldError(
504
- 'confirmPassword',
505
- error.password
506
- )
507
- }
508
- if (error?.email && !onLogin) {
509
- // False will stand for outside controll
510
- setAlreadyHasUser(true)
511
- setShowModalLogin(true)
512
- }
513
-
514
- if (_includes(error, 'The cart is expired') && !hideErrorsAlertSection) {
515
- setError(error)
516
- }
517
-
518
- onRegisterError(e, values.email)
519
- }
520
- return
521
- }
522
- const profileData = await getProfileData(access_token_register)
523
- const profileSpecifiedData = _get(profileData, 'data.data')
524
- const profileDataObj = setLoggedUserData(profileSpecifiedData)
525
- if (isWindowDefined) {
526
- window.localStorage.setItem(
527
- 'user_data',
528
- JSON.stringify(profileDataObj)
529
- )
530
- }
531
-
532
- const checkoutBody = collectCheckoutBody(values)
533
- const res = await postOnCheckout(
534
- checkoutBody,
535
- access_token_register
536
- )
537
- handleSubmit(
538
- values,
539
- formikHelpers as FormikHelpers<any>,
540
- eventId,
541
- res
542
- )
543
- } catch (e) {
544
- if (axios.isAxiosError(e)) {
545
- if (e.response?.data.error === 'invalid_token') {
546
- if (isWindowDefined) {
547
- window.localStorage.removeItem('user_data')
548
- window.localStorage.removeItem('access_token')
549
- setUserExpired(true)
550
- setShowModalLogin(true)
551
- }
552
- }
553
- if (e.response?.data.message && !hideErrorsAlertSection) {
554
- setError(_get(e, 'response.data.message'))
555
- }
556
- onSubmitError(e)
557
- }
558
- }
559
- }}
560
- >
561
- {(props: FormikProps<any>) => (
562
- <Form onSubmit={props.handleSubmit}>
563
- <ErrorFocus />
564
- <LogicRunner
565
- values={props.values}
566
- setStates={setStates}
567
- setFieldValue={props.setFieldValue}
568
- setValues={props.setValues}
569
- setUserValues={setUserValues}
570
- onGetStatesSuccess={onGetStatesSuccess}
571
- onGetStatesError={onGetStatesError}
572
- />
573
- <div className={`billing-info-container ${theme}`}>
574
- <SnackbarAlert
575
- type="error"
576
- isOpen={!!error}
577
- message={error || ''}
578
- onClose={() => {
579
- setError(null)
580
- onErrorClose()
581
- }}
582
- />
583
- {!isLoggedIn && (
584
- <div className="account-actions-block">
585
- <div>{accountInfoTitle}</div>
586
- <div>Login & skip ahead:</div>
587
- <div className="login-block">
588
- <button
589
- className="login-register-button"
590
- type="button"
591
- onClick={() => {
592
- // If outside login needed to skip package login functionallity
593
- if (onLogin) {
594
- onLogin()
595
- } else {
596
- setShowModalLogin(true)
597
- }
598
- }}
599
- >
600
- Login
601
- </button>
602
- {!hideLogo && (
603
- <div className="logo-image-container">
604
- <img
605
- src={
606
- theme === 'dark'
607
- ? 'https://www.ticketfairy.com/resources/images/logo-ttf.svg'
608
- : 'https://www.ticketfairy.com/resources/images/logo-ttf-black.svg'
609
- }
610
- alt="nodata"
611
- />
612
- </div>
613
- )}
614
- </div>
615
- </div>
616
- )}
617
- {_map(dataWithUniqueIds, item => {
618
- const { label, labelClassName, fields } = item
619
- return (
620
- <React.Fragment key={item.uniqueId}>
621
- <p className={labelClassName}>{label}</p>
622
- {_map(fields, group => {
623
- const { groupClassname, groupItems } = group
624
- return (
625
- <React.Fragment key={group.uniqueId}>
626
- <div className={groupClassname}>
627
- {_map(
628
- groupItems.filter(el => {
629
- if (el.name === 'holderAge' && !showDOB) {
630
- return false
631
- }
632
- if (el.name === 'ttf_opt_in' && hideTtfOptIn) {
633
- return false
634
- }
635
- if (el.name === 'phone') {
636
- el.required = flagRequirePhone
637
- }
638
- if (el.name === 'street_address') {
639
- el.required = true
640
- }
641
- return true
642
- }),
643
- element =>
644
- [
645
- 'password',
646
- 'confirmPassword',
647
- 'password-info',
648
- ].includes(element.name) &&
649
- isLoggedIn ? null : (
650
- <React.Fragment key={element.uniqueId}>
651
- <div className={element.className}>
652
- {element.component ? (
653
- element.component
654
- ) : (
655
- <Field
656
- name={element.name}
657
- label={element.name === 'phone'
658
- ? `${element.label}${flagRequirePhone ? '' : ' (optional)'} `
659
- : element.label}
660
- type={element.type}
661
- validate={getValidateFunctions(
662
- element,
663
- states,
664
- props.values
665
- )}
666
- setFieldValue={props.setFieldValue}
667
- onBlur={props.handleBlur}
668
- component={
669
- element.type === 'checkbox'
670
- ? CheckboxField
671
- : element.type === 'select'
672
- ? SelectField
673
- : element.type === 'phone'
674
- ? FormikPhoneNumberField
675
- : CustomField
676
- }
677
- selectOptions={
678
- element.name === 'country'
679
- ? countries
680
- : element.name === 'state'
681
- ? states
682
- : []
683
- }
684
- theme={theme}
685
- />
686
- )}
687
- </div>
688
- </React.Fragment>
689
- )
690
- )}
691
- </div>
692
- </React.Fragment>
693
- )
694
- })}
695
- </React.Fragment>
696
- )
697
- })}
698
- {!_isEmpty(ticketHoldersFields.fields) && (
699
- <div className="ticket-holders-fields">
700
- <p>{ticketHoldersFields.label}</p>
701
- {_map(ticketsQuantity, (_item, index) => (
702
- <div key={_item}>
703
- <h5>Ticket {index + 1}</h5>
704
- {_map(ticketHoldersFields.fields, group => {
705
- const { groupClassname, groupItems } = group
706
- return (
707
- <div key={group.id}>
708
- <div className={groupClassname}>
709
- {_map(groupItems, element => (
710
- <div
711
- className={element.className}
712
- key={element.name}
713
- >
714
- <Field
715
- name={`${element.name}-${index}`}
716
- label={element.label}
717
- type={element.type}
718
- required={true}
719
- component={
720
- element.type === 'checkbox'
721
- ? CheckboxField
722
- : CustomField
723
- }
724
- validate={combineValidators(
725
- element.required
726
- ? requiredValidator
727
- : () => {},
728
- element.onValidate
729
- ? element.onValidate
730
- : () => {}
731
- )}
732
- />
733
- </div>
734
- ))}
735
- </div>
736
- </div>
737
- )
738
- })}
739
- </div>
740
- ))}
741
- </div>
742
- )}
743
- <div className="button-container">
744
- <Button
745
- type="submit"
746
- variant="contained"
747
- className="login-register-button"
748
- disabled={props.isSubmitting}
749
- >
750
- {props.isSubmitting ? (
751
- <CircularProgress size={26} />
752
- ) : (
753
- buttonName
754
- )}
755
- </Button>
756
- </div>
757
- </div>
758
- </Form>
759
- )}
760
- </Formik>
761
- {showModalLogin && (
762
- <LoginModal
763
- onClose={() => {
764
- setShowModalLogin(false)
765
- }}
766
- onLogin={() => {
767
- setShowModalLogin(false)
768
- setUserExpired(false)
769
- onLoginSuccess()
770
- }}
771
- alreadyHasUser={alreadyHasUser}
772
- userExpired={userExpired}
773
- onAuthorizeSuccess={onAuthorizeSuccess}
774
- onAuthorizeError={onAuthorizeError}
775
- onGetProfileDataSuccess={(data: any) => {
776
- fetchCart()
777
- onGetProfileDataSuccess(data)
778
- }}
779
- onGetProfileDataError={onGetProfileDataError}
780
- />
781
- )}
782
- {showModalRegister && (
783
- <RegisterModal
784
- onClose={() => {
785
- setShowModalRegister(false)
786
- }}
787
- onRegister={() => {
788
- setShowModalRegister(false)
789
- }}
790
- onGetProfileDataSuccess={(data: any) => {
791
- fetchCart()
792
- onGetProfileDataSuccess(data)
793
- }}
794
- onGetProfileDataError={onGetProfileDataError}
795
- />
796
- )}
797
- </ThemeProvider>
798
- )
799
- }
1
+ import React, { FC, useEffect, useState, useRef } from 'react'
2
+ import { AxiosError } from 'axios'
3
+ import {
4
+ Field,
5
+ Form,
6
+ Formik,
7
+ FormikHelpers,
8
+ FormikProps,
9
+ FormikValues,
10
+ } from 'formik'
11
+ import Button from '@mui/material/Button'
12
+ import _identity from 'lodash/identity'
13
+ import _map from 'lodash/map'
14
+ import _get from 'lodash/get'
15
+ import _includes from 'lodash/includes'
16
+ import _isEqual from 'lodash/isEqual'
17
+ import _isEmpty from 'lodash/isEmpty'
18
+
19
+ import './style.css'
20
+ import { combineValidators, requiredValidator } from '../../validators'
21
+ import { IBillingInfoData } from '../../types'
22
+ import {
23
+ getCart,
24
+ getCountries,
25
+ getProfileData,
26
+ handleSetAccessToken,
27
+ postOnCheckout,
28
+ register,
29
+ setCustomHeader,
30
+ getStates,
31
+ } from '../../api'
32
+ import { LoginModal } from '../loginModal'
33
+ import { RegisterModal } from '../registerModal'
34
+ import {
35
+ assingUniqueIds,
36
+ createCheckoutDataBody,
37
+ createRegisterFormData,
38
+ getInitialValues,
39
+ getValidateFunctions,
40
+ setLoggedUserData,
41
+ } from './utils'
42
+ import { createCheckoutDataBodyWithDefaultHolder } from '../../utils'
43
+ import axios from 'axios'
44
+ import { CustomField, CheckboxField, SelectField, FormikPhoneNumberField } from '../common/index'
45
+ import { CircularProgress, ThemeOptions } from '@mui/material'
46
+ import { nanoid } from 'nanoid'
47
+ import { getQueryVariable } from '../../utils/getQueryVariable'
48
+ import { ErrorFocus } from '../../utils/formikErrorFocus'
49
+ import { ThemeProvider, createTheme } from '@mui/material/styles'
50
+ import { CSSProperties } from '@mui/styles'
51
+ import Backdrop from '@mui/material/Backdrop'
52
+ import TimerWidget from '../timerWidget'
53
+ import SnackbarAlert from '../common/SnackbarAlert'
54
+ import { DatePickerField } from '../common/DatePickerField'
55
+
56
+ export interface IBillingInfoPage {
57
+ data?: IBillingInfoData[];
58
+ ticketHoldersFields?: IBillingInfoData;
59
+ handleSubmit?: (
60
+ values: FormikValues,
61
+ formikHelpers: FormikHelpers<FormikValues>,
62
+ eventId: any,
63
+ res: any
64
+ ) => void;
65
+ onRegisterSuccess?: (value: {
66
+ accessToken: string;
67
+ refreshToken: string;
68
+ }) => void;
69
+ onRegisterError?: (e: AxiosError, email: string) => void;
70
+ onSubmitError?: (e: AxiosError) => void;
71
+ onGetCartSuccess?: (res: any) => void;
72
+ onGetCartError?: (e: AxiosError) => void;
73
+ onGetCountriesSuccess?: (res: any) => void;
74
+ onGetCountriesError?: (e: AxiosError) => void;
75
+ onGetStatesSuccess?: (res: any) => void;
76
+ onGetStatesError?: (e: AxiosError) => void;
77
+ onGetProfileDataSuccess?: (res: any) => void;
78
+ onGetProfileDataError?: (e: AxiosError) => void;
79
+ onAuthorizeSuccess?: () => void;
80
+ onAuthorizeError?: (e: AxiosError) => void;
81
+ onLogin?: () => void;
82
+ onLoginSuccess?: () => void;
83
+ onErrorClose?: () => void;
84
+ initialValues?: FormikValues;
85
+ buttonName?: string;
86
+ theme?: 'light' | 'dark';
87
+ isLoggedIn?: boolean;
88
+ accountInfoTitle?: string | JSX.Element;
89
+ hideLogo?: boolean;
90
+ themeOptions?: ThemeOptions & {
91
+ input?: CSSProperties;
92
+ checkbox?: CSSProperties;
93
+ };
94
+ hideErrorsAlertSection?: boolean;
95
+ onSkipBillingPage: (data: any) => void;
96
+ skipPage?: boolean;
97
+ canSkipHolderNames?: boolean;
98
+ onCountdownFinish?: () => void;
99
+ enableTimer?: boolean;
100
+ }
101
+
102
+ const LogicRunner: FC<{
103
+ values: any;
104
+ setStates: React.Dispatch<any>;
105
+ setFieldValue: any;
106
+ setValues: any;
107
+ setUserValues: any;
108
+ onGetStatesSuccess: any;
109
+ onGetStatesError: any;
110
+ }> = ({
111
+ values,
112
+ setStates,
113
+ setFieldValue,
114
+ setValues,
115
+ setUserValues,
116
+ onGetStatesSuccess,
117
+ onGetStatesError,
118
+ }) => {
119
+ const prevCountry = useRef(values.country)
120
+ useEffect(() => {
121
+ const fetchStates = async () => {
122
+ try {
123
+ const res = await getStates(values.country)
124
+ const mappedStates = _map(_get(res, 'data.data'), (item, key) => ({
125
+ label: item,
126
+ value: key,
127
+ }))
128
+ setStates(mappedStates)
129
+ if (prevCountry.current !== values.country) {
130
+ setFieldValue('state', mappedStates[0]?.value ?? '')
131
+ prevCountry.current = values.country
132
+ }
133
+ onGetStatesSuccess(res.data)
134
+ } catch (e) {
135
+ if (axios.isAxiosError(e)) {
136
+ onGetStatesError(e)
137
+ }
138
+ }
139
+ }
140
+ fetchStates()
141
+ }, [values.country, setStates, setFieldValue])
142
+ const userDataEncoded =
143
+ typeof window !== 'undefined'
144
+ ? window.localStorage.getItem('user_data')
145
+ : ''
146
+ useEffect(() => {
147
+ // set user data from local storage
148
+ const getStoredUserData = () => {
149
+ if (typeof window !== 'undefined') {
150
+ if (userDataEncoded) {
151
+ try {
152
+ const parsedData = JSON.parse(userDataEncoded)
153
+ const mappedValues = {
154
+ firstName: parsedData?.first_name || '',
155
+ lastName: parsedData?.last_name || '',
156
+ email: parsedData?.email || '',
157
+ phone: parsedData?.phone || '',
158
+ confirmEmail: parsedData?.email || '',
159
+ state: parsedData?.state || '',
160
+ street_address: parsedData?.street_address || '',
161
+ country: parsedData?.country || '1',
162
+ zip: parsedData?.zip || '',
163
+ brand_opt_in: parsedData?.brand_opt_in || '',
164
+ city: parsedData?.city || '',
165
+ confirmPassword: '',
166
+ password: '',
167
+ 'holderFirstName-0': parsedData?.first_name || '',
168
+ 'holderLastName-0': parsedData?.last_name || '',
169
+ 'holderEmail-0': parsedData?.email || '',
170
+ }
171
+ setValues(mappedValues)
172
+ setUserValues(mappedValues)
173
+ } catch (e) {}
174
+ }
175
+ }
176
+ }
177
+ getStoredUserData()
178
+ }, [userDataEncoded, setValues, setUserValues])
179
+ return null
180
+ }
181
+ export const BillingInfoContainer = ({
182
+ data = [],
183
+ ticketHoldersFields = {
184
+ id: 1,
185
+ fields: [],
186
+ },
187
+ initialValues = {},
188
+ buttonName = 'Submit',
189
+ handleSubmit = _identity,
190
+ theme = 'light',
191
+ onRegisterSuccess = () => {},
192
+ onRegisterError = () => {},
193
+ onSubmitError = () => {},
194
+ onGetCartSuccess = () => {},
195
+ onGetCartError = () => {},
196
+ onGetCountriesSuccess = () => {},
197
+ onGetCountriesError = () => {},
198
+ onGetStatesSuccess = () => {},
199
+ onGetStatesError = () => {},
200
+ onGetProfileDataSuccess = () => {},
201
+ onGetProfileDataError = () => {},
202
+ onAuthorizeSuccess = () => {},
203
+ onAuthorizeError = () => {},
204
+ onLogin,
205
+ onLoginSuccess = () => {},
206
+ isLoggedIn: pIsLoggedIn = false,
207
+ accountInfoTitle = '',
208
+ hideLogo,
209
+ themeOptions,
210
+ onErrorClose = () => {},
211
+ hideErrorsAlertSection = false,
212
+ onSkipBillingPage = () => {},
213
+ skipPage = false,
214
+ canSkipHolderNames = false,
215
+ onCountdownFinish = () => {},
216
+ enableTimer = false
217
+ }: IBillingInfoPage) => {
218
+ const themeMui = createTheme(themeOptions)
219
+ const isWindowDefined = typeof window !== 'undefined'
220
+ const userData =
221
+ isWindowDefined && window.localStorage.getItem('user_data')
222
+ ? JSON.parse(window.localStorage.getItem('user_data') || '')
223
+ : {}
224
+ const access_token =
225
+ isWindowDefined && window.localStorage.getItem('access_token')
226
+ ? window.localStorage.getItem('access_token') || ''
227
+ : ''
228
+ const [dataWithUniqueIds, setDataWithUniqueIds] = useState<
229
+ IBillingInfoData[]
230
+ >(data)
231
+ const [isLoggedIn, setIsLoggedIn] = useState(!!access_token)
232
+ const [cartInfoData, setCartInfo] = useState<any>({})
233
+ const [countries, setCountries] = useState<any>([])
234
+ const [states, setStates] = useState<any>([])
235
+ const [showModalLogin, setShowModalLogin] = useState(false)
236
+ const [alreadyHasUser, setAlreadyHasUser] = useState(false)
237
+ const [userExpired, setUserExpired] = useState(false)
238
+ const [showModalRegister, setShowModalRegister] = useState(false)
239
+ const [ticketsQuantity, setTicketsQuantity] = useState<string[]>([])
240
+ const [userValues, setUserValues] = useState<any>({
241
+ firstName: '',
242
+ lastName: '',
243
+ email: '',
244
+ phone: '',
245
+ confirmEmail: '',
246
+ holderFirstName: '',
247
+ holderLastName: '',
248
+ holderAge: '',
249
+ city: '',
250
+ country: '',
251
+ street_address: '',
252
+ state: '',
253
+ zip: '',
254
+ })
255
+ const [loading, setLoading] = useState(true)
256
+ const [error, setError] = useState(null)
257
+ const emailLogged =
258
+ _get(userData, 'email', '') || _get(userValues, 'email', '')
259
+ const firstNameLogged =
260
+ _get(userData, 'first_name', '') || _get(userValues, 'first_name', '')
261
+ const lastNameLogged =
262
+ _get(userData, 'last_name', '') || _get(userValues, 'last_name', '')
263
+ const showDOB = getQueryVariable('age_required') === 'true'
264
+ const showTicketHolders = getQueryVariable('names_required') === 'true'
265
+ const eventId = getQueryVariable('event_id')
266
+ const optedInFieldValue: boolean = _get(cartInfoData, 'optedIn', false)
267
+ const ttfOptIn: boolean = Boolean(_get(cartInfoData, 'ttfOptIn', false))
268
+ const hideTtfOptIn: boolean = _get(cartInfoData, 'hide_ttf_opt_in', true)
269
+ const expirationTime = _get(cartInfoData, 'expiresAt')
270
+ const flagRequirePhone = getQueryVariable('phone_required') === 'true'
271
+
272
+ // Get prevProps
273
+ const prevData = useRef(data)
274
+
275
+ useEffect(() => {
276
+ const hasUniqueId = _get(dataWithUniqueIds, '[0].uniqueId')
277
+ const isEqualData = _isEqual(prevData.current, data)
278
+ if (!hasUniqueId || !isEqualData) {
279
+ setDataWithUniqueIds(assingUniqueIds(data))
280
+ if (!isEqualData) {
281
+ prevData.current = data
282
+ }
283
+ }
284
+ }, [dataWithUniqueIds, data])
285
+
286
+ const getQuantity = (cart: any = []) => {
287
+ let qty: any = 0
288
+ cart.forEach((item: any) => {
289
+ qty += +item.quantity
290
+ })
291
+ return qty
292
+ }
293
+
294
+ useEffect(() => {
295
+ if ((pIsLoggedIn || access_token) && !isLoggedIn) {
296
+ setIsLoggedIn(true)
297
+ }
298
+ }, [pIsLoggedIn, isLoggedIn, access_token])
299
+ //just once
300
+ useEffect(() => {
301
+ // fetch countries data
302
+ const fetchCountries = async () => {
303
+ try {
304
+ const res = await getCountries()
305
+ setCustomHeader(res)
306
+ setCountries(
307
+ _map(_get(res, 'data.data'), (item, key) => ({
308
+ label: item,
309
+ value: key,
310
+ }))
311
+ )
312
+ onGetCountriesSuccess(res.data)
313
+ } catch (e) {
314
+ if (axios.isAxiosError(e)) {
315
+ onGetCountriesError(e)
316
+ }
317
+ }
318
+ }
319
+ fetchCountries()
320
+ fetchCart()
321
+ }, [])
322
+ // fetch cart data
323
+ const fetchCart = async () => {
324
+ try {
325
+ const res = await getCart()
326
+ setCustomHeader(res)
327
+ const cartInfo = _get(res, 'data.data.attributes')
328
+ setCartInfo(cartInfo)
329
+ const { cart = [] } = cartInfo
330
+ setTicketsQuantity(
331
+ new Array(getQuantity(cart)).fill(null).map(() => nanoid())
332
+ )
333
+ onGetCartSuccess(res.data)
334
+ } catch (e) {
335
+ if (axios.isAxiosError(e)) {
336
+ onGetCartError(e)
337
+ }
338
+ }
339
+ }
340
+ // fetch user data
341
+ const fetchUserData = async (token: string) => {
342
+ try {
343
+ if (isWindowDefined && token) {
344
+ const userDataResponse = await getProfileData(token)
345
+ const profileSpecifiedData = _get(userDataResponse, 'data.data')
346
+ const profileDataObj = setLoggedUserData(profileSpecifiedData)
347
+ setUserValues({
348
+ ...profileDataObj,
349
+ firstName: profileDataObj.first_name,
350
+ lastName: profileDataObj.last_name,
351
+ })
352
+ window.localStorage.setItem(
353
+ 'user_data',
354
+ JSON.stringify(profileDataObj)
355
+ )
356
+ onGetProfileDataSuccess(userDataResponse.data)
357
+ }
358
+ } catch (e) {
359
+ if (axios.isAxiosError(e)) {
360
+ onGetProfileDataError(e)
361
+ }
362
+ }
363
+ }
364
+ useEffect(() => {
365
+ fetchUserData(access_token)
366
+ }, [access_token])
367
+
368
+ useEffect(() => {
369
+ const collectPaymentData = async () => {
370
+ if (skipPage && !_isEmpty(ticketsQuantity) && !showDOB) {
371
+ setLoading(true)
372
+ const checkoutBody = createCheckoutDataBodyWithDefaultHolder(
373
+ ticketsQuantity.length,
374
+ userData
375
+ )
376
+
377
+ try {
378
+ const res = await postOnCheckout(checkoutBody, access_token)
379
+ removeReferralKey()
380
+ onSkipBillingPage(_get(res, 'data.data.attributes'))
381
+ setLoading(false)
382
+ } catch (e) {
383
+ onSubmitError(e)
384
+ }
385
+ } else {
386
+ setLoading(false)
387
+ }
388
+ }
389
+ collectPaymentData()
390
+ }, [skipPage, ticketsQuantity])
391
+
392
+
393
+ const collectCheckoutBody = (values: object): object => {
394
+ let checkoutBody = {}
395
+
396
+ // Auto collect ticket holders name when it was skipped optionally
397
+ if (showDOB && !showTicketHolders && canSkipHolderNames) {
398
+ checkoutBody = createCheckoutDataBodyWithDefaultHolder(
399
+ ticketsQuantity.length,
400
+ values,
401
+ true
402
+ )
403
+ } else {
404
+ checkoutBody = createCheckoutDataBody(
405
+ ticketsQuantity.length,
406
+ values,
407
+ { emailLogged, firstNameLogged, lastNameLogged },
408
+ showDOB
409
+ )
410
+ }
411
+
412
+ return checkoutBody
413
+ }
414
+
415
+ const removeReferralKey = () => {
416
+ localStorage.removeItem('referral_key')
417
+ }
418
+
419
+ // Displaying loader
420
+ if (loading || (enableTimer && !expirationTime && typeof window !== 'undefined')) {
421
+ if (expirationTime === 0) {
422
+ // Redirect to homepage (countdown finished and browser reloaded case)
423
+ window.location.href='/'
424
+ }
425
+
426
+ return (
427
+ <Backdrop sx={{ color: '#fff' }} open={true}>
428
+ <CircularProgress color="inherit" />
429
+ </Backdrop>
430
+ )
431
+ }
432
+
433
+ return (
434
+ <ThemeProvider theme={themeMui}>
435
+ {!!expirationTime && enableTimer && (
436
+ <TimerWidget
437
+ expires_at={expirationTime}
438
+ onCountdownFinish={onCountdownFinish}
439
+ />
440
+ )}
441
+ <Formik
442
+ initialValues={getInitialValues(
443
+ dataWithUniqueIds,
444
+ {
445
+ ...initialValues,
446
+ country: _get(userData, 'country', '') || '1',
447
+ state: _get(userData, 'state', '') || '1',
448
+ brand_opt_in: optedInFieldValue,
449
+ ttf_opt_in: ttfOptIn,
450
+ },
451
+ userValues
452
+ )}
453
+ enableReinitialize={true}
454
+ onSubmit={async (values, formikHelpers) => {
455
+ try {
456
+ if (isLoggedIn) {
457
+ if (access_token) {
458
+ const updatedUserData = await getProfileData(access_token)
459
+ const profileSpecifiedData = _get(updatedUserData, 'data.data')
460
+ const profileDataObj = setLoggedUserData(profileSpecifiedData)
461
+ if (isWindowDefined) {
462
+ window.localStorage.setItem(
463
+ 'user_data',
464
+ JSON.stringify(profileDataObj)
465
+ )
466
+ }
467
+ }
468
+
469
+ const checkoutBody = collectCheckoutBody(values)
470
+ const res = await postOnCheckout(checkoutBody, access_token)
471
+ removeReferralKey()
472
+ handleSubmit(
473
+ values,
474
+ formikHelpers as FormikHelpers<any>,
475
+ eventId,
476
+ res
477
+ )
478
+ return
479
+ }
480
+ const checkoutBodyForRegistration = createCheckoutDataBody(
481
+ ticketsQuantity.length,
482
+ values,
483
+ { emailLogged, firstNameLogged, lastNameLogged },
484
+ showDOB
485
+ )
486
+ const bodyFormData = createRegisterFormData(
487
+ values,
488
+ checkoutBodyForRegistration
489
+ )
490
+ let access_token_register = null
491
+ try {
492
+ const resRegister = await register(bodyFormData)
493
+ access_token_register = _get(
494
+ resRegister,
495
+ 'data.data.attributes.access_token'
496
+ )
497
+ const refreshToken = _get(
498
+ resRegister,
499
+ 'data.data.attributes.refresh_token'
500
+ )
501
+ handleSetAccessToken(access_token_register)
502
+ const tokens = {
503
+ accessToken: access_token_register,
504
+ refreshToken,
505
+ }
506
+ onRegisterSuccess(tokens)
507
+ } catch (e) {
508
+ if (axios.isAxiosError(e)) {
509
+ const error = e?.response?.data?.message
510
+ if (_includes(error, 'You must be aged')) {
511
+ formikHelpers.setFieldError('holderAge', error)
512
+ }
513
+ if (error?.password) {
514
+ formikHelpers.setFieldError('password', error.password)
515
+ formikHelpers.setFieldError(
516
+ 'confirmPassword',
517
+ error.password
518
+ )
519
+ }
520
+ if (error?.email && !onLogin) {
521
+ // False will stand for outside controll
522
+ setAlreadyHasUser(true)
523
+ setShowModalLogin(true)
524
+ }
525
+
526
+ if (_includes(error, 'The cart is expired') && !hideErrorsAlertSection) {
527
+ setError(error)
528
+ }
529
+
530
+ onRegisterError(e, values.email)
531
+ }
532
+ return
533
+ }
534
+ const profileData = await getProfileData(access_token_register)
535
+ const profileSpecifiedData = _get(profileData, 'data.data')
536
+ const profileDataObj = setLoggedUserData(profileSpecifiedData)
537
+ if (isWindowDefined) {
538
+ window.localStorage.setItem(
539
+ 'user_data',
540
+ JSON.stringify(profileDataObj)
541
+ )
542
+ }
543
+
544
+ const checkoutBody = collectCheckoutBody(values)
545
+ const res = await postOnCheckout(
546
+ checkoutBody,
547
+ access_token_register
548
+ )
549
+ removeReferralKey()
550
+ handleSubmit(
551
+ values,
552
+ formikHelpers as FormikHelpers<any>,
553
+ eventId,
554
+ res
555
+ )
556
+ } catch (e) {
557
+ if (axios.isAxiosError(e)) {
558
+ if (e.response?.data.error === 'invalid_token') {
559
+ if (isWindowDefined) {
560
+ window.localStorage.removeItem('user_data')
561
+ window.localStorage.removeItem('access_token')
562
+ setUserExpired(true)
563
+ setShowModalLogin(true)
564
+ }
565
+ }
566
+ if (e.response?.data.message && !hideErrorsAlertSection) {
567
+ setError(_get(e, 'response.data.message'))
568
+ }
569
+ onSubmitError(e)
570
+ }
571
+ }
572
+ }}
573
+ >
574
+ {(props: FormikProps<any>) => (
575
+ <Form onSubmit={props.handleSubmit}>
576
+ <ErrorFocus />
577
+ <LogicRunner
578
+ values={props.values}
579
+ setStates={setStates}
580
+ setFieldValue={props.setFieldValue}
581
+ setValues={props.setValues}
582
+ setUserValues={setUserValues}
583
+ onGetStatesSuccess={onGetStatesSuccess}
584
+ onGetStatesError={onGetStatesError}
585
+ />
586
+ <div className={`billing-info-container ${theme}`}>
587
+ <SnackbarAlert
588
+ type="error"
589
+ isOpen={!!error}
590
+ message={error || ''}
591
+ onClose={() => {
592
+ setError(null)
593
+ onErrorClose()
594
+ }}
595
+ />
596
+ {!isLoggedIn && (
597
+ <div className="account-actions-block">
598
+ <div>{accountInfoTitle}</div>
599
+ <div>Login & skip ahead:</div>
600
+ <div className="login-block">
601
+ <button
602
+ className="login-register-button"
603
+ type="button"
604
+ onClick={() => {
605
+ // If outside login needed to skip package login functionallity
606
+ if (onLogin) {
607
+ onLogin()
608
+ } else {
609
+ setShowModalLogin(true)
610
+ }
611
+ }}
612
+ >
613
+ Login
614
+ </button>
615
+ {!hideLogo && (
616
+ <div className="logo-image-container">
617
+ <img
618
+ src={
619
+ theme === 'dark'
620
+ ? 'https://www.ticketfairy.com/resources/images/logo-ttf.svg'
621
+ : 'https://www.ticketfairy.com/resources/images/logo-ttf-black.svg'
622
+ }
623
+ alt="nodata"
624
+ />
625
+ </div>
626
+ )}
627
+ </div>
628
+ </div>
629
+ )}
630
+ {_map(dataWithUniqueIds, item => {
631
+ const { label, labelClassName, fields } = item
632
+ return (
633
+ <React.Fragment key={item.uniqueId}>
634
+ <p className={labelClassName}>{label}</p>
635
+ {_map(fields, group => {
636
+ const { groupClassname, groupItems } = group
637
+ return (
638
+ <React.Fragment key={group.uniqueId}>
639
+ <div className={groupClassname}>
640
+ {_map(
641
+ groupItems.filter(el => {
642
+ if (el.name === 'holderAge' && !showDOB) {
643
+ return false
644
+ }
645
+ if (el.name === 'ttf_opt_in' && hideTtfOptIn) {
646
+ return false
647
+ }
648
+ if (el.name === 'phone') {
649
+ el.required = flagRequirePhone
650
+ }
651
+ return true
652
+ }),
653
+ element =>
654
+ [
655
+ 'password',
656
+ 'confirmPassword',
657
+ 'password-info',
658
+ ].includes(element.name) &&
659
+ isLoggedIn ? null : (
660
+ <React.Fragment key={element.uniqueId}>
661
+ <div className={element.className}>
662
+ {element.component ? (
663
+ element.component
664
+ ) : (
665
+ <Field
666
+ name={element.name}
667
+ label={element.name === 'phone'
668
+ ? `${element.label}${flagRequirePhone ? '' : ' (optional)'} `
669
+ : element.label}
670
+ type={element.type}
671
+ validate={getValidateFunctions(
672
+ element,
673
+ states,
674
+ props.values
675
+ )}
676
+ setFieldValue={props.setFieldValue}
677
+ onBlur={props.handleBlur}
678
+ component={
679
+ element.type === 'checkbox'
680
+ ? CheckboxField
681
+ : element.type === 'select'
682
+ ? SelectField
683
+ : element.type === 'phone'
684
+ ? FormikPhoneNumberField
685
+ : element.type === 'date'
686
+ ? DatePickerField
687
+ : CustomField
688
+ }
689
+ selectOptions={
690
+ element.name === 'country'
691
+ ? countries
692
+ : element.name === 'state'
693
+ ? states
694
+ : []
695
+ }
696
+ theme={theme}
697
+ />
698
+ )}
699
+ </div>
700
+ </React.Fragment>
701
+ )
702
+ )}
703
+ </div>
704
+ </React.Fragment>
705
+ )
706
+ })}
707
+ </React.Fragment>
708
+ )
709
+ })}
710
+ {!_isEmpty(ticketHoldersFields.fields) && (
711
+ <div className="ticket-holders-fields">
712
+ <p>{ticketHoldersFields.label}</p>
713
+ {_map(ticketsQuantity, (_item, index) => (
714
+ <div key={_item}>
715
+ <h5>Ticket {index + 1}</h5>
716
+ {_map(ticketHoldersFields.fields, group => {
717
+ const { groupClassname, groupItems } = group
718
+ return (
719
+ <div key={group.id}>
720
+ <div className={groupClassname}>
721
+ {_map(groupItems, element => (
722
+ <div
723
+ className={element.className}
724
+ key={element.name}
725
+ >
726
+ <Field
727
+ name={`${element.name}-${index}`}
728
+ label={element.label}
729
+ type={element.type}
730
+ required={true}
731
+ component={
732
+ element.type === 'checkbox'
733
+ ? CheckboxField
734
+ : CustomField
735
+ }
736
+ validate={combineValidators(
737
+ element.required
738
+ ? requiredValidator
739
+ : () => {},
740
+ element.onValidate
741
+ ? element.onValidate
742
+ : () => {}
743
+ )}
744
+ />
745
+ </div>
746
+ ))}
747
+ </div>
748
+ </div>
749
+ )
750
+ })}
751
+ </div>
752
+ ))}
753
+ </div>
754
+ )}
755
+ <div className="button-container">
756
+ <Button
757
+ type="submit"
758
+ variant="contained"
759
+ className="login-register-button"
760
+ disabled={props.isSubmitting}
761
+ >
762
+ {props.isSubmitting ? (
763
+ <CircularProgress size={26} />
764
+ ) : (
765
+ buttonName
766
+ )}
767
+ </Button>
768
+ </div>
769
+ </div>
770
+ </Form>
771
+ )}
772
+ </Formik>
773
+ {showModalLogin && (
774
+ <LoginModal
775
+ onClose={() => {
776
+ setShowModalLogin(false)
777
+ }}
778
+ onLogin={() => {
779
+ setShowModalLogin(false)
780
+ setUserExpired(false)
781
+ onLoginSuccess()
782
+ }}
783
+ alreadyHasUser={alreadyHasUser}
784
+ userExpired={userExpired}
785
+ onAuthorizeSuccess={onAuthorizeSuccess}
786
+ onAuthorizeError={onAuthorizeError}
787
+ onGetProfileDataSuccess={(data: any) => {
788
+ fetchCart()
789
+ onGetProfileDataSuccess(data)
790
+ }}
791
+ onGetProfileDataError={onGetProfileDataError}
792
+ />
793
+ )}
794
+ {showModalRegister && (
795
+ <RegisterModal
796
+ onClose={() => {
797
+ setShowModalRegister(false)
798
+ }}
799
+ onRegister={() => {
800
+ setShowModalRegister(false)
801
+ }}
802
+ onGetProfileDataSuccess={(data: any) => {
803
+ fetchCart()
804
+ onGetProfileDataSuccess(data)
805
+ }}
806
+ onGetProfileDataError={onGetProfileDataError}
807
+ />
808
+ )}
809
+ </ThemeProvider>
810
+ )
811
+ }