Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
11960 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'
12017 stevensc 9
import { useHistory } from 'react-router-dom'
11960 stevensc 10
 
11
const headers = [
12
	{ key: 'name', label: 'Nombre', isSorteable: true },
13
	{ key: 'job_description', label: 'Descripción', isSorteable: true },
14
	{ key: 'status', label: 'Estatus', isSorteable: false },
15
	{ key: 'actions', label: 'Acciones', isSorteable: false }
16
]
17
 
12012 stevensc 18
const TableView = ({ add_link, table_link, permisions, actionLink, setActionLink }) => {
11960 stevensc 19
 
12015 stevensc 20
	const history = useHistory()
11960 stevensc 21
	const dispatch = useDispatch()
22
	const [modalToShow, setModalToShow] = useState('')
23
	const [items, setItems] = useState([])
24
	const [total, setTotal] = useState(0)
25
	const [search, setSearch] = useState('')
26
	const [dataLength, setDataLength] = useState(10)
27
	const [pages, setPages] = useState({
28
		current: 1,
29
		last: 1
30
	})
31
 
32
	const getData = ({ url = '', params = {} }) => {
33
 
34
		axios.get(url, { params: { ...params } })
35
			.then(({ data }) => {
36
				if (!data.success) {
37
					return dispatch(addNotification({
38
						style: 'danger',
39
						msg: 'Ha ocurrido un error'
40
					}))
41
				}
42
 
43
				setItems(data.data.items)
44
				setTotal(data.data.total)
45
				setPages({ ...pages, last: Math.ceil(data.data.total / dataLength) })
46
			})
47
			.catch(() => dispatch(addNotification({
48
				style: 'danger',
49
				msg: 'Ha ocurrido un error'
50
			})))
51
	}
52
 
53
	useEffect(() => {
54
		getData({
55
			url: table_link,
56
			params: {
57
				search: search,
58
				length: dataLength,
59
				page: pages.current
60
			}
61
		})
62
	}, [search, dataLength, pages.current])
63
 
64
	return (
65
		<>
11965 stevensc 66
			<section className="content">
67
				<div className="container-fluid">
68
					<div className="row">
69
						<div className="col-12">
70
							<Card>
71
								<Card.Header>
72
									<div className="row justify-content-end" style={{ gap: '10px' }}>
73
										{
74
											permisions.allowAdd
75
											&&
76
											<label
77
												className='d-flex align-items-center'
78
												onClick={() => {
79
													setActionLink(add_link)
12018 stevensc 80
													history.push('/performance-evaluation/forms/add')
11965 stevensc 81
												}}
82
												style={{ cursor: 'pointer' }}
83
											>
84
												<i className="fa fa-plus mr-2" />
85
												Agregar
86
											</label>
87
										}
88
										<label
89
											className='d-flex align-items-center'
90
											onClick={() => getData({
91
												url: table_link,
92
												params: {
93
													search: search,
94
													length: dataLength,
95
													page: pages.current
96
												}
97
											})}
98
											style={{ cursor: 'pointer' }}
99
										>
100
											<i className='fa fa-refresh mr-2' />
101
											Actualizar
102
										</label>
103
									</div>
104
									<div className="row justify-content-between align-items-center">
105
										<LengthFilter onChange={(e) => setDataLength(e.target.value)} />
106
										<SearchInput onChange={(e) => setSearch(e.target.value)} />
107
									</div>
108
								</Card.Header>
109
								<Card.Body>
110
									<div className="table-responsive">
111
										<Table data={items} headers={headers} setData={setItems}>
112
											{
113
												items.length
114
												&&
115
												items.map((item, index) => (
116
													<tr key={index}>
117
														<td className='text-vertical-middle'>{item.name}</td>
118
														<td className='text-vertical-middle'>{item.job_description}</td>
119
														<td className='text-vertical-middle'>
120
															{
121
																item.status === 'a'
122
																	? 'Activo'
123
																	: 'Inactivo'
124
															}
125
														</td>
126
														<td>
127
															<div className="d-flex align-items-center" style={{ gap: '5px' }}>
128
																{
129
																	permisions.allowEdit
130
																	&&
131
																	<i
132
																		className='fa fa-pencil'
133
																		onClick={() => {
134
																			setActionLink(item.actions.link_edit)
12018 stevensc 135
																			history.push('/performance-evaluation/forms/edit')
11965 stevensc 136
																		}}
137
																		style={{ cursor: 'pointer' }}
138
																	/>
139
																}
140
																{
141
																	permisions.allowDelete
142
																	&&
143
																	<i
144
																		className='fa fa-trash'
145
																		onClick={() => {
146
																			setActionLink(item.actions.link_delete)
147
																			setModalToShow('delete')
148
																		}}
149
																		style={{ cursor: 'pointer' }}
150
																	/>
151
																}
152
																{
153
																	permisions.allowReport
154
																	&&
155
																	<a href={item.actions.link_report} target='_blank' rel="noreferrer" >
12156 stevensc 156
																		<i className='fa fa-file' style={{ color: '#333' }} />
11965 stevensc 157
																	</a>
158
																}
159
															</div>
160
														</td>
161
													</tr>
162
												))
163
											}
164
										</Table>
165
									</div>
166
									<div className='row justify-content-between align-items-center'>
167
										<p className='mb-0'>
168
											{`Mostrando registros del ${(dataLength * pages.current) - (dataLength - 1) || 0} al ${(dataLength * pages.current) - (dataLength - total) || 0} de un total de ${total || 0} registros`}
169
										</p>
170
										<TablePagination
12326 stevensc 171
											onDecrement={() => setPages({ ...pages, current: pages.current - 1 })}
172
											onIncrement={() => setPages({ ...pages, current: pages.current + 1 })}
11965 stevensc 173
											totalPages={pages.last}
174
											currentPage={pages.current}
175
										/>
176
									</div>
177
								</Card.Body>
178
							</Card>
179
						</div>
180
					</div >
181
				</div >
182
			</section >
183
			<DeleteModal
184
				url={actionLink}
185
				isOpen={modalToShow === 'delete'}
186
				closeModal={() => setModalToShow('')}
187
				title="Esta seguro de borrar este formulario?"
188
				onComplete={() => setItems(items.filter((item) => item.actions.link_delete !== actionLink))}
189
				message="Registro borrado"
190
			/>
11960 stevensc 191
		</>
192
	)
193
}
194
 
195
export default TableView