Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
11024 stevensc 1
/* eslint-disable no-mixed-spaces-and-tabs */
10061 stevensc 2
import React, { useState, useEffect } from 'react'
3
import axios from 'axios'
4
import { Card } from 'react-bootstrap'
5
import { LengthFilter, SearchInput, Table, TablePagination } from '../../components/TableComponents'
6
import { addNotification } from '../../../redux/notification/notification.actions'
7
import { useDispatch } from 'react-redux'
8
import DeleteModal from '../../../shared/DeleteModal'
10095 stevensc 9
import ContentTitle from '../../../shared/ContentTitle'
11046 stevensc 10
import AddModal from '../components/AddModal'
11061 stevensc 11
import EditModal from '../components/EditModal'
9954 stevensc 12
 
10061 stevensc 13
const headers = [
11024 stevensc 14
	{ key: 'first_name', label: 'Nombre', isSorteable: true },
15
	{ key: 'last_name', label: 'Apellido', isSorteable: true },
16
	{ key: 'email', label: 'Correo electrónico', isSorteable: true },
17
	{ key: 'status', label: 'Estatus', isSorteable: false },
18
	{ key: 'actions', label: 'Acciones', isSorteable: false }
10061 stevensc 19
]
9954 stevensc 20
 
11024 stevensc 21
const MainView = ({ email_link, add_link, table_link, permisions, vacancies }) => {
9954 stevensc 22
 
11024 stevensc 23
	const dispatch = useDispatch()
24
	const [showDeleteModal, setShowDeleteModal] = useState(false)
11046 stevensc 25
	const [isShowAddModal, setIsShowAddModal] = useState(false)
11061 stevensc 26
	const [currentEditLink, setCurrentEditLink] = useState('')
27
	const [isShowEditModal, setIsShowEditModal] = useState(false)
14393 kerby 28
	const [currentVacancy, setCurrentVacancy] = useState((vacancies.length > 0)? vacancies[0].uuid : '')
11024 stevensc 29
	const [actionLink, setActionLink] = useState('')
12712 stevensc 30
	const [items, setItems] = useState([])
31
	const [total, setTotal] = useState(0)
11024 stevensc 32
	const [search, setSearch] = useState('')
33
	const [dataLength, setDataLength] = useState(10)
12375 stevensc 34
	const [startItem, setStartItem] = useState(1)
35
	const [lastItem, setLastItem] = useState(10)
11024 stevensc 36
	const [pages, setPages] = useState({
37
		current: 1,
38
		last: 1
39
	})
10061 stevensc 40
 
11024 stevensc 41
	const getData = ({ url = '', params = {} }) => {
10061 stevensc 42
 
11024 stevensc 43
		axios.get(`${url}/${currentVacancy}`, { params: { ...params } })
44
			.then(({ data }) => {
45
				if (!data.success) {
46
					dispatch(addNotification({
11051 stevensc 47
						style: 'danger',
11024 stevensc 48
						msg: 'Ha ocurrido un error'
49
					}))
50
				}
10061 stevensc 51
 
12712 stevensc 52
				setItems(data.data.items)
53
				setTotal(data.data.total)
11024 stevensc 54
				setPages({ ...pages, last: Math.ceil(data.data.total / dataLength) })
55
			})
56
			.catch(() => dispatch(addNotification({
11051 stevensc 57
				style: 'danger',
11024 stevensc 58
				msg: 'Ha ocurrido un error'
59
			})))
60
	}
10061 stevensc 61
 
11024 stevensc 62
	useEffect(() => {
63
		getData({
64
			url: table_link,
65
			params: {
66
				search: search,
67
				length: dataLength,
12375 stevensc 68
				start: pages.current
11024 stevensc 69
			}
70
		})
11025 stevensc 71
	}, [search, dataLength, pages.current, currentVacancy])
10061 stevensc 72
 
12375 stevensc 73
	useEffect(() => {
74
		if (pages.current > 1) {
75
			setStartItem((dataLength * (pages.current - 1)) + 1)
76
		} else {
77
			setStartItem(1)
78
		}
79
	}, [pages.current])
80
 
81
	useEffect(() => {
12712 stevensc 82
		if (items) {
12375 stevensc 83
			if (startItem > 1) {
12712 stevensc 84
				setLastItem(startItem + (items.length - 1))
12375 stevensc 85
			} else {
12712 stevensc 86
				setLastItem(items.length)
12375 stevensc 87
			}
88
		}
12712 stevensc 89
	}, [items])
12375 stevensc 90
 
11024 stevensc 91
	return (
92
		<ContentTitle title='Preselección'>
93
			<section className="content">
94
				<div className="container-fluid">
95
					<div className="row">
96
						<div className="col-12">
97
							<Card>
98
								<Card.Header>
99
									<div className="row" style={{ gap: '10px' }}>
100
										<div className="form-group">
101
											<label>Vacantes</label>
102
											<select
103
												className="form-control"
104
												value={currentVacancy}
105
												onChange={(e) => setCurrentVacancy(e.target.value)}
106
												defaultValue={vacancies[0].uuid}
107
											>
108
												{
109
													vacancies.map((vacancy) => (
110
														<option key={vacancy.uuid} value={vacancy.uuid}>{vacancy.name}</option>
111
													))
112
												}
113
											</select>
114
										</div>
115
									</div>
116
									<div className="row justify-content-end" style={{ gap: '10px' }}>
117
										{
118
											permisions.allowAdd
11059 stevensc 119
											&&
120
											<label
121
												className='d-flex align-items-center'
122
												onClick={() => setIsShowAddModal(true)}
123
												style={{ cursor: 'pointer' }}
124
											>
125
												<i className="fa fa-plus mr-2" />
126
												Agregar
127
											</label>
11024 stevensc 128
										}
129
										<label
130
											className='d-flex align-items-center'
131
											onClick={() => getData({
132
												url: table_link,
133
												params: {
134
													search: search,
135
													length: dataLength,
136
													page: pages.current
137
												}
138
											})}
139
											style={{ cursor: 'pointer' }}
140
										>
141
											<i className='fa fa-refresh mr-2' />
11059 stevensc 142
											Actualizar
11024 stevensc 143
										</label>
144
									</div>
145
									<div className="row justify-content-between align-items-center">
146
										<LengthFilter onChange={(e) => setDataLength(e.target.value)} />
147
										<SearchInput onChange={(e) => setSearch(e.target.value)} />
148
									</div>
149
								</Card.Header>
150
								<Card.Body>
11268 stevensc 151
									<div className="table-responsive">
12712 stevensc 152
										<Table data={items} headers={headers} setData={setItems}>
11268 stevensc 153
											{
12712 stevensc 154
												items.map((item, index) => (
11268 stevensc 155
													<tr key={index}>
156
														<td>{item.first_name}</td>
157
														<td>{item.last_name}</td>
158
														<td>{item.email}</td>
159
														<td>
160
															{
161
																item.status === 'a'
162
																	? <i className='fa fa-check' style={{ color: '#5cb85c' }} />
163
																	: <i className='fa fa-close' style={{ color: 'red' }} />
164
															}
165
														</td>
166
														<td className='d-flex' style={{ gap: '10px' }}>
167
															{
168
																permisions.allowEdit
169
																&&
170
																<i
171
																	className='fa fa-pencil'
172
																	style={{ cursor: 'pointer' }}
173
																	onClick={() => {
174
																		setCurrentEditLink(item.actions.link_edit)
175
																		setIsShowEditModal(true)
176
																	}}
177
																/>
178
															}
179
															{
180
																permisions.allowDelete
181
																&&
182
																<i
183
																	className='fa fa-trash'
184
																	onClick={() => {
185
																		setShowDeleteModal(true)
186
																		setActionLink(item.actions.link_delete)
187
																	}}
188
																	style={{ cursor: 'pointer' }}
189
																/>
190
															}
191
														</td>
192
													</tr>
193
												))
194
											}
195
										</Table>
196
									</div>
11024 stevensc 197
									<div className='row justify-content-between align-items-center'>
198
										<p className='mb-0'>
12712 stevensc 199
											{`Mostrando registros del ${startItem} al ${lastItem} de un total de ${total} registros`}
11024 stevensc 200
										</p>
201
										<TablePagination
12326 stevensc 202
											onDecrement={() => setPages({ ...pages, current: pages.current - 1 })}
203
											onIncrement={() => setPages({ ...pages, current: pages.current + 1 })}
11024 stevensc 204
											totalPages={pages.last}
205
											currentPage={pages.current}
206
										/>
207
									</div>
208
								</Card.Body>
209
							</Card>
210
						</div>
211
					</div >
212
				</div >
213
			</section >
11049 stevensc 214
			<AddModal
11046 stevensc 215
				email_link={email_link}
11059 stevensc 216
				add_link={add_link.replace('UUID_PLACEHOLDER', currentVacancy)}
11046 stevensc 217
				closeModal={() => setIsShowAddModal(false)}
218
				isOpen={isShowAddModal}
15061 stevensc 219
				onComplete={()=> getData({url: table_link})}
11049 stevensc 220
			/>
11061 stevensc 221
			<EditModal
222
				edit_link={currentEditLink}
223
				closeModal={() => setIsShowEditModal(false)}
224
				isOpen={isShowEditModal}
11227 stevensc 225
				resetLink={() => setCurrentEditLink('')}
11061 stevensc 226
			/>
11024 stevensc 227
			<DeleteModal
228
				url={actionLink}
229
				isOpen={showDeleteModal}
230
				closeModal={() => setShowDeleteModal(false)}
231
				title="Esta seguro de borrar esta vacante?"
12712 stevensc 232
				onComplete={() => setItems(items.filter((item) => item.actions.link_delete !== actionLink))}
11024 stevensc 233
				message="Vacante eliminada"
234
			/>
235
		</ContentTitle>
236
	)
9954 stevensc 237
}
238
export default MainView