| 3481 |
stevensc |
1 |
import React, { useState } from 'react';
|
|
|
2 |
import { useDispatch, useSelector } from 'react-redux';
|
|
|
3 |
import { useForm } from 'react-hook-form';
|
|
|
4 |
import { Button } from '@mui/material';
|
|
|
5 |
|
|
|
6 |
import { axios } from '@utils';
|
|
|
7 |
import { addNotification } from '@store/notification/notification.actions';
|
|
|
8 |
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback';
|
|
|
9 |
|
|
|
10 |
const CommentForm = ({ linkCommentAdd = '' }) => {
|
|
|
11 |
const [rating, setRating] = useState(1);
|
|
|
12 |
const labels = useSelector(({ intl }) => intl.labels);
|
|
|
13 |
const dispatch = useDispatch();
|
|
|
14 |
|
|
|
15 |
const {
|
|
|
16 |
register,
|
|
|
17 |
handleSubmit,
|
|
|
18 |
reset,
|
|
|
19 |
formState: { errors }
|
|
|
20 |
} = useForm({ mode: 'onBlur' });
|
|
|
21 |
|
|
|
22 |
const addComment = handleSubmit(({ comment }) => {
|
|
|
23 |
axios
|
|
|
24 |
.post(linkCommentAdd, { comment, rating })
|
|
|
25 |
.then((response) => {
|
|
|
26 |
const { data, success } = response.data;
|
|
|
27 |
|
|
|
28 |
if (!success) {
|
|
|
29 |
const errorMessage = typeof data === 'string' ? data : JSON.stringify(data);
|
|
|
30 |
throw new Error(errorMessage);
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
dispatch(addNotification({ style: 'success', msg: data.message }));
|
|
|
34 |
setRating(1);
|
|
|
35 |
reset();
|
|
|
36 |
})
|
|
|
37 |
.catch((err) => {
|
|
|
38 |
dispatch(addNotification({ style: 'danger', msg: err.message }));
|
|
|
39 |
});
|
|
|
40 |
});
|
|
|
41 |
|
|
|
42 |
return (
|
|
|
43 |
<>
|
|
|
44 |
<form onSubmit={addComment}>
|
|
|
45 |
<div className='form-group'>
|
|
|
46 |
<label htmlFor='comment'>Comentario</label>
|
|
|
47 |
<textarea
|
|
|
48 |
className='form-control'
|
|
|
49 |
rows='4'
|
|
|
50 |
name='comment'
|
|
|
51 |
id='comment-add'
|
|
|
52 |
placeholder={labels.write_a_post}
|
|
|
53 |
maxLength={250}
|
|
|
54 |
ref={register({ required: 'Este campo es requerido' })}
|
|
|
55 |
/>
|
|
|
56 |
{errors.comment && <FormErrorFeedback>{errors.comment.message}</FormErrorFeedback>}
|
|
|
57 |
</div>
|
|
|
58 |
|
|
|
59 |
<Button
|
|
|
60 |
color='primary'
|
|
|
61 |
disabled={!rating}
|
|
|
62 |
type='submit'
|
|
|
63 |
sx={{ width: '100%', mt: 1, p: 1 }}
|
|
|
64 |
>
|
|
|
65 |
Enviar
|
|
|
66 |
</Button>
|
|
|
67 |
</form>
|
|
|
68 |
</>
|
|
|
69 |
);
|
|
|
70 |
};
|
|
|
71 |
|
|
|
72 |
export default CommentForm;
|