12709 |
stevensc |
1 |
/* eslint-disable no-mixed-spaces-and-tabs */
|
|
|
2 |
import React, { useState, useEffect } from 'react'
|
|
|
3 |
import axios from 'axios'
|
|
|
4 |
import parse from 'html-react-parser'
|
|
|
5 |
import { useForm } from 'react-hook-form'
|
|
|
6 |
import { useDispatch } from 'react-redux'
|
|
|
7 |
import { useHistory } from 'react-router-dom'
|
|
|
8 |
import { addNotification } from '../../../redux/notification/notification.actions'
|
|
|
9 |
import DescriptionInput from '../../../shared/DescriptionInput'
|
|
|
10 |
import SectionModal from '../components/SectionModal'
|
|
|
11 |
|
|
|
12 |
const sectionTypeOptions = {
|
|
|
13 |
open: 'Abierto',
|
|
|
14 |
simple: 'Simple'
|
|
|
15 |
}
|
|
|
16 |
|
|
|
17 |
const INITIAL_SECTION = {
|
|
|
18 |
slug_section: '',
|
|
|
19 |
name: '',
|
|
|
20 |
text: '',
|
|
|
21 |
position: 0,
|
|
|
22 |
questions: [],
|
|
|
23 |
status: 0
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
const INITIAL_QUESTION = {
|
|
|
27 |
slug_section: '',
|
|
|
28 |
slug_question: '',
|
|
|
29 |
text: '',
|
|
|
30 |
type: '',
|
|
|
31 |
position: 0,
|
|
|
32 |
maxlength: '0',
|
|
|
33 |
multiline: '0',
|
|
|
34 |
range: '0',
|
|
|
35 |
options: [],
|
|
|
36 |
answer: ''
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
const FormView = ({ actionLink }) => {
|
|
|
41 |
|
|
|
42 |
// Hooks
|
|
|
43 |
const history = useHistory()
|
|
|
44 |
const dispatch = useDispatch()
|
|
|
45 |
const { register, setValue, watch, reset } = useForm()
|
|
|
46 |
|
|
|
47 |
// Section modal states
|
|
|
48 |
const [isShowSection, setIsShowSectionModal] = useState(false)
|
|
|
49 |
const [sectionSelected, setSectionSelected] = useState(INITIAL_SECTION)
|
|
|
50 |
const [sectionType, setSectionType] = useState('add')
|
|
|
51 |
|
|
|
52 |
// Section modal states
|
|
|
53 |
const [isShowQuestion, setIsShowQuestionModal] = useState(false)
|
|
|
54 |
const [questionSelected, setQuestionSelected] = useState(INITIAL_QUESTION)
|
|
|
55 |
const [questionType, setQuestionType] = useState('add')
|
|
|
56 |
|
|
|
57 |
const [content, setContent] = useState([])
|
|
|
58 |
const [status, setStatus] = useState('A')
|
|
|
59 |
|
|
|
60 |
const showSectionModal = (section = INITIAL_SECTION, type = 'add') => {
|
|
|
61 |
setIsShowSectionModal(true)
|
|
|
62 |
setSectionSelected(section)
|
|
|
63 |
setSectionType(type)
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
const closeSectionModal = () => {
|
|
|
67 |
setIsShowSectionModal(false)
|
|
|
68 |
setSectionSelected(INITIAL_SECTION)
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
const addSection = (section) => {
|
|
|
72 |
setContent(prev => [...prev, section])
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
const editSection = (section) => {
|
|
|
76 |
setContent(prev => prev.map(prevSection => prevSection.slug_section === section.slug_section && section))
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
const showQuestionModal = (question = INITIAL_QUESTION, type = 'add') => {
|
|
|
80 |
setIsShowQuestionModal(true)
|
|
|
81 |
setQuestionType(type)
|
|
|
82 |
setQuestionSelected(question)
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
const closeQuestionModal = () => {
|
|
|
86 |
setIsShowQuestionModal(false)
|
|
|
87 |
setQuestionSelected(INITIAL_QUESTION)
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
const addQuestion = (question) => {
|
|
|
91 |
setContent(prev => prev.map(prevSection => prevSection.slug_section === question.slug_section && prev.questions.push(question)))
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
const editQuestion = (question) => {
|
|
|
95 |
setContent(prev => prev.map(prevSection => {
|
|
|
96 |
if (prevSection.slug_section === question.slug_section) {
|
|
|
97 |
const questions = prevSection.questions
|
|
|
98 |
|
|
|
99 |
return questions.map(prevQuestion => prevQuestion.slug_question === question.slug_question && question)
|
|
|
100 |
}
|
|
|
101 |
}))
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
const onSubmit = () => {
|
|
|
105 |
const submitData = new FormData()
|
|
|
106 |
submitData.append('name', watch('name'))
|
|
|
107 |
submitData.append('description', watch('description'))
|
|
|
108 |
submitData.append('text', watch('text'))
|
|
|
109 |
submitData.append('status', status)
|
|
|
110 |
submitData.append('content', content)
|
|
|
111 |
|
|
|
112 |
axios.post(actionLink, submitData)
|
|
|
113 |
.then(({ data }) => {
|
|
|
114 |
if (!data.success) {
|
|
|
115 |
return dispatch(addNotification({
|
|
|
116 |
style: 'danger',
|
|
|
117 |
msg: typeof data.data === 'string'
|
|
|
118 |
? data.data
|
|
|
119 |
: 'Ha ocurrido un error'
|
|
|
120 |
}))
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
dispatch(addNotification({
|
|
|
124 |
style: 'success',
|
|
|
125 |
msg: data.data
|
|
|
126 |
}))
|
|
|
127 |
})
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
const submitAndClose = () => {
|
|
|
131 |
onSubmit()
|
|
|
132 |
reset()
|
|
|
133 |
history.goBack()
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
useEffect(() => {
|
|
|
137 |
register('description')
|
|
|
138 |
register('text')
|
|
|
139 |
}, [])
|
|
|
140 |
|
|
|
141 |
useEffect(() => {
|
|
|
142 |
axios.get(actionLink)
|
|
|
143 |
.then(({ data }) => {
|
|
|
144 |
if (!data.success) {
|
|
|
145 |
return dispatch(addNotification({
|
|
|
146 |
style: 'danger',
|
|
|
147 |
msg: 'Ha ocurrido un error'
|
|
|
148 |
}))
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
setValue('name', data.data.name)
|
|
|
152 |
setValue('description', data.data.description)
|
|
|
153 |
setValue('text', data.data.description)
|
|
|
154 |
setContent(data.data.content)
|
|
|
155 |
setStatus(data.data.status)
|
|
|
156 |
})
|
|
|
157 |
}, [actionLink])
|
|
|
158 |
|
|
|
159 |
return (
|
|
|
160 |
<>
|
|
|
161 |
<section className="content">
|
|
|
162 |
<div className="row" style={{ padding: 16 }}>
|
|
|
163 |
<div className="col-xs-12 col-md-12">
|
|
|
164 |
<div className="form-group">
|
|
|
165 |
<label>Nombre</label>
|
|
|
166 |
<input type="text" name="name" className='form-control' ref={register({ required: true, maxLength: 50 })} />
|
|
|
167 |
</div>
|
|
|
168 |
<div className="form-group">
|
|
|
169 |
<label htmlFor="form-description">Descripción</label>
|
|
|
170 |
<DescriptionInput
|
|
|
171 |
defaultValue={parse(watch('description'))}
|
|
|
172 |
name='description'
|
|
|
173 |
onChange={setValue}
|
|
|
174 |
/>
|
|
|
175 |
</div>
|
|
|
176 |
<div className="form-group">
|
|
|
177 |
<label htmlFor="form-description">Texto</label>
|
|
|
178 |
<DescriptionInput
|
|
|
179 |
defaultValue={parse(watch('description'))}
|
|
|
180 |
name='text'
|
|
|
181 |
onChange={setValue}
|
|
|
182 |
/>
|
|
|
183 |
</div>
|
|
|
184 |
<div className="form-group">
|
|
|
185 |
<label htmlFor="form-status">Estatus</label>
|
|
|
186 |
<select name="form-status" className="form-control" onChange={(e) => setStatus(e.target.value)} value={status}>
|
|
|
187 |
<option selected={status === 'A'} value="A">Activo</option>
|
|
|
188 |
<option selected={status === 'I'} value="I">Inactivo</option>
|
|
|
189 |
</select>
|
|
|
190 |
</div>
|
|
|
191 |
<br />
|
|
|
192 |
<div className="row">
|
|
|
193 |
<div className="col-xs-12 col-md-12">
|
|
|
194 |
<div className="panel-group" id="rows" >
|
|
|
195 |
<div className="form-group">
|
|
|
196 |
<div className="row">
|
|
|
197 |
<div className="col-xs-12 col-md-12">
|
|
|
198 |
<hr />
|
|
|
199 |
<h4 style={{ fontSize: 18, fontWeight: 'bold' }}>Competencias asociadas al cargo:</h4>
|
|
|
200 |
<button className='btn btn-primary' onClick={showSectionModal}>
|
|
|
201 |
Agregar sección
|
|
|
202 |
</button>
|
|
|
203 |
<br />
|
|
|
204 |
<div className="panel-group" id="rows-job-competencies" >
|
|
|
205 |
{
|
|
|
206 |
content.length > 0
|
|
|
207 |
&&
|
|
|
208 |
content.map((section) => {
|
|
|
209 |
|
|
|
210 |
return (
|
|
|
211 |
<div className="panel panel-default" key={section.slug_section}>
|
|
|
212 |
<div className="panel-heading">
|
|
|
213 |
<h4 className="panel-title">
|
|
|
214 |
<a className="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent={`panel-${section.slug_section}`} href={`#collapse-${section.slug_section}`}>
|
|
|
215 |
<span className={`section-name${section.slug_section}`}>
|
|
|
216 |
{section.name}
|
|
|
217 |
</span>
|
|
|
218 |
</a>
|
|
|
219 |
</h4>
|
|
|
220 |
</div>
|
|
|
221 |
<div id="collapse-section1661528423935" className="panel-collapse in collapse show">
|
|
|
222 |
<div className="panel-body">
|
|
|
223 |
<div className="table-responsive">
|
|
|
224 |
<table className="table table-bordered">
|
|
|
225 |
<thead>
|
|
|
226 |
<tr>
|
|
|
227 |
<th style={{ width: '10%' }}>Elemento</th>
|
|
|
228 |
<th style={{ width: '50%' }}>Texto</th>
|
|
|
229 |
<th style={{ width: '10%' }}>Tipo</th>
|
|
|
230 |
<th style={{ width: '20%' }}>Acciones</th>
|
|
|
231 |
</tr>
|
|
|
232 |
</thead>
|
|
|
233 |
<tbody>
|
|
|
234 |
<tr className="tr-section">
|
|
|
235 |
<td className="text-left">Sección</td>
|
|
|
236 |
<td className="text-left">{section.name}</td>
|
|
|
237 |
<td />
|
|
|
238 |
<td>
|
|
|
239 |
<button className="btn btn-default" onClick={() => showSectionModal(section, 'edit')}>
|
|
|
240 |
<i className="fa fa-edit" />
|
|
|
241 |
Editar Sección
|
|
|
242 |
</button>
|
|
|
243 |
<button className="btn btn-default" >
|
|
|
244 |
<i className="fa fa-ban" />
|
|
|
245 |
Borrar Sección
|
|
|
246 |
</button>
|
|
|
247 |
<button className="btn btn-default btn-add-question" >
|
|
|
248 |
<i className="fa fa-plus" />
|
|
|
249 |
Agregar Pregunta
|
|
|
250 |
</button>
|
|
|
251 |
</td>
|
|
|
252 |
</tr>
|
|
|
253 |
{
|
|
|
254 |
section.questions.map((question) => (
|
|
|
255 |
<tr key={question.slug_question} className="tr-question">
|
|
|
256 |
<td className="text-left">Pregunta</td>
|
|
|
257 |
<td className="text-left">
|
|
|
258 |
{parse(question.text)}
|
|
|
259 |
</td>
|
|
|
260 |
<td className="text-capitalize">
|
|
|
261 |
{sectionTypeOptions[question.type]}
|
|
|
262 |
</td>
|
|
|
263 |
<td>
|
|
|
264 |
<button className="btn btn-default btn-edit-question">
|
|
|
265 |
<i className="fa fa-edit" /> Editar Pregunta
|
|
|
266 |
</button>
|
|
|
267 |
<button className="btn btn-default btn-delete-question">
|
|
|
268 |
<i className="fa fa-ban" /> Borrar Pregunta
|
|
|
269 |
</button>
|
|
|
270 |
</td>
|
|
|
271 |
</tr>
|
|
|
272 |
))
|
|
|
273 |
}
|
|
|
274 |
</tbody>
|
|
|
275 |
</table>
|
|
|
276 |
</div>
|
|
|
277 |
</div>
|
|
|
278 |
</div>
|
|
|
279 |
</div>
|
|
|
280 |
)
|
|
|
281 |
})
|
|
|
282 |
}
|
|
|
283 |
</div>
|
|
|
284 |
</div>
|
|
|
285 |
</div>
|
|
|
286 |
</div>
|
|
|
287 |
</div>
|
|
|
288 |
</div>
|
|
|
289 |
</div>
|
|
|
290 |
<div className="d-flex" style={{ gap: '5px' }}>
|
|
|
291 |
<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
|
|
|
292 |
<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
|
|
|
293 |
<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
|
|
|
294 |
</div>
|
|
|
295 |
</div>
|
|
|
296 |
</div >
|
|
|
297 |
</section >
|
|
|
298 |
<SectionModal
|
|
|
299 |
show={isShowSection}
|
|
|
300 |
section={sectionSelected}
|
|
|
301 |
closeModal={closeSectionModal}
|
|
|
302 |
onSubmit={sectionType === 'add' ? addSection : editSection}
|
|
|
303 |
/>
|
|
|
304 |
</>
|
|
|
305 |
)
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
export default FormView
|