Proyectos de Subversion LeadersLinked - Backend

Rev

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