Proyectos de Subversion LeadersLinked - Backend

Rev

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