Proyectos de Subversion LeadersLinked - Backend

Rev

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