Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ShippingDetailsForm.jsx 5.4KB

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