3577 |
stevensc |
1 |
import React from 'react';
|
|
|
2 |
import { useDispatch } from 'react-redux';
|
|
|
3 |
import { useForm } from 'react-hook-form';
|
|
|
4 |
|
|
|
5 |
import { useInmail } from '@hooks';
|
|
|
6 |
import { sendInmail } from '@app/services/inmail';
|
|
|
7 |
import { addNotification } from '@app/redux/notification/notification.actions';
|
|
|
8 |
|
|
|
9 |
import Ckeditor from '@components/common/ckeditor/Ckeditor';
|
|
|
10 |
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback';
|
|
|
11 |
import Input from '@components/UI/inputs/Input';
|
|
|
12 |
import Modal from '@components/UI/modal/Modal';
|
|
|
13 |
import SearchInput from './search-input';
|
|
|
14 |
|
|
|
15 |
export default function ComposeModal() {
|
|
|
16 |
const { composeModalShow, toggleComposeModal } = useInmail();
|
|
|
17 |
const dispatch = useDispatch();
|
|
|
18 |
const requiredErrorMsg = 'Este campo es requerido';
|
|
|
19 |
|
|
|
20 |
const {
|
|
|
21 |
control,
|
|
|
22 |
setValue,
|
|
|
23 |
handleSubmit,
|
|
|
24 |
reset,
|
|
|
25 |
formState: { isSubmitting, errors }
|
|
|
26 |
} = useForm({
|
|
|
27 |
defaultValues: {
|
|
|
28 |
to: '',
|
|
|
29 |
subject: '',
|
|
|
30 |
message: '',
|
|
|
31 |
file: ''
|
|
|
32 |
}
|
|
|
33 |
});
|
|
|
34 |
|
|
|
35 |
const onSend = handleSubmit(async ({ to, ...message }) => {
|
|
|
36 |
try {
|
|
|
37 |
const result = await sendInmail(to, message);
|
|
|
38 |
dispatch(addNotification({ style: 'success', msg: result }));
|
|
|
39 |
toggleComposeModal();
|
|
|
40 |
reset();
|
|
|
41 |
} catch (error) {
|
|
|
42 |
dispatch(addNotification({ style: 'danger', msg: error.message }));
|
|
|
43 |
}
|
|
|
44 |
});
|
|
|
45 |
|
|
|
46 |
return (
|
|
|
47 |
<Modal
|
|
|
48 |
show={composeModalShow}
|
|
|
49 |
title='Nuevo mensaje'
|
|
|
50 |
onAccept={onSend}
|
|
|
51 |
loading={isSubmitting}
|
|
|
52 |
onClose={toggleComposeModal}
|
|
|
53 |
>
|
|
|
54 |
<SearchInput onChange={(value) => setValue('to', value.send_link)} />
|
|
|
55 |
{errors.to?.message ? <FormErrorFeedback>{errors.to.message}</FormErrorFeedback> : null}
|
|
|
56 |
|
|
|
57 |
<Input
|
|
|
58 |
label='Asunto'
|
|
|
59 |
name='subject'
|
|
|
60 |
control={control}
|
|
|
61 |
rules={{ required: requiredErrorMsg }}
|
|
|
62 |
error={errors.subject?.message}
|
|
|
63 |
/>
|
|
|
64 |
|
|
|
65 |
<Ckeditor onChange={(value) => setValue('message', value)} />
|
|
|
66 |
{errors.message?.message ? (
|
|
|
67 |
<FormErrorFeedback>{errors.message.message}</FormErrorFeedback>
|
|
|
68 |
) : null}
|
|
|
69 |
|
|
|
70 |
<Input
|
|
|
71 |
type='file'
|
|
|
72 |
label='Adjunto'
|
|
|
73 |
accept='image/*'
|
|
|
74 |
onChange={(e) => setValue('file', e.target.files[0])}
|
|
|
75 |
error={errors.file?.message}
|
|
|
76 |
/>
|
|
|
77 |
</Modal>
|
|
|
78 |
);
|
|
|
79 |
}
|