Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 12714 | Rev 12718 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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