Skip to content

Commit 0ea0720

Browse files
committed
added delete button to table
1 parent 0586315 commit 0ea0720

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

client/src/components/Button.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
button {
1+
.modify-button {
22
background-color: rgb(92, 2, 117);
33
color: white;
44
border: none;

client/src/components/Button.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import './Button.css';
22

33
export default function Button({type, text, fn}) {
44
return(
5-
<button type={type || "button"} onClick={fn}>{text}</button>
5+
<button className='modify-button' type={type || "button"} onClick={fn}>{text}</button>
66
);
77
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.delete-button {
2+
background-color: rgb(188, 28, 4);
3+
color: white;
4+
border: none;
5+
box-shadow: none;
6+
padding: 10px;
7+
font-weight: 600;
8+
border-radius: 10px;
9+
display: block;
10+
box-sizing: border-box;
11+
margin: 0;
12+
}
13+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import './DeleteButton.css'
2+
3+
export default function DeleteButton({onDelete}) {
4+
return(
5+
<button onClick={onDelete} className='delete-button'>X</button>
6+
)
7+
}

client/src/components/Table.jsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useReactTable, getCoreRowModel, flexRender } from '@tanstack/react-tabl
22
import { useEffect, useMemo, useState } from 'react'
33
import "./Table.css"
44
import EditTaskForm from './modals/EditTaskForm';
5+
import DeleteButton from './DeleteButton';
56

67
export 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

Comments
 (0)