|
| 1 | +import { Box, Button, Grid, IconButton, Paper, TextField } from '@mui/material'; |
| 2 | +import axios from 'axios'; |
| 3 | +import { Formik, Field, Form, ErrorMessage } from 'formik'; |
| 4 | +import * as Yup from 'yup'; |
| 5 | +import CircularProgress from '@mui/material/CircularProgress'; |
| 6 | +import Snackbar from '@mui/material/Snackbar'; |
| 7 | +import CloseIcon from '@mui/icons-material/Close'; |
| 8 | +import React, { useState } from 'react'; |
| 9 | +const SupportForm = () => { |
| 10 | + const [loading, setLoading] = useState(false); |
| 11 | + const [showSnackBar, setShowSnackBar] = useState(false); |
| 12 | + const [showError, setShowError] = useState(false); |
| 13 | + const [supportError, setSupportError] = useState(''); |
| 14 | + const initialValue = { |
| 15 | + name: '', |
| 16 | + email: '', |
| 17 | + description: '', |
| 18 | + }; |
| 19 | + const validationSchema = Yup.object({ |
| 20 | + name: Yup.string().required('Required'), |
| 21 | + email: Yup.string().email('E-mail is not valid').required('Required'), |
| 22 | + description: Yup.string().required('Required!'), |
| 23 | + }); |
| 24 | + const handleSubmit = async (values, onSubmitProps) => { |
| 25 | + const { name, email, description } = values; |
| 26 | + try { |
| 27 | + setLoading(true); |
| 28 | + const response = await axios.post( |
| 29 | + 'http://localhost:3001/api/support/newsupport', |
| 30 | + { |
| 31 | + name, |
| 32 | + email, |
| 33 | + description, |
| 34 | + } |
| 35 | + ); |
| 36 | + if (response.status === 200) { |
| 37 | + setShowSnackBar(true); |
| 38 | + onSubmitProps.resetForm(); |
| 39 | + } |
| 40 | + } catch (err) { |
| 41 | + const errorMsg = err.response.data.message; |
| 42 | + setSupportError(errorMsg); |
| 43 | + setShowError(true); |
| 44 | + } |
| 45 | + setLoading(false); |
| 46 | + }; |
| 47 | + const closeSnackBar = () => { |
| 48 | + setShowSnackBar(false); |
| 49 | + setShowError(false); |
| 50 | + }; |
| 51 | + const action = ( |
| 52 | + <React.Fragment> |
| 53 | + <IconButton |
| 54 | + size='small' |
| 55 | + aria-label='close' |
| 56 | + color='inherit' |
| 57 | + onClick={closeSnackBar} |
| 58 | + autoHideDuration={4000} |
| 59 | + > |
| 60 | + <CloseIcon fontSize='small' /> |
| 61 | + </IconButton> |
| 62 | + </React.Fragment> |
| 63 | + ); |
| 64 | + |
| 65 | + return ( |
| 66 | + <> |
| 67 | + <Snackbar |
| 68 | + action={action} |
| 69 | + open={showSnackBar} |
| 70 | + autoHideDuration={3000} |
| 71 | + onClose={closeSnackBar} |
| 72 | + message='Support Query Successfully Submitted' |
| 73 | + /> |
| 74 | + <Snackbar |
| 75 | + action={action} |
| 76 | + open={showError} |
| 77 | + autoHideDuration={3000} |
| 78 | + onClose={closeSnackBar} |
| 79 | + message={supportError} |
| 80 | + /> |
| 81 | + <Grid container> |
| 82 | + <Grid item sm={3} xs={false}></Grid> |
| 83 | + <Grid item sm={6} xs={12}> |
| 84 | + <Paper> |
| 85 | + <Box m={5} p={3}> |
| 86 | + <Formik |
| 87 | + initialValues={initialValue} |
| 88 | + validationSchema={validationSchema} |
| 89 | + onSubmit={handleSubmit} |
| 90 | + > |
| 91 | + {({ |
| 92 | + values, |
| 93 | + handleChange, |
| 94 | + handleBlur, |
| 95 | + touched, |
| 96 | + errors, |
| 97 | + }) => { |
| 98 | + const { name, email } = values; |
| 99 | + return ( |
| 100 | + <Form> |
| 101 | + <TextField |
| 102 | + label='Name' |
| 103 | + name='name' |
| 104 | + fullWidth |
| 105 | + variant='outlined' |
| 106 | + margin='dense' |
| 107 | + value={name} |
| 108 | + onChange={handleChange} |
| 109 | + onBlur={handleBlur} |
| 110 | + helperText={ |
| 111 | + <ErrorMessage name='name' /> |
| 112 | + } |
| 113 | + error={ |
| 114 | + errors.name && touched.name |
| 115 | + } |
| 116 | + required |
| 117 | + /> |
| 118 | + <TextField |
| 119 | + label='E-mail' |
| 120 | + name='email' |
| 121 | + fullWidth |
| 122 | + variant='outlined' |
| 123 | + margin='dense' |
| 124 | + value={email} |
| 125 | + onChange={handleChange} |
| 126 | + onBlur={handleBlur} |
| 127 | + helperText={ |
| 128 | + <ErrorMessage name='email' /> |
| 129 | + } |
| 130 | + error={ |
| 131 | + errors.email && |
| 132 | + touched.email |
| 133 | + } |
| 134 | + required |
| 135 | + /> |
| 136 | + <Field |
| 137 | + as={TextField} |
| 138 | + label='How can we help?' |
| 139 | + type='text' |
| 140 | + name='description' |
| 141 | + fullWidth |
| 142 | + multiline |
| 143 | + margin='dense' |
| 144 | + variant='outlined' |
| 145 | + InputProps={{ |
| 146 | + inputComponent: 'textarea', |
| 147 | + rows: 10, |
| 148 | + }} |
| 149 | + helperText={ |
| 150 | + <ErrorMessage name='description' /> |
| 151 | + } |
| 152 | + error={ |
| 153 | + errors.description && |
| 154 | + touched.description |
| 155 | + } |
| 156 | + /> |
| 157 | + <Button |
| 158 | + variant='contained' |
| 159 | + type='submit' |
| 160 | + color='primary' |
| 161 | + fullWidth |
| 162 | + disabled={loading} |
| 163 | + > |
| 164 | + {loading && ( |
| 165 | + <CircularProgress |
| 166 | + size={15} |
| 167 | + sx={{ mr: '10px' }} |
| 168 | + color='inherit' |
| 169 | + /> |
| 170 | + )} |
| 171 | + Submit |
| 172 | + </Button> |
| 173 | + </Form> |
| 174 | + ); |
| 175 | + }} |
| 176 | + </Formik> |
| 177 | + </Box> |
| 178 | + </Paper> |
| 179 | + </Grid> |
| 180 | + <Grid item sm={3} xs={false}></Grid> |
| 181 | + </Grid> |
| 182 | + </> |
| 183 | + ); |
| 184 | +}; |
| 185 | + |
| 186 | +export default SupportForm; |
0 commit comments