Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ShippingDetailsForm.tsx 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 { useState } from 'react';
  6. import { registerSchema } 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 { t } = useTranslation('addressForm');
  16. const [error] = useState({ hasError: false, errorMessage: '' });
  17. const { userStorage } = useUserData();
  18. const router = useRouter();
  19. const formikSubmitHandler = async (values) => {
  20. submitHandler(values);
  21. };
  22. const formik = useFormik({
  23. initialValues: {
  24. fullName: userStorage ? userStorage.fullName : '',
  25. address: userStorage ? userStorage.address : '',
  26. address2: userStorage ? userStorage.address2 : '',
  27. city: userStorage ? userStorage.city : '',
  28. country: userStorage ? userStorage.country : '',
  29. postcode: userStorage ? userStorage.postcode : '',
  30. },
  31. validationSchema: registerSchema,
  32. onSubmit: formikSubmitHandler,
  33. validateOnBlur: true,
  34. enableReinitialize: true,
  35. });
  36. return (
  37. <Card sx={{ p: 3, backgroundColor: '#f2f2f2' }}>
  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={t('addressForm: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={t('addressForm: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={t('addressForm:address2')}
  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={t('addressForm: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={t('addressForm: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={t('addressForm:postcode')}
  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. onClick={() => {
  128. router.push('/cart');
  129. }}
  130. >
  131. {t('addressForm:back')}
  132. </Button>
  133. )}
  134. <Button
  135. type="submit"
  136. variant="contained"
  137. sx={{
  138. mt: 3,
  139. mb: 2,
  140. backgroundColor: '#CBA213',
  141. height: 50,
  142. width: isCheckout ? 200 : 150,
  143. textTransform: 'none',
  144. color: 'white',
  145. }}
  146. disabled={!enableBtn}
  147. onClick={() => {
  148. submitHandler;
  149. }}
  150. >
  151. {isCheckout ? t('addressForm:shipping') : t('addressForm:submit')}
  152. </Button>
  153. </Box>
  154. </Box>
  155. </Card>
  156. );
  157. };
  158. export default ShippingDetailsForm;