-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathone-click-user-registration.jsx
More file actions
179 lines (170 loc) · 7.01 KB
/
one-click-user-registration.jsx
File metadata and controls
179 lines (170 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (c) 2025, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React, {useRef} from 'react'
import {FormattedMessage} from 'react-intl'
import PropTypes from 'prop-types'
import {
Box,
Checkbox,
Stack,
Text,
Heading,
useDisclosure
} from '@salesforce/retail-react-app/app/components/shared/ui'
import OtpAuth from '@salesforce/retail-react-app/app/components/otp-auth'
import {useCurrentBasket} from '@salesforce/retail-react-app/app/hooks/use-current-basket'
import {useCustomerType, useAuthHelper, AuthHelpers} from '@salesforce/commerce-sdk-react'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import {useAppOrigin} from '@salesforce/retail-react-app/app/hooks/use-app-origin'
import {isAbsoluteURL} from '@salesforce/retail-react-app/app/page-designer/utils'
export default function UserRegistration({
enableUserRegistration,
setEnableUserRegistration,
isGuestCheckout = false,
isDisabled = false,
onSavePreferenceChange,
onRegistered
}) {
const {data: basket} = useCurrentBasket()
const {isGuest} = useCustomerType()
const authorizePasswordlessLogin = useAuthHelper(AuthHelpers.AuthorizePasswordless)
const loginPasswordless = useAuthHelper(AuthHelpers.LoginPasswordlessUser)
const appOrigin = useAppOrigin()
const passwordlessConfigCallback = getConfig().app.login?.passwordless?.callbackURI
const callbackURL = isAbsoluteURL(passwordlessConfigCallback)
? passwordlessConfigCallback
: `${appOrigin}${passwordlessConfigCallback}`
const {isOpen: isOtpOpen, onOpen: onOtpOpen, onClose: onOtpClose} = useDisclosure()
const otpSentRef = useRef(false)
const handleOtpClose = () => {
otpSentRef.current = false
onOtpClose()
}
const handleUserRegistrationChange = async (e) => {
const checked = e.target.checked
setEnableUserRegistration(checked)
// Treat opting into registration as opting to save for future
if (onSavePreferenceChange) onSavePreferenceChange(checked)
// If user unchecks, allow OTP to be re-triggered upon re-check
if (!checked) {
otpSentRef.current = false
}
// Kick off OTP for guests when they opt in
if (checked && isGuest && basket?.customerInfo?.email && !otpSentRef.current) {
try {
await authorizePasswordlessLogin.mutateAsync({
userid: basket.customerInfo.email,
callbackURI: `${callbackURL}?mode=otp_email`,
register_customer: true,
last_name: basket.customerInfo.email,
email: basket.customerInfo.email
})
otpSentRef.current = true
onOtpOpen()
} catch (_e) {
// Silent failure; user can continue as guest
}
}
}
const handleOtpVerification = async (otpCode) => {
try {
await loginPasswordless.mutateAsync({
pwdlessLoginToken: otpCode,
register_customer: true
})
if (onRegistered) {
await onRegistered(basket?.basketId)
}
handleOtpClose()
} catch (_e) {
// Let OtpAuth surface errors via its own UI/toast
}
return {success: true}
}
// Hide the form if the "Checkout as Guest" button was clicked
if (isGuestCheckout) {
return null
}
return (
<>
<Box
border="1px solid"
borderColor="gray.200"
rounded="md"
p={4}
data-testid="sf-user-registration-content"
>
<Stack spacing={2}>
<Heading fontSize="lg" lineHeight="30px" tabIndex="0">
<FormattedMessage
defaultMessage="Save Checkout Info for Future Use"
id="checkout.title.user_registration"
/>
</Heading>
<Checkbox
name="userRegistration"
isChecked={enableUserRegistration}
onChange={handleUserRegistrationChange}
isDisabled={isDisabled}
alignItems="flex-start"
>
<Stack spacing={1}>
<Text>
<FormattedMessage
defaultMessage="Create an account to check out faster"
id="checkout.label.user_registration"
/>
</Text>
{enableUserRegistration && (
<Text fontSize="sm" color="gray.500">
<FormattedMessage
defaultMessage="Your payment, address, and contact information will be saved in a new account. Use the emailed one-time password (OTP) to create your account. After creating your account, use the Forgot Password function to set a new password."
id="checkout.message.user_registration"
/>
</Text>
)}
</Stack>
</Checkbox>
</Stack>
</Box>
{/* OTP modal lives with registration now */}
<OtpAuth
isOpen={isOtpOpen}
onClose={handleOtpClose}
isGuestRegistration
form={{
getValues: (name) =>
name === 'email' ? basket?.customerInfo?.email : undefined,
setValue: () => {}
}}
handleSendEmailOtp={async (email) => {
return authorizePasswordlessLogin.mutateAsync({
userid: email,
callbackURI: `${callbackURL}?mode=otp_email`,
register_customer: true,
last_name: email,
email
})
}}
handleOtpVerification={handleOtpVerification}
/>
</>
)
}
UserRegistration.propTypes = {
/** Whether user registration is enabled */
enableUserRegistration: PropTypes.bool,
/** Callback to set user registration state */
setEnableUserRegistration: PropTypes.func,
/** Whether the "Checkout as Guest" button was clicked */
isGuestCheckout: PropTypes.bool,
/** Disable the registration checkbox (e.g., until payment info is filled) */
isDisabled: PropTypes.bool,
/** Callback to set save-for-future preference */
onSavePreferenceChange: PropTypes.func,
onRegistered: PropTypes.func
}