Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ContactPageForm.jsx 4.1KB

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