Rev 12326 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
/* eslint-disable no-mixed-spaces-and-tabs */
import React, { useState, useEffect } from 'react'
import { Card } from 'react-bootstrap'
import { useDispatch } from 'react-redux'
import axios from 'axios'
import { LengthFilter, SearchInput, Table, TablePagination } from '../../../recruitment_and_selection/components/TableComponents'
import { addNotification } from '../../../redux/notification/notification.actions'
import ContentTitle from '../../../shared/ContentTitle'
import DeleteModal from '../../../shared/DeleteModal'
import EditAndAddModal from '../components/EditAndAddModal'
const headers = [
{ key: 'title', label: 'Nombre', isSorteable: true },
{ key: 'description', label: 'Descripción', isSorteable: true },
{ key: 'progress', label: 'Progreso', isSorteable: true },
{ key: 'cost', label: 'Costo', isSorteable: true },
{ key: 'status', label: 'Estatus', isSorteable: true },
{ key: 'actions', label: 'Acciones', isSorteable: false }
]
const TableView = ({ add_link, table_link, goBack_link, permisions }) => {
const dispatch = useDispatch()
const [modalToShow, setModalToShow] = useState('')
const [actionLink, setActionLink] = useState(add_link)
const [items, setItems] = useState([])
const [goals, setGoals] = useState({})
const [total, setTotal] = useState(0)
const [startItem, setStartItem] = useState(1)
const [lastItem, setLastItem] = useState(10)
const [search, setSearch] = useState('')
const [dataLength, setDataLength] = useState(10)
const [pages, setPages] = useState({
current: 1,
last: 1
})
const getData = ({ url = '', params = {} }) => {
axios.get(url, { params: { ...params } })
.then(({ data }) => {
if (!data.success) {
return dispatch(addNotification({
style: 'danger',
msg: 'Ha ocurrido un error'
}))
}
setItems(data.data.items)
setGoals(data.data.goals)
setTotal(data.data.total)
setPages({ ...pages, last: Math.ceil(data.data.total / dataLength) })
})
.catch(() => dispatch(addNotification({
style: 'danger',
msg: 'Ha ocurrido un error'
})))
}
useEffect(() => {
getData({
url: table_link,
params: {
search: search,
length: dataLength,
start: pages.current
}
})
}, [search, dataLength, pages.current])
useEffect(() => {
if (pages.current > 1) {
setStartItem((dataLength * (pages.current - 1)) + 1)
} else {
setStartItem(1)
}
}, [pages.current])
useEffect(() => {
if (items) {
if (startItem > 1) {
setLastItem(startItem + (items.length - 1))
} else {
setLastItem(items.length)
}
}
}, [items])
return (
<ContentTitle title={`Tareas de la meta: ${goals.titleGoals || ''}`}>
<div className="col-sm-12 d-flex flex-column p-2">
<div className="m-2">
<strong>Descripción:</strong>
<br />
<span className="xd">{goals.descriptionGoals}</span>
</div>
<div className="m-2 d-flex">
<div>
<strong>Estatus:</strong>
<br />
<span className="xd">{goals.statusGoals}</span>
</div>
<div className="ml-5">
<strong>Costo:</strong>
<br />
<span className="xd">{goals.costGoals}</span>
</div>
<div className="ml-5 ">
<strong>Progreso:</strong>
<br />
{
goals.indicatorGoals > 0
?
<div className="d-flex align-items-center">
<progress value={goals.indicatorGoals || 0} max='100' />
<span className="ml-2">{`${goals.indicatorGoals}%`}</span>
</div>
:
'Sin tareas'
}
</div>
</div>
<div className="col-sm-12 mt-3">
<a href={goBack_link} className="btn btn-primary">
<i className="fa fa-arrow-left mr-1" />
Ir atras
</a>
</div>
</div>
<section className="content">
<div className="container-fluid">
<div className="row">
<div className="col-12">
<Card>
<Card.Header>
<div className="row justify-content-end" style={{ gap: '10px' }}>
{
permisions.allowAdd
&&
<label
className='d-flex align-items-center'
onClick={() => {
setActionLink(add_link)
setModalToShow('add')
}}
style={{ cursor: 'pointer' }}
>
<i className="fa fa-plus mr-2" />
Agregar
</label>
}
<label
className='d-flex align-items-center'
onClick={() => getData({
url: table_link,
params: {
search: search,
length: dataLength,
page: pages.current
}
})}
style={{ cursor: 'pointer' }}
>
<i className='fa fa-refresh mr-2' />
Actualizar
</label>
</div>
<div className="row justify-content-between align-items-center">
<LengthFilter onChange={(e) => setDataLength(e.target.value)} />
<SearchInput onChange={(e) => setSearch(e.target.value)} />
</div>
</Card.Header>
<Card.Body>
<div className="table-responsive">
<Table data={items} headers={headers} setData={setItems}>
{
items.length
&&
items.map((item, index) => (
<tr key={index}>
<td className='text-vertical-middle'>{item.title}</td>
<td className='text-vertical-middle'>{item.description}</td>
<td className='d-flex align-items-center'>
{
item.progress > 0
?
<>
<progress value={item.progress} max='100' />
<span className='ml-2'>{item.progress}</span>
</>
:
'Sin progreso'
}
</td>
<td className='text-vertical-middle'>{`$${item.cost}`}</td>
<td className='text-vertical-middle'>
{
item.status === 'a'
? 'Activo'
: 'Inactivo'
}
</td>
<td>
<div className="d-flex align-items-center" style={{ gap: '5px' }}>
{
permisions.allowEdit
&&
<i
className='fa fa-pencil'
onClick={() => {
setActionLink(item.actions.link_edit)
setModalToShow('edit')
}}
style={{ cursor: 'pointer' }}
/>
}
{
permisions.allowDelete
&&
<i
className='fa fa-trash'
onClick={() => {
setActionLink(item.actions.link_delete)
setModalToShow('delete')
}}
style={{ cursor: 'pointer' }}
/>
}
</div>
</td>
</tr>
))
}
</Table>
</div>
<div className='row justify-content-between align-items-center'>
<p className='mb-0'>
{`Mostrando registros del ${startItem} al ${lastItem} de un total de ${total} registros`}
</p>
<TablePagination
onDecrement={() => setPages({ ...pages, current: pages.current - 1 })}
onIncrement={() => setPages({ ...pages, current: pages.current + 1 })}
totalPages={pages.last}
currentPage={pages.current}
/>
</div>
</Card.Body>
</Card>
</div>
</div >
</div >
</section >
<EditAndAddModal
action_link={actionLink}
type={modalToShow}
closeModal={() => setModalToShow('')}
onComplete={() => getData({
url: table_link,
params: {
search: search,
length: dataLength,
page: pages.current
}
})}
/>
<DeleteModal
url={actionLink}
isOpen={modalToShow === 'delete'}
closeModal={() => setModalToShow('')}
title="Esta seguro de borrar esta tarea?"
onComplete={() => setItems(items.filter((item) => item.actions.link_delete !== actionLink))}
message="Registro borrado"
/>
</ContentTitle >
)
}
export default TableView