|
| 1 | +import { useState } from 'react'; |
| 2 | +import { Alert, Box, Button, TextField, Typography } from '@mui/material'; |
| 3 | + |
| 4 | +import Form from 'components/Form/Form'; |
| 5 | +import Modal from 'components/Modal/Modal'; |
| 6 | +import LoadingButton from 'components/SwarmForm/LoadingButton'; |
| 7 | + |
| 8 | +interface IFeedbackForm { |
| 9 | + email: string; |
| 10 | + message: string; |
| 11 | +} |
| 12 | + |
| 13 | +const unexpectedErrorMessage = |
| 14 | + "We're sorry but something seems to have gone wrong. Please try again or reach out to us directly at support@locust.cloud"; |
| 15 | + |
| 16 | +export default function FeedbackForm() { |
| 17 | + const [open, setOpen] = useState(false); |
| 18 | + const [isLoading, setIsLoading] = useState(false); |
| 19 | + const [errorMessage, setErrorMessage] = useState<string>(); |
| 20 | + const [success, setSuccess] = useState<boolean>(); |
| 21 | + |
| 22 | + const onSubmitFeedback = async (inputData: IFeedbackForm) => { |
| 23 | + setIsLoading(true); |
| 24 | + |
| 25 | + try { |
| 26 | + const response = await fetch('http://localhost:8000/1/customer/send-feedback', { |
| 27 | + method: 'POST', |
| 28 | + body: JSON.stringify(inputData), |
| 29 | + headers: { 'Content-Type': 'application/json' }, |
| 30 | + }); |
| 31 | + |
| 32 | + if (response.status >= 400) { |
| 33 | + setErrorMessage(unexpectedErrorMessage); |
| 34 | + } else { |
| 35 | + setSuccess(true); |
| 36 | + } |
| 37 | + } catch { |
| 38 | + setErrorMessage(unexpectedErrorMessage); |
| 39 | + } |
| 40 | + |
| 41 | + setIsLoading(false); |
| 42 | + }; |
| 43 | + |
| 44 | + return ( |
| 45 | + <> |
| 46 | + <Box sx={{ display: 'flex', justifyContent: 'flex-end' }}> |
| 47 | + <Button color='inherit' onClick={() => setOpen(true)} variant='text'> |
| 48 | + Feedback |
| 49 | + </Button> |
| 50 | + </Box> |
| 51 | + <Modal onClose={() => setOpen(false)} open={open}> |
| 52 | + {success ? ( |
| 53 | + <Box |
| 54 | + sx={{ |
| 55 | + display: 'flex', |
| 56 | + flexDirection: 'column', |
| 57 | + justifyContent: 'center', |
| 58 | + textAlign: 'center', |
| 59 | + height: 200, |
| 60 | + }} |
| 61 | + > |
| 62 | + <Typography component='h4' sx={{ mb: 2 }} variant='h4'> |
| 63 | + Thanks for reaching out! |
| 64 | + </Typography> |
| 65 | + <Typography component='h4' variant='h5'> |
| 66 | + Your feedback is very valuable to us as we work to improve Locust! |
| 67 | + </Typography> |
| 68 | + </Box> |
| 69 | + ) : ( |
| 70 | + <Form<IFeedbackForm> onSubmit={onSubmitFeedback}> |
| 71 | + <Box sx={{ display: 'flex', flexDirection: 'column', rowGap: 2, my: 2 }}> |
| 72 | + <TextField label='Email' name='email' required type='email' /> |
| 73 | + <TextField label='Name' name='name' /> |
| 74 | + <TextField label='Message' multiline name='message' required rows={3} /> |
| 75 | + {!!errorMessage && <Alert severity='error'>{errorMessage}</Alert>} |
| 76 | + |
| 77 | + <LoadingButton isLoading={isLoading} type='submit'> |
| 78 | + Submit |
| 79 | + </LoadingButton> |
| 80 | + </Box> |
| 81 | + </Form> |
| 82 | + )} |
| 83 | + </Modal> |
| 84 | + </> |
| 85 | + ); |
| 86 | +} |
0 commit comments