12135 |
stevensc |
1 |
import axios from 'axios'
|
|
|
2 |
import React, { useEffect, useState } from 'react'
|
|
|
3 |
import parse from 'html-react-parser'
|
|
|
4 |
import { useForm } from 'react-hook-form'
|
|
|
5 |
import { useDispatch } from 'react-redux'
|
|
|
6 |
import { useHistory } from 'react-router-dom'
|
|
|
7 |
import { addNotification } from '../../../redux/notification/notification.actions'
|
|
|
8 |
import DescriptionInput from '../../../shared/DescriptionInput'
|
12012 |
stevensc |
9 |
|
12135 |
stevensc |
10 |
const AddView = ({ actionLink, jobsDescription: jobDescriptionOptions }) => {
|
|
|
11 |
|
|
|
12 |
// Hooks
|
|
|
13 |
const history = useHistory()
|
|
|
14 |
const { register, setValue, watch, reset } = useForm()
|
|
|
15 |
const dispatch = useDispatch()
|
|
|
16 |
|
|
|
17 |
// State
|
|
|
18 |
const [currentJobDescription, setCurretJobDescription] = useState('')
|
|
|
19 |
const [status, setStatus] = useState('a')
|
|
|
20 |
|
|
|
21 |
const onSubmit = () => {
|
|
|
22 |
|
|
|
23 |
const submitData = new FormData()
|
|
|
24 |
submitData.append('name', watch('name'))
|
|
|
25 |
submitData.append('description', watch('description'))
|
|
|
26 |
submitData.append('status', status)
|
|
|
27 |
submitData.append('job_description_id', currentJobDescription)
|
|
|
28 |
submitData.append('content', '[]')
|
|
|
29 |
|
|
|
30 |
axios.post(actionLink, submitData)
|
|
|
31 |
.then(({ data }) => {
|
|
|
32 |
if (!data.success) {
|
|
|
33 |
return dispatch(addNotification({
|
|
|
34 |
style: 'danger',
|
|
|
35 |
msg: 'Ha ocurrido un error'
|
|
|
36 |
}))
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
dispatch(addNotification({
|
|
|
40 |
style: 'success',
|
|
|
41 |
msg: data.data
|
|
|
42 |
}))
|
|
|
43 |
})
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
const submitAndClose = () => {
|
|
|
47 |
onSubmit()
|
|
|
48 |
reset()
|
|
|
49 |
history.goBack()
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
useEffect(() => {
|
|
|
53 |
register('description')
|
|
|
54 |
}, [])
|
|
|
55 |
|
12012 |
stevensc |
56 |
return (
|
12135 |
stevensc |
57 |
<section className="content">
|
|
|
58 |
<div className="row" style={{ padding: 16 }}>
|
|
|
59 |
<div className="col-xs-12 col-md-12">
|
|
|
60 |
<div className="form-group">
|
|
|
61 |
<label>Nombre</label>
|
|
|
62 |
<input type="text" name="name" className='form-control' ref={register({ required: true, maxLength: 50 })} />
|
|
|
63 |
</div>
|
|
|
64 |
<div className="form-group">
|
|
|
65 |
<label>Cargo a evaluar</label>
|
|
|
66 |
<select name="job_description_id" className="form-control" onChange={(e) => setCurretJobDescription(e.target.value)}>
|
|
|
67 |
{
|
12138 |
stevensc |
68 |
jobDescriptionOptions.map(({ name, uuid }) => (
|
|
|
69 |
<option key={uuid} value={uuid}>{name}</option>
|
12135 |
stevensc |
70 |
))
|
|
|
71 |
}
|
|
|
72 |
</select>
|
|
|
73 |
</div>
|
|
|
74 |
<div className="form-group">
|
|
|
75 |
<label htmlFor="form-description">Descripción</label>
|
|
|
76 |
<DescriptionInput
|
|
|
77 |
defaultValue={typeof watch('description') === 'string' ? parse(watch('description')) : ''}
|
|
|
78 |
name='description'
|
|
|
79 |
onChange={setValue}
|
|
|
80 |
/>
|
|
|
81 |
</div>
|
|
|
82 |
<div className="form-group">
|
|
|
83 |
<label htmlFor="form-status">Estatus</label>
|
|
|
84 |
<select name="form-status" className="form-control" onChange={(e) => setStatus(e.target.value)} value={status}>
|
|
|
85 |
<option selected={status === 'i'} value="i">Inactivo</option>
|
|
|
86 |
<option selected={status === 'a'} value="a">Activo</option>
|
|
|
87 |
</select>
|
|
|
88 |
</div>
|
|
|
89 |
<br />
|
|
|
90 |
<div className="d-flex" style={{ gap: '5px' }}>
|
|
|
91 |
<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
|
|
|
92 |
<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
|
|
|
93 |
<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
|
|
|
94 |
</div>
|
|
|
95 |
</div>
|
|
|
96 |
</div >
|
|
|
97 |
</section >
|
|
|
98 |
|
12012 |
stevensc |
99 |
)
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
export default AddView
|