| 12329 |
stevensc |
1 |
import React, { useState } from 'react'
|
|
|
2 |
import { Button, Modal } from 'react-bootstrap'
|
|
|
3 |
import axios from 'axios'
|
|
|
4 |
import { useForm } from 'react-hook-form'
|
|
|
5 |
import { useDispatch } from 'react-redux'
|
|
|
6 |
import Datetime from 'react-datetime'
|
|
|
7 |
import 'react-datetime/css/react-datetime.css'
|
|
|
8 |
import { addNotification } from '../../../redux/notification/notification.actions'
|
|
|
9 |
|
|
|
10 |
const AddModal = ({
|
|
|
11 |
action_link,
|
|
|
12 |
closeModal,
|
|
|
13 |
isOpen,
|
|
|
14 |
onComplete,
|
|
|
15 |
email_link
|
|
|
16 |
}) => {
|
|
|
17 |
|
|
|
18 |
//Hooks
|
|
|
19 |
const { register, handleSubmit, errors, setValue, clearErrors, watch } = useForm()
|
|
|
20 |
const [year, setYear] = useState(new Intl.DateTimeFormat('en-CA').format(new Date()))
|
|
|
21 |
const dispatch = useDispatch()
|
|
|
22 |
|
|
|
23 |
const onSubmit = (data) => {
|
|
|
24 |
|
|
|
25 |
const submitData = new FormData()
|
|
|
26 |
submitData.append('date', year)
|
|
|
27 |
|
|
|
28 |
Object.entries(data).map(([key, value]) => {
|
|
|
29 |
submitData.append(key, value)
|
|
|
30 |
})
|
|
|
31 |
|
|
|
32 |
axios.post(action_link, submitData)
|
|
|
33 |
.then(({ data }) => {
|
|
|
34 |
if (!data.success) {
|
|
|
35 |
return dispatch(addNotification({
|
|
|
36 |
style: 'danger',
|
|
|
37 |
msg: typeof data.data === 'string'
|
|
|
38 |
? data.data
|
|
|
39 |
: 'Ha ocurrido un error'
|
|
|
40 |
}))
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
clearErrors()
|
|
|
44 |
closeModal()
|
|
|
45 |
onComplete()
|
|
|
46 |
dispatch(addNotification({
|
|
|
47 |
style: 'success',
|
|
|
48 |
msg: 'Resgistro guardadof'
|
|
|
49 |
}))
|
|
|
50 |
})
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
const checkEmail = () => {
|
|
|
54 |
axios.get(email_link, { params: { email: watch('email') } })
|
|
|
55 |
.then(({ data }) => {
|
|
|
56 |
if (!data.success) {
|
|
|
57 |
dispatch(addNotification({
|
|
|
58 |
style: 'danger',
|
|
|
59 |
msg: 'Ha ocurrido un error'
|
|
|
60 |
}))
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
setValue('user_id', data.data.user_id)
|
|
|
64 |
setValue('first_name', data.data.first_name)
|
|
|
65 |
setValue('last_name', data.data.last_name)
|
|
|
66 |
clearErrors()
|
|
|
67 |
})
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
return (
|
|
|
71 |
<Modal size="md" onHide={closeModal} show={isOpen}>
|
|
|
72 |
<Modal.Header closeButton>
|
|
|
73 |
<Modal.Title>
|
|
|
74 |
Evaluación
|
|
|
75 |
</Modal.Title>
|
|
|
76 |
</Modal.Header>
|
|
|
77 |
<form onSubmit={handleSubmit(onSubmit)}>
|
|
|
78 |
<Modal.Body>
|
|
|
79 |
<div className='form-group'>
|
|
|
80 |
<label className="form-label">Último día</label>
|
|
|
81 |
<Datetime
|
|
|
82 |
dateFormat="DD-MM-YYYY"
|
|
|
83 |
timeFormat={false}
|
|
|
84 |
onChange={(e) =>
|
|
|
85 |
setYear(new Intl.DateTimeFormat('en-CA', { year: 'numeric', month: 'numeric', day: 'numeric' }).format(e.toDate()))
|
|
|
86 |
}
|
|
|
87 |
inputProps={{ className: 'form-control' }}
|
|
|
88 |
initialValue={Date.parse(year)}
|
|
|
89 |
closeOnSelect
|
|
|
90 |
value={year}
|
|
|
91 |
/>
|
|
|
92 |
</div>
|
|
|
93 |
<div className='form-group'>
|
|
|
94 |
<label className="form-label">Correo electrónico</label>
|
|
|
95 |
<input type="email" name='email' className='form-control' ref={register({ required: true })} />
|
|
|
96 |
{errors.email && <p>{errors.email.message}</p>}
|
|
|
97 |
</div>
|
|
|
98 |
<button
|
|
|
99 |
type="button"
|
|
|
100 |
className="btn btn-primary mb-1"
|
|
|
101 |
onClick={checkEmail}
|
|
|
102 |
>
|
|
|
103 |
Verificar Email
|
|
|
104 |
</button>
|
|
|
105 |
<div className='form-group'>
|
|
|
106 |
<label className="form-label">Nombre</label>
|
|
|
107 |
<input type="text" name='first_name' className='form-control' ref={register({ required: true })} />
|
|
|
108 |
{errors.first_name && <p>{errors.first_name.message}</p>}
|
|
|
109 |
</div>
|
|
|
110 |
<div className='form-group'>
|
|
|
111 |
<label className="form-label">Apellido</label>
|
|
|
112 |
<input type="text" name='last_name' className='form-control' ref={register({ required: true })} />
|
|
|
113 |
{errors.last_name && <p>{errors.last_name.message}</p>}
|
|
|
114 |
</div>
|
|
|
115 |
</Modal.Body>
|
|
|
116 |
<Modal.Footer>
|
|
|
117 |
<Button variant="primary" type='submit'>
|
|
|
118 |
Enviar
|
|
|
119 |
</Button>
|
|
|
120 |
<Button variant="danger" onClick={closeModal}>
|
|
|
121 |
Cancelar
|
|
|
122 |
</Button>
|
|
|
123 |
</Modal.Footer>
|
|
|
124 |
</form>
|
|
|
125 |
</Modal >
|
|
|
126 |
)
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
export default AddModal
|