@@ -2,6 +2,7 @@ import { useReactTable, getCoreRowModel, flexRender } from '@tanstack/react-tabl
22import { useEffect , useMemo , useState } from 'react'
33import "./Table.css"
44import EditTaskForm from './modals/EditTaskForm' ;
5+ import DeleteButton from './DeleteButton' ;
56
67export default function Table ( { data} ) {
78
@@ -13,6 +14,7 @@ export default function Table({data}) {
1314 { header : 'Estatus' , accessorFn : row => row . status . percentage } ,
1415 { header : 'Descripción' , accessorKey : 'description' } ,
1516 { header : 'Asignación' , accessorKey : 'asignee' } ,
17+ { header : 'Borrar' , cell : info => < DeleteButton onDelete = { ( e ) => handleDelete ( e , info ) } /> }
1618 ] )
1719
1820 const table = useReactTable ( {
@@ -21,6 +23,22 @@ export default function Table({data}) {
2123 getCoreRowModel : getCoreRowModel ( ) ,
2224 } )
2325
26+ function handleDelete ( e , info ) {
27+ e . stopPropagation ( ) ;
28+ setTasks ( ( prev ) => prev . filter ( task => task . _id !== info . row . original . _id ) )
29+
30+ fetch ( `${ import . meta. env . VITE_SERVER_URL } /${ info . row . original . _id } ` , {
31+ method : 'DELETE'
32+ } )
33+ . then ( res => {
34+ if ( ! res . ok ) {
35+ throw new Error ( res . statusText ) ;
36+ }
37+ } )
38+ . catch ( ( error ) => console . log ( error ) )
39+
40+ }
41+
2442 function handleRowClick ( row ) {
2543 setSelectedTask ( row . original ) ;
2644 }
@@ -53,10 +71,7 @@ export default function Table({data}) {
5371 setSelectedTask ( null )
5472 }
5573
56- useEffect ( ( ) => {
57- console . log ( "Tasks changed: " , tasks ) ;
58- } , [ tasks ] )
59-
74+
6075 return (
6176 < >
6277 < table className = "task-table" >
@@ -73,6 +88,7 @@ export default function Table({data}) {
7388 </ thead >
7489 < tbody >
7590 { table . getRowModel ( ) . rows . map ( row => (
91+
7692 < tr onClick = { ( ) => handleRowClick ( row ) } key = { row . id } >
7793 { row . getVisibleCells ( ) . map ( cell => (
7894 < td key = { cell . id } >
0 commit comments