Comece criando o componente “DeleteButton.jsx”:
import { useNavigate } from "react-router-dom"
import useStock from "../hooks/useStock"
import PropTypes from "prop-types"
DeleteButton.propTypes = {
itemId: PropTypes.number.isRequired,
itemName: PropTypes.string.isRequired
}
export default function DeleteButton({ itemId, itemName }) {
const { deleteItem } = useStock()
const navigate = useNavigate()
const handleDelete = () => {
if (confirm(`Tem certeza que deseja excluir ${itemName}?`)) {
deleteItem(itemId)
navigate("/items")
}
}
return (
<button
className="button is-danger is-small"
onClick={handleDelete}
>
Excluir
</button>
)
}
E inclua-o no componente “ItemsTable.jsx”:
import { Link } from "react-router-dom";
import useStock from "../hooks/useStock";
import DeleteButton from "./DeleteButton";
export default function ItemsTable() {
const { items } = useStock();
// ...
<Link to={`/items/${item.id}/update`} className="button is-small">
Atualizar
</Link>
<DeleteButton itemId={item.id} itemName={item.name} />
</td>
</tr>
))}
</tbody>
</table>
)
}
Atualize o componente da página “UpdateItem.jsx”:
import { useParams } from "react-router-dom"
import useStock from "../../hooks/useStock"
import ItemForm from "../../components/ItemForm"
export default function UpdateItem() {
const { getItem } = useStock()
const { id } = useParams()
const item = getItem(id)
return (
<>
<h2>Atualizar Item - {item.name}</h2>
<ItemForm itemToUpdate={item} />
</>
)
}
Atualize também o componente da página “ShowItem.jsx”:
import { Link, useParams } from "react-router-dom"
import useStock from "../../hooks/useStock"
import DeleteButton from "../../components/DeleteButton"
export default function ShowItem() {
const { getItem } = useStock()
const { id } = useParams()
const item = getItem(id)
return (
<div className="item">
<h2>{item.name}</h2>
<Link to={`/items/${item.id}/update`} className="button is-small">Atualizar</Link>
<DeleteButton itemId={item.id} itemName={item.name} />
<div className="row">
<span>Categoria: {item.category}</span>
<span>Quantidade em estoque: {item.quantity}</span>
<span>Preço: R$ {item.price}</span>
</div>
<p>{item.description}</p>
<div className="row">
<p>Cadastrado em: {item.createdAt.toDateString()}</p>
<p>Atualizado em: {item.updatedAt.toDateString()}</p>
</div>
</div>
)
}
Por fim, atualize o componente “Home.jsx” para incluir o dashboard:
import { Link } from "react-router-dom"
import useStock from "../hooks/useStock"
export default function Home() {
const { items } = useStock()
const diversity = items.length
const inventoryTotal = items.reduce((sum, item) => +sum + +item.quantity, 0)
const today = new Date()
const limitDate = new Date()
limitDate.setDate(limitDate.getDate() - 10)
const recentItems = items.filter((item) => item.createdAt >= limitDate && item.createdAt <= today)
const recentTotal = recentItems.length
const lowQuantityItems = items.filter((item) => item.quantity < 10)
const lowQuantityTotal = lowQuantityItems.length
return (
<main>
<h1>Dashboard</h1>
<div className="row">
<div className="dashboard-card">
Diversidade de itens
<span>{diversity}</span>
</div>
<div className="dashboard-card">
Inventário total
<span>{inventoryTotal}</span>
</div>
<div className="dashboard-card">
Itens recentes
<span>{recentTotal}</span>
</div>
<div className="dashboard-card">
Itens acabando
<span>{lowQuantityTotal}</span>
</div>
</div>
<div className="row">
<div className="recent">
<table>
<thead>
<tr><th>Itens Recentes</th>
<th>Ações</th>
</tr>
</thead>
<tbody>
{recentItems.map((item) => (
<tr key={item.id}>
<td>{item.name}</td>
<td><Link to={`/items/${item.id}`} className="button is-small">Ver</Link></td>
</tr>
))}
</tbody>
</table>
</div>
<div className="low">
<table>
<thead>
<tr>
<th>Itens acabando</th>
<th>Qtd.</th>
<th>Ações</th>
</tr>
</thead>
<tbody>
{lowQuantityItems.map((item) => (
<tr key={item.id}>
<td>{item.name}</td>
<td>{item.quantity}</td>
<td><Link to={`/items/${item.id}`} className="button is-small">Ver</Link></td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</main>
)
}