diff --git a/db.json b/db.json index 285c67c0..54b000c2 100644 --- a/db.json +++ b/db.json @@ -1,40 +1,29 @@ { "items": [ { - "id": 1, - "name": "Yogurt", - "category": "Dairy", - "isInCart": false - }, - { - "id": 2, - "name": "Pomegranate", - "category": "Produce", + "id": 6, + "name": "Cookies", + "category": "Dessert", "isInCart": false }, { - "id": 3, - "name": "Lettuce", + "name": "bread", "category": "Produce", - "isInCart": false + "isInFact": false, + "id": 7 }, { - "id": 4, - "name": "String Cheese", + "name": "Milk", "category": "Dairy", - "isInCart": false + "isInFact": false, + "id": 8 }, { - "id": 5, - "name": "Swiss Cheese", + "name": "Cheese", "category": "Dairy", - "isInCart": false - }, - { - "id": 6, - "name": "Cookies", - "category": "Dessert", - "isInCart": false + "isInFact": false, + "id": 9, + "isInCart": true } ] } \ No newline at end of file diff --git a/src/components/Item.js b/src/components/Item.js index 2f40e63f..a23b0852 100644 --- a/src/components/Item.js +++ b/src/components/Item.js @@ -1,14 +1,34 @@ import React from "react"; -function Item({ item }) { +function Item({ item, onUpdateItem, onDeleteItem }) { + const handleAddToCartClick = () => { + fetch(`http://localhost:4000/items/${item.id}`,{ + method: "PATCH", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({isInCart: !item.isInCart}) + }) + .then(res => res.json()) + .then(updatedItem => onUpdateItem(updatedItem)) + } + + const handleDeleteCLick = () => { + fetch(`http://localhost:4000/items/${item.id}`, { + method: "DELETE", + }) + .then((r) => r.json()) + .then(() => onDeleteItem(item)) + } + return (