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.

ContactPageForm.jsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {
  2. Alert,
  3. Box,
  4. Button,
  5. Container,
  6. Grid,
  7. Snackbar,
  8. TextField,
  9. Typography,
  10. } from '@mui/material';
  11. import { useFormik } from 'formik';
  12. import { useTranslation } from 'next-i18next';
  13. import Link from 'next/link';
  14. import PropType from 'prop-types';
  15. import React, { useState } from 'react';
  16. import { BASE_PAGE } from '../../../constants/pages';
  17. import { postQuestion } from '../../../requests/question/postQuestionRequest';
  18. import { contactPageSchema } from '../../../schemas/contactSchema';
  19. const ContactPageForm = () => {
  20. const { t } = useTranslation('forms', 'contact', 'common');
  21. const [open, setOpen] = useState(false);
  22. //const [error] = useState({ hasError: false, errorMessage: '' });
  23. const handleSubmit = async (values) => {
  24. try {
  25. postQuestion(values);
  26. setOpen(true);
  27. } catch (error) {
  28. console.log(error);
  29. }
  30. };
  31. const handleCloseNotification = () => {
  32. setOpen(false);
  33. };
  34. const formik = useFormik({
  35. initialValues: {
  36. firstName: '',
  37. lastName: '',
  38. email: '',
  39. message: '',
  40. },
  41. validationSchema: contactPageSchema,
  42. onSubmit: handleSubmit,
  43. validateOnBlur: true,
  44. enableReinitialize: true,
  45. });
  46. return (
  47. <Container component="main" maxWidth="md" sx={{ mb: '60px' }}>
  48. <Snackbar
  49. anchorOrigin={{ vertical: 'top', horizontal: 'center' }}
  50. open={open}
  51. autoHideDuration={3000}
  52. onClose={handleCloseNotification}
  53. >
  54. <Alert
  55. onClose={handleCloseNotification}
  56. severity="success"
  57. sx={{ width: '100%', backgroundColor: 'green', color: 'white' }}
  58. >
  59. Question submitted!
  60. </Alert>
  61. </Snackbar>
  62. <Box
  63. sx={{
  64. marginTop: 32,
  65. display: 'flex',
  66. flexDirection: 'column',
  67. alignItems: 'center',
  68. }}
  69. >
  70. <Typography fontSize={48}>{t('contact:Title')}</Typography>
  71. <Box
  72. component="form"
  73. onSubmit={formik.handleSubmit}
  74. sx={{ position: 'relative', mt: 1, p: 1 }}
  75. >
  76. <TextField
  77. name="firstName"
  78. label={t('forms:FirstName')}
  79. margin="normal"
  80. value={formik.values.firstName}
  81. onChange={formik.handleChange}
  82. error={formik.touched.firstName && Boolean(formik.errors.firstName)}
  83. helperText={formik.touched.firstName && formik.errors.firstName}
  84. autoFocus
  85. fullWidth
  86. />
  87. <TextField
  88. name="lastName"
  89. label={t('forms:LastName')}
  90. margin="normal"
  91. value={formik.values.lastName}
  92. onChange={formik.handleChange}
  93. error={formik.touched.lastName && Boolean(formik.errors.lastName)}
  94. helperText={formik.touched.lastName && formik.errors.lastName}
  95. autoFocus
  96. fullWidth
  97. />
  98. <TextField
  99. name="email"
  100. label={t('forms:Email')}
  101. margin="normal"
  102. value={formik.values.email}
  103. onChange={formik.handleChange}
  104. error={formik.touched.email && Boolean(formik.errors.email)}
  105. helperText={formik.touched.email && formik.errors.email}
  106. autoFocus
  107. fullWidth
  108. />
  109. <TextField
  110. name="message"
  111. label={t('forms:Message')}
  112. multiline
  113. margin="normal"
  114. value={formik.values.message}
  115. onChange={formik.handleChange}
  116. error={formik.touched.message && Boolean(formik.errors.message)}
  117. helperText={formik.touched.message && formik.errors.message}
  118. rows={4}
  119. autoFocus
  120. fullWidth
  121. />
  122. <Button
  123. type="submit"
  124. variant="contained"
  125. sx={{ mt: 3, mb: 2 }}
  126. fullWidth
  127. >
  128. {t('contact:SendBtn')}
  129. </Button>
  130. <Grid container justifyContent="center">
  131. <Link href={BASE_PAGE}>
  132. <Typography>{t('common:Back')}</Typography>
  133. </Link>
  134. </Grid>
  135. </Box>
  136. </Box>
  137. </Container>
  138. );
  139. };
  140. ContactPageForm.propTypes = {
  141. submitHandler: PropType.func,
  142. };
  143. export default ContactPageForm;