Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
12264 stevensc 1
/* eslint-disable no-mixed-spaces-and-tabs */
2
import React, { useState, useEffect } from 'react'
3
import { Card } from 'react-bootstrap'
4
import { LengthFilter, SearchInput, Table, TablePagination } from '../../../recruitment_and_selection/components/TableComponents'
5
import { addNotification } from '../../../redux/notification/notification.actions'
6
import DeleteModal from '../../../shared/DeleteModal'
7
import axios from 'axios'
8
import { useDispatch } from 'react-redux'
9
 
10
const headers = [
11
	{ key: 'last_date', label: 'Último día', isSorteable: true },
12
	{ key: 'form', label: 'Nombre del Formulario', isSorteable: true },
13
	{ key: 'supervisor', label: 'Supervisor', isSorteable: true },
14
	{ key: 'first_name', label: 'Evaluado', isSorteable: true },
15
	{ key: 'actions', label: 'Acciones', isSorteable: false }
16
]
17
 
18
const TableView = ({
19
	add_link,
20
	table_link,
21
	permisions,
22
	actionLink,
23
	setActionLink
24
}) => {
25
 
26
	//Hooks
27
	const dispatch = useDispatch()
28
 
29
	//State
30
	const [modalToShow, setModalToShow] = useState('')
31
	const [items, setItems] = useState([])
32
	const [total, setTotal] = useState(0)
33
	const [search, setSearch] = useState('')
34
	const [dataLength, setDataLength] = useState(10)
35
	const [pages, setPages] = useState({
36
		current: 1,
37
		last: 1
38
	})
39
 
40
	const getData = ({ url = '', params = {} }) => {
41
		axios.get(url, { params: { ...params } })
42
			.then(({ data }) => {
43
				if (!data.success) {
44
					return dispatch(addNotification({
45
						style: 'danger',
46
						msg: typeof data.data === 'string'
47
							? data.data
48
							: 'Ha ocurrido un error'
49
					}))
50
				}
51
 
52
				setItems(data.data.items)
53
				setTotal(data.data.total)
54
				setPages({ ...pages, last: Math.ceil(data.data.total / dataLength) })
55
			})
56
			.catch(() => dispatch(addNotification({
57
				style: 'danger',
58
				msg: 'Ha ocurrido un error'
59
			})))
60
	}
61
 
62
	useEffect(() => {
63
		getData({
64
			url: table_link,
65
			params: {
66
				search: search,
67
				length: dataLength,
68
				page: pages.current
69
			}
70
		})
71
	}, [search, dataLength, pages.current])
72
 
73
	return (
74
		<>
75
			<section className="content">
76
				<div className="container-fluid">
77
					<div className="row">
78
						<div className="col-12">
79
							<Card>
80
								<Card.Header>
81
									<div className="row justify-content-end" style={{ gap: '10px' }}>
82
										{
83
											permisions.allowAdd
84
											&&
85
											<label
86
												className='d-flex align-items-center'
87
												onClick={() => {
88
													setActionLink(add_link)
89
													setModalToShow('add')
90
												}}
91
												style={{ cursor: 'pointer' }}
92
											>
93
												<i className="fa fa-plus mr-2" />
94
												Agregar
95
											</label>
96
										}
97
										<label
98
											className='d-flex align-items-center'
99
											onClick={() => getData({
100
												url: table_link,
101
												params: {
102
													search: search,
103
													length: dataLength,
104
													page: pages.current
105
												}
106
											})}
107
											style={{ cursor: 'pointer' }}
108
										>
109
											<i className='fa fa-refresh mr-2' />
110
											Actualizar
111
										</label>
112
									</div>
113
									<div className="row justify-content-between align-items-center">
114
										<LengthFilter onChange={(e) => setDataLength(e.target.value)} />
115
										<SearchInput onChange={(e) => setSearch(e.target.value)} />
116
									</div>
117
								</Card.Header>
118
								<Card.Body>
119
									<div className="table-responsive">
120
										<Table data={items} headers={headers} setData={setItems}>
121
											{
122
												items.length
123
												&&
124
												items.map((item, index) => (
125
													<tr key={index}>
126
														<td className='text-vertical-middle'>{item.last_date}F</td>
127
														<td className='text-vertical-middle'>
128
															{item.form}
12269 stevensc 129
															<br />
130
															<button
131
																className="btn btn-info btn-sm"
132
																onClick={() => {
133
																	setActionLink(item.actions.link_both)
134
																	history.push(item.actions.link_both)
135
																}}
136
															>
137
																<i className="fa fa-external-link" />
138
																Evaluacion en conjunto
139
															</button>
12264 stevensc 140
														</td>
141
														<td className='text-vertical-middle'>
142
															{item.supervisor}
12269 stevensc 143
															<button
144
																className="btn btn-info btn-sm"
145
																onClick={() => {
146
																	setActionLink(item.actions.link_superviser)
147
																	history.push(item.actions.link_superviser)
148
																}}
149
															>
150
																<i className="fa fa-external-link" />
151
																Evaluacion del supervisor
152
															</button>
12264 stevensc 153
														</td>
154
														<td className='text-vertical-middle'>
155
															{`${item.first_name} ${item.last_name}`}
12269 stevensc 156
															<button
157
																className="btn btn-info btn-sm"
158
																onClick={() => {
159
																	setActionLink(item.actions.link_self)
160
																	history.push(item.actions.link_self)
161
																}}
162
															>
163
																<i className="fa fa-external-link" />
164
																Autoevaluación
165
															</button>
12264 stevensc 166
														</td>
167
														<td>
168
															<div className="d-flex align-items-center" style={{ gap: '5px' }}>
169
																{
170
																	permisions.allowDelete
171
																	&&
172
																	<i
173
																		className='fa fa-trash'
174
																		onClick={() => {
175
																			setActionLink(item.actions.link_delete)
176
																			setModalToShow('delete')
177
																		}}
178
																		style={{ cursor: 'pointer' }}
179
																	/>
180
																}
181
															</div>
182
														</td>
183
													</tr>
184
												))
185
											}
186
										</Table>
187
									</div>
188
									<div className='row justify-content-between align-items-center'>
189
										<p className='mb-0'>
190
											{`Mostrando registros del ${(dataLength * pages.current) - (dataLength - 1) || 0} al ${(dataLength * pages.current) - (dataLength - total) || 0} de un total de ${total || 0} registros`}
191
										</p>
192
										<TablePagination
193
											onDecrement={() => setPages(prev => prev.current -= 1)}
194
											onIncrement={() => setPages(prev => prev.current += 1)}
195
											totalPages={pages.last}
196
											currentPage={pages.current}
197
										/>
198
									</div>
199
								</Card.Body>
200
							</Card>
201
						</div>
202
					</div >
203
				</div >
204
			</section >
205
			<DeleteModal
206
				url={actionLink}
207
				isOpen={modalToShow === 'delete'}
208
				closeModal={() => setModalToShow('')}
209
				title="Esta seguro de borrar esta evaluación?"
210
				onComplete={() => setItems(items.filter((item) => item.actions.link_delete !== actionLink))}
211
				message="Registro borrado"
212
			/>
213
		</>
214
	)
215
}
216
 
217
export default TableView