2025-10-28 11:45:51 -03:00
|
|
|
// frontend/src/components/TablaTitulares.tsx
|
|
|
|
|
|
|
|
|
|
import {
|
2025-10-28 11:54:36 -03:00
|
|
|
Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, Chip, IconButton, Typography
|
2025-10-28 11:45:51 -03:00
|
|
|
} from '@mui/material';
|
|
|
|
|
import DeleteIcon from '@mui/icons-material/Delete';
|
2025-10-28 11:54:36 -03:00
|
|
|
import DragHandleIcon from '@mui/icons-material/DragHandle'; // Importar el ícono
|
|
|
|
|
import { DndContext, closestCenter, PointerSensor, useSensor, useSensors, type DragEndEvent } from '@dnd-kit/core';
|
2025-10-28 11:45:51 -03:00
|
|
|
import { arrayMove, SortableContext, useSortable, verticalListSortingStrategy } from '@dnd-kit/sortable';
|
|
|
|
|
import { CSS } from '@dnd-kit/utilities';
|
|
|
|
|
import type { Titular } from '../types';
|
2025-10-28 14:12:05 -03:00
|
|
|
import EditIcon from '@mui/icons-material/Edit';
|
2025-10-28 11:45:51 -03:00
|
|
|
|
2025-10-28 11:54:36 -03:00
|
|
|
// La prop `onDelete` se añade para comunicar el evento al componente padre
|
|
|
|
|
interface SortableRowProps {
|
|
|
|
|
titular: Titular;
|
|
|
|
|
onDelete: (id: number) => void;
|
2025-10-28 14:12:05 -03:00
|
|
|
onEdit: (titular: Titular) => void;
|
2025-10-28 11:54:36 -03:00
|
|
|
}
|
|
|
|
|
|
2025-10-28 14:12:05 -03:00
|
|
|
const SortableRow = ({ titular, onDelete, onEdit }: SortableRowProps) => {
|
2025-10-28 11:45:51 -03:00
|
|
|
const { attributes, listeners, setNodeRef, transform, transition } = useSortable({ id: titular.id });
|
|
|
|
|
|
|
|
|
|
const style = {
|
|
|
|
|
transform: CSS.Transform.toString(transform),
|
|
|
|
|
transition,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getChipColor = (tipo: Titular['tipo']) => {
|
|
|
|
|
if (tipo === 'Edited') return 'warning';
|
|
|
|
|
if (tipo === 'Manual') return 'info';
|
|
|
|
|
return 'success';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-10-28 11:54:36 -03:00
|
|
|
<TableRow ref={setNodeRef} style={style} {...attributes} >
|
|
|
|
|
{/* El handle de arrastre ahora es un ícono */}
|
|
|
|
|
<TableCell sx={{ cursor: 'grab' }} {...listeners}>
|
|
|
|
|
<DragHandleIcon />
|
|
|
|
|
</TableCell>
|
2025-10-28 11:45:51 -03:00
|
|
|
<TableCell>{titular.texto}</TableCell>
|
|
|
|
|
<TableCell>
|
|
|
|
|
<Chip label={titular.tipo || 'Scraped'} color={getChipColor(titular.tipo)} size="small" />
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell>{titular.fuente}</TableCell>
|
|
|
|
|
<TableCell>
|
2025-10-28 14:12:05 -03:00
|
|
|
<IconButton size="small" onClick={(e) => { e.stopPropagation(); onEdit(titular); }}>
|
|
|
|
|
<EditIcon fontSize="small" />
|
|
|
|
|
</IconButton>
|
2025-10-28 11:54:36 -03:00
|
|
|
<IconButton size="small" onClick={(e) => { e.stopPropagation(); onDelete(titular.id); }}>
|
2025-10-28 11:45:51 -03:00
|
|
|
<DeleteIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-28 11:54:36 -03:00
|
|
|
interface TablaTitularesProps {
|
|
|
|
|
titulares: Titular[];
|
|
|
|
|
onReorder: (titulares: Titular[]) => void;
|
|
|
|
|
onDelete: (id: number) => void;
|
2025-10-28 14:12:05 -03:00
|
|
|
onEdit: (titular: Titular) => void;
|
2025-10-28 11:54:36 -03:00
|
|
|
}
|
2025-10-28 11:45:51 -03:00
|
|
|
|
2025-10-28 14:12:05 -03:00
|
|
|
const TablaTitulares = ({ titulares, onReorder, onDelete, onEdit }: TablaTitularesProps) => {
|
2025-10-28 11:54:36 -03:00
|
|
|
const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 5 } })); // Evita activar el drag con un simple clic
|
2025-10-28 11:45:51 -03:00
|
|
|
|
2025-10-28 11:54:36 -03:00
|
|
|
const handleDragEnd = (event: DragEndEvent) => {
|
2025-10-28 11:45:51 -03:00
|
|
|
const { active, over } = event;
|
2025-10-28 11:54:36 -03:00
|
|
|
if (over && active.id !== over.id) {
|
|
|
|
|
const oldIndex = titulares.findIndex((item) => item.id === active.id);
|
|
|
|
|
const newIndex = titulares.findIndex((item) => item.id === over.id);
|
|
|
|
|
const newArray = arrayMove(titulares, oldIndex, newIndex);
|
|
|
|
|
onReorder(newArray); // Pasamos el nuevo array al padre para que gestione el estado y la llamada a la API
|
2025-10-28 11:45:51 -03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-28 11:54:36 -03:00
|
|
|
if (titulares.length === 0) {
|
|
|
|
|
return (
|
|
|
|
|
<Paper elevation={3} sx={{ padding: 3, textAlign: 'center' }}>
|
|
|
|
|
<Typography>No hay titulares para mostrar.</Typography>
|
|
|
|
|
</Paper>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-28 11:45:51 -03:00
|
|
|
return (
|
2025-10-28 11:54:36 -03:00
|
|
|
<Paper elevation={3}>
|
|
|
|
|
<TableContainer>
|
|
|
|
|
<DndContext sensors={sensors} collisionDetection={closestCenter} onDragEnd={handleDragEnd}>
|
|
|
|
|
<SortableContext items={titulares.map(t => t.id)} strategy={verticalListSortingStrategy}>
|
|
|
|
|
<Table>
|
|
|
|
|
<TableHead>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableCell style={{ width: 50 }}></TableCell>
|
|
|
|
|
<TableCell>Texto del Titular</TableCell>
|
|
|
|
|
<TableCell>Tipo</TableCell>
|
|
|
|
|
<TableCell>Fuente</TableCell>
|
|
|
|
|
<TableCell>Acciones</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableHead>
|
|
|
|
|
<TableBody>
|
|
|
|
|
{titulares.map((titular) => (
|
2025-10-28 14:12:05 -03:00
|
|
|
<SortableRow key={titular.id} titular={titular} onDelete={onDelete} onEdit={onEdit} />
|
2025-10-28 11:54:36 -03:00
|
|
|
))}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</SortableContext>
|
|
|
|
|
</DndContext>
|
|
|
|
|
</TableContainer>
|
|
|
|
|
</Paper>
|
2025-10-28 11:45:51 -03:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default TablaTitulares;
|