Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 12269 | Ir a la última revisión | | 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}
129
															{item.actions.link_both}
130
														</td>
131
														<td className='text-vertical-middle'>
132
															{item.supervisor}
133
															{item.actions.link_superviser}
134
														</td>
135
														<td className='text-vertical-middle'>
136
															{`${item.first_name} ${item.last_name}`}
137
															{item.actions.link_self}
138
														</td>
139
														<td>
140
															<div className="d-flex align-items-center" style={{ gap: '5px' }}>
141
																{
142
																	permisions.allowDelete
143
																	&&
144
																	<i
145
																		className='fa fa-trash'
146
																		onClick={() => {
147
																			setActionLink(item.actions.link_delete)
148
																			setModalToShow('delete')
149
																		}}
150
																		style={{ cursor: 'pointer' }}
151
																	/>
152
																}
153
															</div>
154
														</td>
155
													</tr>
156
												))
157
											}
158
										</Table>
159
									</div>
160
									<div className='row justify-content-between align-items-center'>
161
										<p className='mb-0'>
162
											{`Mostrando registros del ${(dataLength * pages.current) - (dataLength - 1) || 0} al ${(dataLength * pages.current) - (dataLength - total) || 0} de un total de ${total || 0} registros`}
163
										</p>
164
										<TablePagination
165
											onDecrement={() => setPages(prev => prev.current -= 1)}
166
											onIncrement={() => setPages(prev => prev.current += 1)}
167
											totalPages={pages.last}
168
											currentPage={pages.current}
169
										/>
170
									</div>
171
								</Card.Body>
172
							</Card>
173
						</div>
174
					</div >
175
				</div >
176
			</section >
177
			<DeleteModal
178
				url={actionLink}
179
				isOpen={modalToShow === 'delete'}
180
				closeModal={() => setModalToShow('')}
181
				title="Esta seguro de borrar esta evaluación?"
182
				onComplete={() => setItems(items.filter((item) => item.actions.link_delete !== actionLink))}
183
				message="Registro borrado"
184
			/>
185
		</>
186
	)
187
}
188
 
189
export default TableView