diff --git a/db.json b/db.json index 285c67c0..e46cf3da 100644 --- a/db.json +++ b/db.json @@ -10,7 +10,7 @@ "id": 2, "name": "Pomegranate", "category": "Produce", - "isInCart": false + "isInCart": true }, { "id": 3, @@ -31,10 +31,10 @@ "isInCart": false }, { - "id": 6, "name": "Cookies", "category": "Dessert", - "isInCart": false + "isInCart": false, + "id": 6 } ] } \ No newline at end of file diff --git a/src/components/Item.js b/src/components/Item.js index 2f40e63f..c133e521 100644 --- a/src/components/Item.js +++ b/src/components/Item.js @@ -1,14 +1,41 @@ import React from "react"; -function Item({ item }) { +function Item({ item, onUpdateItem, onDeleteItem }) { + function 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)); + } + + function handleDeleteClick() { + fetch(`http://localhost:4000/items/${item.id}`, { + method: "DELETE", + }) + .then((res) => res.json()) + .then(() => onDeleteItem(item)); + } + return (
  • {item.name} {item.category} - - +
  • ); } diff --git a/src/components/ItemForm.js b/src/components/ItemForm.js index 5b91f0b5..f80b6c9b 100644 --- a/src/components/ItemForm.js +++ b/src/components/ItemForm.js @@ -1,11 +1,29 @@ import React, { useState } from "react"; -function ItemForm() { +function ItemForm({ onAddItem, onDeleteItem }) { const [name, setName] = useState(""); const [category, setCategory] = useState("Produce"); + const handleSubmit = (event) => { + event.preventDefault(); + const itemData = { + name: name, + category: category, + isInCart: false, + }; + fetch("http://localhost:4000/items", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(itemData), + }) + .then((res) => res.json()) + .then((newItem) => onAddItem(newItem)); + }; + return ( -
    +