You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShippingDetailsForm.jsx 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { Box, Button, Paper, TextField } from '@mui/material';
  2. import { useFormik } from 'formik';
  3. import { useSession } from 'next-auth/react';
  4. import PropType from 'prop-types';
  5. import { useState } from 'react';
  6. import { shippingDetailsSchema } from '../../../schemas/shippingDetailsSchema';
  7. import ErrorMessageComponent from '../../mui/ErrorMessageComponent';
  8. const ShippingDetailsForm = ({
  9. backBtn = false,
  10. isCheckout = false,
  11. submitHandler,
  12. }) => {
  13. const [error] = useState({ hasError: false, errorMessage: '' });
  14. const { data: session } = useSession();
  15. const formikSubmitHandler = async (values) => {
  16. console.log('hi');
  17. submitHandler(values);
  18. };
  19. const formik = useFormik({
  20. initialValues: {
  21. fullName: session ? session.user.fullName : '',
  22. address: session ? session.user.address : '',
  23. address2: session ? session.user.address2 : '',
  24. city: session ? session.user.city : '',
  25. country: session ? session.user.country : '',
  26. postcode: session ? session.user.postcode : '',
  27. },
  28. validationSchema: shippingDetailsSchema,
  29. onSubmit: formikSubmitHandler,
  30. validateOnBlur: true,
  31. enableReinitialize: true,
  32. });
  33. return (
  34. <Paper
  35. sx={{ p: 3, width: '90%', ml: 12, mt: 2, backgroundColor: '#f2f2f2' }}
  36. elevation={3}
  37. >
  38. <Box
  39. sx={{
  40. width: '100%',
  41. display: 'flex',
  42. flexDirection: 'column',
  43. }}
  44. >
  45. {error.hasError && <ErrorMessageComponent error={error.errorMessage} />}
  46. <Box
  47. component="form"
  48. onSubmit={formik.handleSubmit}
  49. sx={{ position: 'relative', mt: 1, p: 1 }}
  50. >
  51. <TextField
  52. name="fullName"
  53. label="Name"
  54. margin="normal"
  55. value={formik.values.fullName}
  56. onChange={formik.handleChange}
  57. error={formik.touched.fullName && Boolean(formik.errors.fullName)}
  58. helperText={formik.touched.fullName && formik.errors.fullName}
  59. fullWidth
  60. />
  61. <TextField
  62. name="address"
  63. label="Address"
  64. margin="normal"
  65. value={formik.values.address}
  66. onChange={formik.handleChange}
  67. error={formik.touched.address && Boolean(formik.errors.address)}
  68. helperText={formik.touched.address && formik.errors.address}
  69. fullWidth
  70. />
  71. <TextField
  72. name="address2"
  73. label="Address Line 2"
  74. margin="normal"
  75. value={formik.values.address2}
  76. onChange={formik.handleChange}
  77. error={formik.touched.address2 && Boolean(formik.errors.address2)}
  78. helperText={formik.touched.address2 && formik.errors.address2}
  79. fullWidth
  80. />
  81. <TextField
  82. name="city"
  83. label="City"
  84. margin="normal"
  85. value={formik.values.city}
  86. onChange={formik.handleChange}
  87. error={formik.touched.city && Boolean(formik.errors.city)}
  88. helperText={formik.touched.city && formik.errors.city}
  89. fullWidth
  90. />
  91. <Box sx={{ display: 'flex' }}>
  92. <TextField
  93. name="country"
  94. label="Country"
  95. margin="normal"
  96. value={formik.values.country}
  97. onChange={formik.handleChange}
  98. error={formik.touched.country && Boolean(formik.errors.country)}
  99. helperText={formik.touched.country && formik.errors.country}
  100. fullWidth
  101. sx={{ mr: 1.5 }}
  102. />
  103. <TextField
  104. name="postcode"
  105. label="Postal Code"
  106. margin="normal"
  107. value={formik.values.postcode}
  108. onChange={formik.handleChange}
  109. error={formik.touched.postcode && Boolean(formik.errors.postcode)}
  110. helperText={formik.touched.postcode && formik.errors.postcode}
  111. fullWidth
  112. />
  113. </Box>
  114. {backBtn && (
  115. <Button
  116. variant="contained"
  117. sx={{
  118. mt: 3,
  119. mb: 2,
  120. height: 50,
  121. width: 150,
  122. textTransform: 'none',
  123. backgroundColor: 'primary.main',
  124. color: 'white',
  125. mr: 2,
  126. }}
  127. >
  128. Back to cart
  129. </Button>
  130. )}
  131. <Button
  132. type="submit"
  133. variant="contained"
  134. sx={{
  135. mt: 3,
  136. mb: 2,
  137. backgroundColor: '#CBA213',
  138. height: 50,
  139. width: isCheckout ? 200 : 150,
  140. textTransform: 'none',
  141. color: 'white',
  142. }}
  143. onClick={() => {
  144. submitHandler;
  145. }}
  146. >
  147. {isCheckout ? 'Proceed to shipping' : 'Submit Details'}
  148. </Button>
  149. </Box>
  150. </Box>
  151. </Paper>
  152. );
  153. };
  154. ShippingDetailsForm.propTypes = {
  155. backBtn: PropType.Boolean,
  156. isCheckout: PropType.Boolean,
  157. submitHandler: PropType.func,
  158. };
  159. export default ShippingDetailsForm;