baby_in_magento

guestform.js

Mar 28th, 2024
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.58 KB | None | 0 0
  1. import React, { Fragment, useEffect, useRef } from 'react';
  2. import { FormattedMessage, useIntl } from 'react-intl';
  3. import { Form } from 'informed';
  4. import { func, shape, string, arrayOf, number } from 'prop-types';
  5. import { AlertCircle } from 'react-feather';
  6. import { useGuestForm } from './useGuestForm';
  7. import { useToasts } from '@magento/peregrine';
  8.  
  9. import { useStyle } from '@magento/venia-ui/lib/classify';
  10. import { isRequired } from '@magento/venia-ui/lib/util/formValidators';
  11. import Button from '@magento/venia-ui/lib/components/Button';
  12. import Country from '@magento/venia-ui/lib/components/Country';
  13. import Field, { Message } from '@magento/venia-ui/lib/components/Field';
  14. import FormError from '@magento/venia-ui/lib/components/FormError';
  15. import Region from '@magento/venia-ui/lib/components/Region';
  16. import Postcode from '@magento/venia-ui/lib/components/Postcode';
  17. import TextInput from '@magento/venia-ui/lib/components/TextInput';
  18. import Icon from '@magento/venia-ui/lib/components/Icon';
  19. import { usePriceSummary } from '@magento/peregrine/lib/talons/CartPage/PriceSummary/usePriceSummary';
  20.  
  21. const AlertCircleIcon = <Icon src={AlertCircle} attrs={{ width: 20 }} />;
  22.  
  23. const GuestForm = props => {
  24. const {
  25. afterSubmit,
  26. classes: propClasses,
  27. onCancel,
  28. onSuccess,
  29. shippingData,
  30. toggleSignInContent,
  31. setGuestSignInUsername
  32. } = props;
  33.  
  34. const talonProps = useGuestForm({
  35. afterSubmit,
  36. onCancel,
  37. onSuccess,
  38. shippingData,
  39. toggleSignInContent,
  40. setGuestSignInUsername
  41. });
  42.  
  43. const {
  44. flatData
  45. }=usePriceSummary();
  46.  
  47. const {
  48. errors,
  49. handleCancel,
  50. handleSubmit,
  51. initialValues,
  52. isSaving,
  53. isUpdate,
  54. handleValidateEmail,
  55. showSignInToast,
  56. handleToastAction
  57. } = talonProps;
  58.  
  59. const {
  60. total,
  61. } = flatData;
  62. const formApiRef = useRef();
  63. const getFormApi = api => {
  64. formApiRef.current = api;
  65. };
  66.  
  67. const [, { addToast }] = useToasts();
  68.  
  69. const { formatMessage } = useIntl();
  70. const classes = useStyle({}, propClasses);
  71.  
  72. const guestEmailMessage = !isUpdate ? (
  73. <Message>
  74. <FormattedMessage
  75. id={'guestForm.emailMessage'}
  76. defaultMessage={
  77. 'Set a password at the end of guest checkout to create an account in one easy step.'
  78. }
  79. />
  80. </Message>
  81. ) : null;
  82.  
  83. const cancelButton = isUpdate ? (
  84. <Button disabled={isSaving} onClick={handleCancel} priority="low">
  85. <FormattedMessage
  86. id={'global.cancelButton'}
  87. defaultMessage={'Cancel'}
  88. />
  89. </Button>
  90. ) : null;
  91.  
  92. const continueToMessage = formatMessage({
  93. id: 'guestForm.continueToNextStep'
  94. })
  95.  
  96. const submitButtonText = isUpdate
  97. ? formatMessage({
  98. id: 'global.updateButton',
  99. defaultMessage: 'Update'
  100. })
  101. : formatMessage({
  102. id: 'guestForm.continueToNextStep',
  103. defaultMessage: 'Save and continue'
  104. });
  105. const submitButtonProps = {
  106. disabled: isSaving,
  107. priority: isUpdate ? 'high' : 'normal',
  108. type: 'submit'
  109. };
  110.  
  111. useEffect(() => {
  112. if (showSignInToast) {
  113. addToast({
  114. type: 'info',
  115. icon: AlertCircleIcon,
  116. message: formatMessage({
  117. id: 'checkoutPage.suggestSignInMessage',
  118. defaultMessage:
  119. 'The email you provided is associated with an existing Venia account. Would you like to sign into this account?'
  120. }),
  121. timeout: false,
  122. dismissable: true,
  123. hasDismissAction: true,
  124. dismissActionText: formatMessage({
  125. id: 'checkoutPage.suggestSignInDeclineMessage',
  126. defaultMessage: 'No, thanks'
  127. }),
  128. actionText: formatMessage({
  129. id: 'checkoutPage.suggestSignInConfirmMessage',
  130. defaultMessage: 'Yes, sign in'
  131. }),
  132. onAction: removeToast =>
  133. handleToastAction(
  134. removeToast,
  135. formApiRef.current.getValue('email')
  136. )
  137. });
  138. }
  139. }, [addToast, formatMessage, showSignInToast, handleToastAction]);
  140.  
  141. return (
  142. <Fragment>
  143. <FormError errors={Array.from(errors.values())} />
  144. <Form
  145. className={classes.root + " grid grid-cols-2 gap-y-[1.5rem] gap-x-4"}
  146. data-cy="GuestForm-root"
  147. initialValues={initialValues}
  148. onSubmit={handleSubmit}
  149. getApi={getFormApi}
  150. >
  151. <div className={classes.email + " col-span-2"}>
  152. <Field
  153. id="email"
  154. label={formatMessage({
  155. id: 'global.email',
  156. defaultMessage: 'Email'
  157. })}
  158. >
  159. <TextInput
  160. autoComplete={formatMessage({
  161. id: 'shippingForm.shippingEmail',
  162. defaultMessage: 'Shipping Email'
  163. })}
  164. field="email"
  165. id="email"
  166. data-cy="GuestForm-email"
  167. validate={isRequired}
  168. onBlur={() =>
  169. handleValidateEmail(
  170. formApiRef.current.getValue('email')
  171. )
  172. }
  173. onPaste={e => {
  174. const text = e.clipboardData.getData(
  175. 'text/plain'
  176. );
  177. handleValidateEmail(text);
  178. }}
  179. />
  180. {guestEmailMessage}
  181. </Field>
  182. </div>
  183. <div className={classes.firstname}>
  184. <Field
  185. id="firstname"
  186. label={formatMessage({
  187. id: 'global.firstName',
  188. defaultMessage: 'First Name'
  189. })}
  190. >
  191. <TextInput
  192. autoComplete={formatMessage({
  193. id: 'global.firstName',
  194. defaultMessage: 'First Name'
  195. })}
  196. field="firstname"
  197. id="firstname"
  198. data-cy="GuestForm-firstName"
  199. validate={isRequired}
  200. />
  201. </Field>
  202. </div>
  203. <div className={classes.lastname}>
  204. <Field
  205. id="lastname"
  206. label={formatMessage({
  207. id: 'global.lastName',
  208. defaultMessage: 'Last Name'
  209. })}
  210. >
  211. <TextInput
  212. autoComplete={formatMessage({
  213. id: 'global.lastName',
  214. defaultMessage: 'Last Name'
  215. })}
  216. field="lastname"
  217. id="lastname"
  218. data-cy="GuestForm-lastName"
  219. validate={isRequired}
  220. />
  221. </Field>
  222. </div>
  223. <div className={classes.country + " col-span-2"}>
  224. <Country
  225. autoComplete={formatMessage({
  226. id: 'country.label',
  227. defaultMessage: 'Country'
  228. })}
  229. validate={isRequired}
  230. data-cy="GuestForm-country"
  231. />
  232. </div>
  233. {total?.value >200000 ?
  234. <div className={classes.pan_number + " col-span-2"}>
  235. <Field
  236. id="pan_number"
  237. label={formatMessage({
  238. id: 'global.pan_number',
  239. defaultMessage: 'PAN'
  240. })}
  241. >
  242. <TextInput
  243. autoComplete={formatMessage({
  244. id: 'global.pan_number',
  245. defaultMessage: 'PAN'
  246. })}
  247. field="pan_number"
  248. id="pan_number"
  249. data-cy="GuestForm-pan_number"
  250. validate={isRequired}
  251. />
  252. </Field>
  253. </div>:null}
  254. <div className={classes.street0 + " col-span-2"}>
  255. <Field
  256. id="street0"
  257. label={formatMessage({
  258. id: 'global.streetAddress',
  259. defaultMessage: 'Street Address'
  260. })}
  261. >
  262. <TextInput
  263. autoComplete={formatMessage({
  264. id: 'global.streetAddress',
  265. defaultMessage: 'Street Address'
  266. })}
  267. field="street[0]"
  268. id="street0"
  269. data-cy="GuestForm-street0"
  270. validate={isRequired}
  271. />
  272. </Field>
  273. </div>
  274. <div className={classes.street1 + " col-span-2"}>
  275. <Field
  276. id="street1"
  277. label={formatMessage({
  278. id: 'global.streetAddress2',
  279. defaultMessage: 'Street Address 2'
  280. })}
  281. optional={true}
  282. >
  283. <TextInput
  284. autoComplete={formatMessage({
  285. id: 'global.streetAddress2',
  286. defaultMessage: 'Street Address 2'
  287. })}
  288. field="street[1]"
  289. id="street1"
  290. data-cy="GuestForm-street1"
  291. />
  292. </Field>
  293. </div>
  294. <div className={classes.city + " col-span-2"}>
  295. <Field
  296. id="city"
  297. label={formatMessage({
  298. id: 'global.city',
  299. defaultMessage: 'City'
  300. })}
  301. >
  302. <TextInput
  303. autoComplete={formatMessage({
  304. id: 'global.city',
  305. defaultMessage: 'City'
  306. })}
  307. field="city"
  308. id="city"
  309. data-cy="GuestForm-city"
  310. validate={isRequired}
  311. />
  312. </Field>
  313. </div>
  314. <div className={classes.region + " col-span-2"}>
  315. <Region
  316. autoComplete={formatMessage({
  317. id: 'region.label',
  318. defaultMessage: 'State'
  319. })}
  320. validate={isRequired}
  321. fieldInput={'region[region]'}
  322. fieldSelect={'region[region_id]'}
  323. optionValueKey={'id'}
  324. data-cy="GuestForm-region"
  325. />
  326. </div>
  327. <div className={classes.postcode + " col-span-2"}>
  328. <Postcode
  329. autoComplete={formatMessage({
  330. id: 'postcode.label',
  331. defaultMessage: 'ZIP / Postal Code'
  332. })}
  333. validate={isRequired}
  334. data-cy="GuestForm-postcode"
  335. />
  336. </div>
  337. <div className={classes.telephone + " col-span-2"}>
  338. <Field
  339. id="telephone"
  340. label={formatMessage({
  341. id: 'global.phoneNumber',
  342. defaultMessage: 'Phone Number'
  343. })}
  344. >
  345. <TextInput
  346. autoComplete={formatMessage({
  347. id: 'global.phoneNumber',
  348. defaultMessage: 'Phone Number'
  349. })}
  350. field="telephone"
  351. id="telephone"
  352. data-cy="GuestForm-telephone"
  353. validate={isRequired}
  354. />
  355. </Field>
  356. </div>
  357. <div className={classes.buttons}>
  358. {cancelButton}
  359. <Button
  360. {...submitButtonProps}
  361. className="font-lato text-[12px] text-[#fff] bg-[#181A1D] py-[1rem] px-[2rem] tracking-[2.4px] uppercase mr-4 rounded search_button"
  362. data-cy="GuestForm-submitButton"
  363. >
  364. {submitButtonText}
  365. </Button>
  366. </div>
  367. </Form>
  368. </Fragment>
  369. );
  370. };
  371.  
  372. export default GuestForm;
  373.  
  374. GuestForm.defaultProps = {
  375. shippingData: {
  376. country: {
  377. code: DEFAULT_COUNTRY_CODE
  378. },
  379. region: {
  380. code: ''
  381. }
  382. }
  383. };
  384.  
  385. GuestForm.propTypes = {
  386. afterSubmit: func,
  387. classes: shape({
  388. root: string,
  389. field: string,
  390. email: string,
  391. firstname: string,
  392. lastname: string,
  393. country: string,
  394. street0: string,
  395. street1: string,
  396. city: string,
  397. pan_number:string,
  398. region: string,
  399. postcode: string,
  400. telephone: string,
  401. buttons: string,
  402. submit: string,
  403. submit_update: string
  404. }),
  405. onCancel: func,
  406. onSuccess: func.isRequired,
  407. shippingData: shape({
  408. city: string,
  409. country: shape({
  410. code: string.isRequired
  411. }).isRequired,
  412. email: string,
  413. firstname: string,
  414. lastname: string,
  415. postcode: string,
  416. region: shape({
  417. region_id: number,
  418. region: string
  419. }).isRequired,
  420. street: arrayOf(string),
  421. pan_number:string,
  422. telephone: string
  423. }),
  424. toggleSignInContent: func.isRequired,
  425. setGuestSignInUsername: func.isRequired
  426. };
  427.  
Advertisement
Add Comment
Please, Sign In to add comment