diff --git a/db.json b/db.json index 285c67c0..a0ba37d6 100644 --- a/db.json +++ b/db.json @@ -6,35 +6,23 @@ "category": "Dairy", "isInCart": false }, - { - "id": 2, - "name": "Pomegranate", - "category": "Produce", - "isInCart": false - }, { "id": 3, "name": "Lettuce", "category": "Produce", - "isInCart": false + "isInCart": true }, { "id": 4, "name": "String Cheese", "category": "Dairy", - "isInCart": false + "isInCart": true }, { "id": 5, "name": "Swiss Cheese", "category": "Dairy", - "isInCart": false - }, - { - "id": 6, - "name": "Cookies", - "category": "Dessert", - "isInCart": false + "isInCart": true } ] } \ No newline at end of file diff --git a/src/components/Item.js b/src/components/Item.js index 2f40e63f..8c4d893d 100644 --- a/src/components/Item.js +++ b/src/components/Item.js @@ -1,16 +1,43 @@ -import React from "react"; +import React from "react" + +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((r) => r.json()) + .then((updatedItem) => onUpdateItem(updatedItem)) + } + + function handleDeleteClick() { + fetch(`http://localhost:4000/items/${item.id}`, { + method: "DELETE", + }) + .then((r) => r.json()) + .then(() => onDeleteItem(item)) + } -function Item({ item }) { return (
  • {item.name} {item.category} - - +
  • - ); + ) } -export default Item; +export default Item diff --git a/src/components/ItemForm.js b/src/components/ItemForm.js index 5b91f0b5..10c56c40 100644 --- a/src/components/ItemForm.js +++ b/src/components/ItemForm.js @@ -1,11 +1,29 @@ -import React, { useState } from "react"; +import React, {useState} from "react" -function ItemForm() { - const [name, setName] = useState(""); - const [category, setCategory] = useState("Produce"); +function ItemForm({onAddItem}) { + const [name, setName] = useState("") + const [category, setCategory] = useState("Produce") + + const handleSubmit = (e) => { + e.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((r) => r.json(itemData)) + .then((newItem) => onAddItem(newItem)) + } return ( -
    +