baby_in_magento

customerform

Mar 28th, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.67 KB | None | 0 0
  1. import React, { Fragment } from 'react';
  2. import { FormattedMessage, useIntl } from 'react-intl';
  3. import { Form, Text } from 'informed';
  4. import { arrayOf, bool, func, number, shape, string } from 'prop-types';
  5. import { useCustomerForm } from './useCustomerForm';
  6.  
  7. import { useStyle } from '@magento/venia-ui/lib/classify';
  8. import { isRequired } from '@magento/venia-ui/lib/util/formValidators';
  9. import Button from '@magento/venia-ui/lib/components/Button';
  10. import Checkbox from '@magento/venia-ui/lib/components/Checkbox';
  11. import Country from '@magento/venia-ui/lib/components/Country';
  12. import Field, { Message } from '@magento/venia-ui/lib/components/Field';
  13. import FormError from '@magento/venia-ui/lib/components/FormError';
  14. import Region from '@magento/venia-ui/lib/components/Region';
  15. import Postcode from '@magento/venia-ui/lib/components/Postcode';
  16. import TextInput from '@magento/venia-ui/lib/components/TextInput';
  17. import defaultClasses from './customerForm.module.css';
  18. import LoadingIndicator from '../../../LoadingIndicator';
  19. import { usePriceSummary } from '@magento/peregrine/lib/talons/CartPage/PriceSummary/usePriceSummary';
  20.  
  21. const CustomerForm = props => {
  22. const {
  23. afterSubmit,
  24. classes: propClasses,
  25. onCancel,
  26. onSuccess,
  27. shippingData
  28. } = props;
  29.  
  30. const talonProps = useCustomerForm({
  31. afterSubmit,
  32. onCancel,
  33. onSuccess,
  34. shippingData
  35. });
  36. const {
  37. errors,
  38. handleCancel,
  39. handleSubmit,
  40. hasDefaultShipping,
  41. initialValues,
  42. isLoading,
  43. isSaving,
  44. isUpdate
  45. } = talonProps;
  46.  
  47.  
  48. const {
  49. flatData
  50. }=usePriceSummary();
  51.  
  52. const {
  53. total,
  54. } = flatData;
  55.  
  56. console.log(flatData,'hioh')
  57. const { formatMessage } = useIntl();
  58. const classes = useStyle(defaultClasses, propClasses);
  59.  
  60. if (isLoading) {
  61. return (
  62. <LoadingIndicator>
  63. <FormattedMessage
  64. id={'customerForm.loading'}
  65. defaultMessage={'Fetching Customer Details...'}
  66. />
  67. </LoadingIndicator>
  68. );
  69. }
  70.  
  71. const emailRow = !hasDefaultShipping ? (
  72. <div className={classes.email}>
  73. <Field
  74. id="email"
  75. label={formatMessage({
  76. id: 'global.email',
  77. defaultMessage: 'Email'
  78. })}
  79. >
  80. <TextInput
  81. disabled={true}
  82. field="email"
  83. id="email"
  84. validate={isRequired}
  85. />
  86. </Field>
  87. </div>
  88. ) : null;
  89.  
  90. const formMessageRow = !hasDefaultShipping ? (
  91. <div data-cy="CustomerForm-formMessage" className={classes.formMessage}>
  92. <Message>
  93. <FormattedMessage
  94. id={'customerForm.formMessage'}
  95. defaultMessage={
  96. 'The shipping address you enter will be saved to your address book and set as your default for future purchases.'
  97. }
  98. />
  99. </Message>
  100. </div>
  101. ) : null;
  102.  
  103. const cancelButton = isUpdate ? (
  104. <Button disabled={isSaving} onClick={handleCancel} priority="low">
  105. <FormattedMessage
  106. id={'global.cancelButton'}
  107. defaultMessage={'Cancel'}
  108. />
  109. </Button>
  110. ) : null;
  111.  
  112. const submitButtonText = !hasDefaultShipping
  113. ? formatMessage({
  114. id: 'global.saveAndContinueButton',
  115. defaultMessage: 'Save and Continue'
  116. })
  117. : isUpdate
  118. ? formatMessage({
  119. id: 'global.updateButton',
  120. defaultMessage: 'Update'
  121. })
  122. : formatMessage({
  123. id: 'global.addButton',
  124. defaultMessage: 'Add'
  125. });
  126. const submitButtonProps = {
  127. disabled: isSaving,
  128. priority: !hasDefaultShipping ? 'normal' : 'high',
  129. type: 'submit'
  130. };
  131.  
  132. const defaultShippingElement = hasDefaultShipping ? (
  133. <div className={classes.defaultShipping}>
  134. <Checkbox
  135. disabled={!!initialValues.default_shipping}
  136. id="default_shipping"
  137. data-cy="CustomerForm-defaultShipping"
  138. field="default_shipping"
  139. label={formatMessage({
  140. id: 'customerForm.defaultShipping',
  141. defaultMessage: 'Make this my default address'
  142. })}
  143. />
  144. </div>
  145. ) : (
  146. <Text type="hidden" field="default_shipping" initialValue={true} />
  147. );
  148.  
  149. return (
  150. <Fragment>
  151. <FormError errors={Array.from(errors.values())} />
  152. <Form
  153. className={classes.root}
  154. data-cy="CustomerForm-root"
  155. initialValues={initialValues}
  156. onSubmit={handleSubmit}
  157. >
  158. {formMessageRow}
  159. {emailRow}
  160. <div className={classes.firstname}>
  161. <Field
  162. id="customer_firstname"
  163. label={formatMessage({
  164. id: 'global.firstName',
  165. defaultMessage: 'First Name'
  166. })}
  167. >
  168. <TextInput
  169. disabled={!hasDefaultShipping}
  170. field="firstname"
  171. id="customer_firstname"
  172. data-cy="CustomerForm-firstName"
  173. validate={isRequired}
  174. />
  175. </Field>
  176. </div>
  177. <div className={classes.lastname}>
  178. <Field
  179. id="customer_lastname"
  180. label={formatMessage({
  181. id: 'global.lastName',
  182. defaultMessage: 'Last Name'
  183. })}
  184. >
  185. <TextInput
  186. disabled={!hasDefaultShipping}
  187. field="lastname"
  188. id="customer_lastname"
  189. data-cy="CustomerForm-lastName"
  190. validate={isRequired}
  191. />
  192. </Field>
  193. </div>
  194.  
  195. <div className={classes.street0}>
  196. <Field
  197. id="customer_street0"
  198. label={formatMessage({
  199. id: 'global.streetAddress',
  200. defaultMessage: 'Street Address'
  201. })}
  202. >
  203. <TextInput
  204. field="street[0]"
  205. validate={isRequired}
  206. id="customer_street0"
  207. data-cy="CustomerForm-street0"
  208. />
  209. </Field>
  210. </div>
  211. <div className={classes.firstname}>
  212. <Field
  213. id="customer_street1"
  214. label={formatMessage({
  215. id: 'global.streetAddress2',
  216. defaultMessage: 'Street Address 2'
  217. })}
  218. optional={true}
  219. >
  220. <TextInput
  221. field="street[1]"
  222. id="customer_street1"
  223. data-cy="CustomerForm-street1"
  224. />
  225. </Field>
  226. </div>
  227. <div className={classes.firstname}>
  228. <Field
  229. id="customer_city"
  230. label={formatMessage({
  231. id: 'global.city',
  232. defaultMessage: 'City'
  233. })}
  234. >
  235. <TextInput
  236. field="city"
  237. validate={isRequired}
  238. id="customer_city"
  239. data-cy="CustomerForm-city"
  240. />
  241. </Field>
  242. </div>
  243.  
  244. <div className={classes.firstname}>
  245. <Country
  246. validate={isRequired}
  247. data-cy="CustomerForm-country"
  248. />
  249. </div>
  250. {total?.value >200000 ?
  251. <div className={classes.pan_number + " col-span-2"}>
  252. <Field
  253. id="pan_number"
  254. label={formatMessage({
  255. id: 'global.pan_number',
  256. defaultMessage: 'PAN'
  257. })}
  258. >
  259. <TextInput
  260. autoComplete={formatMessage({
  261. id: 'global.pan_number',
  262. defaultMessage: 'PAN'
  263. })}
  264. field="pan_number"
  265. id="pan_number"
  266. data-cy="GuestForm-pan_number"
  267. validate={isRequired}
  268. />
  269. </Field>
  270. </div>:null}
  271. <div className={classes.firstname}>
  272. <Region
  273. validate={isRequired}
  274. data-cy="CustomerForm-region"
  275. fieldInput={'region[region]'}
  276. fieldSelect={'region[region_id]'}
  277. optionValueKey={'id'}
  278. />
  279. </div>
  280.  
  281. <div className={classes.firstname}>
  282. <Postcode
  283. validate={isRequired}
  284. data-cy="CustomerForm-postcode"
  285. />
  286. </div>
  287. <div className={classes.firstname}>
  288. <Field
  289. id="customer_telephone"
  290. label={formatMessage({
  291. id: 'global.phoneNumber',
  292. defaultMessage: 'Phone Number'
  293. })}
  294. >
  295. <TextInput
  296. field="telephone"
  297. validate={isRequired}
  298. id="customer_telephone"
  299. data-cy="CustomerForm-telephone"
  300. />
  301. </Field>
  302. </div>
  303. {defaultShippingElement}
  304. <div className={classes.buttons + ' !justify-self-start !p-0'}>
  305. {cancelButton}
  306. <Button
  307. {...submitButtonProps}
  308. data-cy="CustomerForm-submitButton"
  309. className="font-lato text-[12px] text-[#fff] bg-[#181A1D] py-[1rem] px-[2rem] tracking-[2.4px] uppercase mr-4 rounded search_button"
  310. >
  311. {submitButtonText}
  312. </Button>
  313. </div>
  314. </Form>
  315. </Fragment>
  316. );
  317. };
  318.  
  319. export default CustomerForm;
  320.  
  321. CustomerForm.defaultProps = {
  322. shippingData: {
  323. country: {
  324. code: DEFAULT_COUNTRY_CODE
  325. },
  326. region: {
  327. id: null
  328. }
  329. }
  330. };
  331.  
  332. CustomerForm.propTypes = {
  333. afterSubmit: func,
  334. classes: shape({
  335. root: string,
  336. field: string,
  337. email: string,
  338. firstname: string,
  339. lastname: string,
  340. country: string,
  341. pan_number:string,
  342. street0: string,
  343. street1: string,
  344. city: string,
  345. region: string,
  346. postcode: string,
  347. telephone: string,
  348. buttons: string,
  349. formMessage: string,
  350. defaultShipping: string
  351. }),
  352. onCancel: func,
  353. shippingData: shape({
  354. city: string,
  355. country: shape({
  356. code: string.isRequired
  357. }).isRequired,
  358. default_shipping: bool,
  359. email: string,
  360. firstname: string,
  361. id: number,
  362. lastname: string,
  363. postcode: string,
  364. region: shape({
  365. id: number
  366. }).isRequired,
  367. street: arrayOf(string),
  368. pan_number:string,
  369. telephone: string
  370. })
  371. };
  372.  
Advertisement
Add Comment
Please, Sign In to add comment