Proyectos de Subversion LeadersLinked - Backend

Rev

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