diff --git a/db.json b/db.json index 285c67c0..0f1f4d0f 100644 --- a/db.json +++ b/db.json @@ -35,6 +35,42 @@ "name": "Cookies", "category": "Dessert", "isInCart": false + }, + { + "name": "", + "category": "Produce", + "isInCart": false, + "id": 7 + }, + { + "name": "", + "category": "Produce", + "isInCart": false, + "id": 8 + }, + { + "name": "fdesgfserf", + "category": "Dessert", + "isInCart": false, + "id": 9 + }, + { + "name": "fdesgfserf", + "category": "Dessert", + "isInCart": false, + "id": 10 + }, + { + "name": "dwad", + "category": "Produce", + "isInCart": false, + "id": 11 + }, + { + "name": "dweadw", + "category": "Produce", + "isInCart": false, + "id": 12 } ] } \ No newline at end of file diff --git a/src/components/Item.js b/src/components/Item.js index 2f40e63f..c4cccfdb 100644 --- a/src/components/Item.js +++ b/src/components/Item.js @@ -1,14 +1,38 @@ import React from "react"; -function Item({ item }) { +function Item({ item, onUpdateItem, onDeleteItem }) { + function handleAddToCartClick() { + // add fetch request + 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((r) => onDeleteItem(item)); + } + return (
  • {item.name} {item.category} - - +
  • ); } diff --git a/src/components/ItemForm.js b/src/components/ItemForm.js index 5b91f0b5..a98b8f9e 100644 --- a/src/components/ItemForm.js +++ b/src/components/ItemForm.js @@ -1,11 +1,31 @@ import React, { useState } from "react"; -function ItemForm() { +function ItemForm({ onAddItem }) { const [name, setName] = useState(""); const [category, setCategory] = useState("Produce"); + + +function 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()) + .then((newItem) => onAddItem(newItem)); +} + return ( -
    +