From 389419889bea0c4f8d9f639b7a635bd5850e5f48 Mon Sep 17 00:00:00 2001 From: Yuriy Ivanenko Date: Mon, 17 Jun 2024 10:44:14 -0400 Subject: [PATCH] Pass All Tests --- db.json | 37 ++++++++++++---------------------- src/components/Item.js | 26 +++++++++++++++++++++--- src/components/ItemForm.js | 23 +++++++++++++++++++-- src/components/ShoppingList.js | 31 +++++++++++++++++++++++++--- 4 files changed, 85 insertions(+), 32 deletions(-) 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 (
  • {item.name} {item.category} - - +
  • ); } diff --git a/src/components/ItemForm.js b/src/components/ItemForm.js index 5b91f0b5..bdc39cde 100644 --- a/src/components/ItemForm.js +++ b/src/components/ItemForm.js @@ -1,11 +1,30 @@ import React, { useState } from "react"; -function ItemForm() { +function ItemForm({ onAddItem }) { const [name, setName] = useState(""); const [category, setCategory] = useState("Produce"); + const handleSubmit = (e) => { + e.preventDefault() + const itemData = { + name: name, + category: category, + isInFact: 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 ( -
    +