tf-checkout-react 1.0.99 → 1.0.100-beta.2

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