diff --git a/db.json b/db.json index 285c67c0..562c9b81 100644 --- a/db.json +++ b/db.json @@ -29,12 +29,6 @@ "name": "Swiss Cheese", "category": "Dairy", "isInCart": false - }, - { - "id": 6, - "name": "Cookies", - "category": "Dessert", - "isInCart": false } ] } \ No newline at end of file diff --git a/src/components/Item.js b/src/components/Item.js index 2f40e63f..20974193 100644 --- a/src/components/Item.js +++ b/src/components/Item.js @@ -1,14 +1,40 @@ import React from "react"; -function Item({ item }) { +function Item({ item, onUpdateItem, onDeleteItem }) { + function handleDeleteClick() { + fetch(`http://localhost:4000/items/${item.id}`, { + method: "DELETE", + }) + .then((r) => r.json()) + .then(() => onDeleteItem(item)); + } + + function handleAddToCartClick() { + fetch(`http://localhost:4000/items/${item.id}`, { + method: "PATCH", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + isInCart: !item.isInCart, + }), + }) + .then((r) => r.json()) + .then((updatedItem) => onUpdateItem(updatedItem)); + } return (