Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ShippingDetailsForm.jsx 5.2KB

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