Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
15314 stevensc 1
import React, { useState, useEffect } from 'react'
2
import axios from 'axios'
9512 stevensc 3
import { Card } from 'react-bootstrap'
15310 stevensc 4
import { LengthFilter, SearchInput, TablePagination } from '../../components/TableComponents'
15314 stevensc 5
import { addNotification } from '../../../redux/notification/notification.actions'
6
import { useDispatch } from 'react-redux'
9908 stevensc 7
import DeleteModal from '../../../shared/DeleteModal'
15310 stevensc 8
import Table from '../../../shared/content-table/Table'
9
import TableRow from '../../../shared/content-table/TableRow'
9469 stevensc 10
 
9543 stevensc 11
const headers = [
11264 stevensc 12
	{ key: 'name', label: 'Nombre', isSorteable: true },
13
	{ key: 'job_description', label: 'Descripción de cargo', isSorteable: true },
12375 stevensc 14
	{ key: 'status', label: 'Estatus', isSorteable: true },
11264 stevensc 15
	{ key: 'actions', label: 'Acciones', isSorteable: false }
9543 stevensc 16
]
17
 
15103 stevensc 18
const MainView = ({
19
	table_link,
20
	setActionLink,
21
	permisions,
22
	add_link,
23
	setAction }) => {
9502 stevensc 24
 
15314 stevensc 25
	const dispatch = useDispatch()
26
	const [showDeleteModal, setShowDeleteModal] = useState(false)
27
	const [deleteLink, setDeleteLink] = useState('')
28
	const [items, setItems] = useState([])
29
	const [total, setTotal] = useState(0)
11264 stevensc 30
	const [search, setSearch] = useState('')
15314 stevensc 31
	const [startItem, setStartItem] = useState(1)
32
	const [lastItem, setLastItem] = useState(10)
11264 stevensc 33
	const [dataLength, setDataLength] = useState(10)
15314 stevensc 34
	const [pages, setPages] = useState({
35
		current: 1,
36
		last: 1
37
	})
15313 stevensc 38
 
15314 stevensc 39
	const getData = ({ url = '', params = {} }) => {
9502 stevensc 40
 
15314 stevensc 41
		axios.get(url, { params: { ...params } })
42
			.then(({ data }) => {
43
				if (!data.success) {
44
					dispatch(addNotification({
45
						style: 'danger',
46
						msg: 'Ha ocurrido un error'
47
					}))
48
				}
15304 stevensc 49
 
15314 stevensc 50
				setItems(data.data.items)
51
				setTotal(data.data.total)
52
				setPages({ ...pages, last: Math.ceil(data.data.total / dataLength) })
53
			})
54
			.catch(() => dispatch(addNotification({
55
				style: 'danger',
56
				msg: 'Ha ocurrido un error'
57
			})))
58
	}
15313 stevensc 59
 
15314 stevensc 60
	useEffect(() => {
61
		getData({
62
			url: table_link,
63
			params: {
64
				search: search,
65
				length: dataLength,
66
				start: pages.current
67
			}
68
		})
69
	}, [search, dataLength, pages.current])
70
 
71
	useEffect(() => {
72
		if (pages.current > 1) {
73
			setStartItem((dataLength * (pages.current - 1)) + 1)
74
		} else {
75
			setStartItem(1)
76
		}
77
	}, [pages.current])
78
 
79
	useEffect(() => {
80
		if (items) {
81
			if (startItem > 1) {
82
				setLastItem(startItem + (items.length - 1))
83
			} else {
84
				setLastItem(items.length)
85
			}
86
		}
87
	}, [items])
88
 
89
 
90
	const handleSort = (result) => setItems(result)
91
 
15310 stevensc 92
	const handleEdit = (url) => {
93
		setActionLink(url)
94
		setAction('edit')
95
	}
96
 
15314 stevensc 97
	const handleDelete = (url) => {
98
		setDeleteLink(url)
99
		setShowDeleteModal(true)
100
	}
15310 stevensc 101
 
11264 stevensc 102
	return (
103
		<>
104
			<section className="content">
105
				<div className="container-fluid">
106
					<div className="row">
107
						<div className="col-12">
108
							<Card>
109
								<Card.Header>
110
									<div className="row justify-content-end" style={{ gap: '10px' }}>
15103 stevensc 111
										{permisions.allowAdd &&
11268 stevensc 112
											<label
113
												className='d-flex align-items-center'
114
												onClick={() => {
115
													setActionLink(add_link)
15103 stevensc 116
													setAction('add')
11268 stevensc 117
												}}
118
												style={{ cursor: 'pointer' }}
119
											>
120
												<i className="fa fa-plus mr-2" />
121
												Agregar
122
											</label>
11264 stevensc 123
										}
124
										<label
125
											className='d-flex align-items-center'
15314 stevensc 126
											onClick={() => getData({
127
												url: table_link,
128
												params: {
129
													search: search,
130
													length: dataLength,
131
													page: pages.current
132
												}
133
											})}
11264 stevensc 134
											style={{ cursor: 'pointer' }}
135
										>
136
											<i className='fa fa-refresh mr-2' />
11268 stevensc 137
											Actualizar
11264 stevensc 138
										</label>
139
									</div>
140
									<div className="row justify-content-between align-items-center">
141
										<LengthFilter onChange={(e) => setDataLength(e.target.value)} />
142
										<SearchInput onChange={(e) => setSearch(e.target.value)} />
143
									</div>
144
								</Card.Header>
145
								<Card.Body>
11268 stevensc 146
									<div className="table-responsive">
15314 stevensc 147
										<Table
148
											data={items}
149
											headers={headers}
150
											setData={handleSort}
151
										>
15310 stevensc 152
											{() => items.map((item, index) =>
153
												<TableRow
154
													key={index}
155
													item={item}
156
													editOptions={{
157
														permission: permisions.allowEdit,
15311 stevensc 158
														icon: (value) => <i className='fa fa-pencil cursor-pointer' onClick={() => handleEdit(value)} />
15310 stevensc 159
													}}
160
													deleteOptions={{
161
														permission: permisions.allowDelete,
15311 stevensc 162
														icon: (value) => <i className='fa fa-trash  cursor-pointer' onClick={() => handleDelete(value)} />
15310 stevensc 163
													}}
164
												/>)}
15298 stevensc 165
										</Table>
11268 stevensc 166
									</div>
11264 stevensc 167
									<div className='row justify-content-between align-items-center'>
168
										<p className='mb-0'>
12712 stevensc 169
											{`Mostrando registros del ${startItem} al ${lastItem} de un total de ${total} registros`}
11264 stevensc 170
										</p>
171
										<TablePagination
15314 stevensc 172
											onDecrement={() => setPages({ ...pages, current: pages.current - 1 })}
173
											onIncrement={() => setPages({ ...pages, current: pages.current + 1 })}
174
											totalPages={pages.last}
175
											currentPage={pages.current}
11264 stevensc 176
										/>
177
									</div>
178
								</Card.Body>
179
							</Card>
180
						</div>
181
					</div >
182
				</div >
183
			</section >
184
			<DeleteModal
15314 stevensc 185
				url={deleteLink}
186
				isOpen={showDeleteModal}
187
				closeModal={() => setShowDeleteModal(false)}
11264 stevensc 188
				title="Esta seguro de borrar esta vacante?"
15314 stevensc 189
				onComplete={() => setItems(items.filter((item) => item.actions.link_delete !== deleteLink))}
11264 stevensc 190
				message="Vacante eliminada"
191
			/>
192
		</>
193
	)
9489 stevensc 194
}
195
export default MainView