Proyectos de Subversion LeadersLinked - Backend

Rev

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