Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/components/Item.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
import React from "react";

function Item({ item }) {
function Item({ item, onUpdateItem, onDeleteItem }) {

function handleAddToCart() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do this in almost every lab!! PLEASE PLEASE PLEASE PLEASE PLEASE try to get out of this habit. I'm going to make you go back and delete these lines until you you stop... sorry!

Suggested change
function Item({ item, onUpdateItem, onDeleteItem }) {
function handleAddToCart() {
function Item({ item, onUpdateItem, onDeleteItem }) {
function handleAddToCart() {

fetch(`http://localhost:4000/items/${item.id}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({isInCart: !item.isInCart
}),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
body: JSON.stringify({isInCart: !item.isInCart
}),
body: JSON.stringify({ isInCart: !item.isInCart }),

})
.then((response) => response.json())
.then((updatedItemData) => onUpdateItem(updatedItemData))
}

function handleDeleteClick() {
fetch(`http://localhost:4000/items/${item.id}`, {
method: "DELETE",
})
.then((r) => r.json())
.then(() => onDeleteItem(item));
}

return (
<li className={item.isInCart ? "in-cart" : ""}>
<span>{item.name}</span>
<span className="category">{item.category}</span>
<button className={item.isInCart ? "remove" : "add"}>
<button className={item.isInCart ? "remove" : "add"} onClick={handleAddToCart}>
{item.isInCart ? "Remove From" : "Add to"} Cart
</button>
<button className="remove">Delete</button>
<button className="remove" onClick={handleDeleteClick}>Delete</button>
</li>
);
}
Expand Down
22 changes: 20 additions & 2 deletions src/components/ItemForm.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import React, { useState } from "react";

function ItemForm() {
function ItemForm({ onAddItem }) {
const [name, setName] = useState("");
Comment on lines +3 to 4

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See, this is what I'm looking for!

const [category, setCategory] = useState("Produce");

function 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((response) => response.json())
.then((newItemData) => onAddItem(newItemData))
}

return (
<form className="NewItem">
<form className="NewItem" onSubmit={handleSubmit}>
<label>
Name:
<input
Expand Down
39 changes: 36 additions & 3 deletions src/components/ShoppingList.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useEffect, useState } from "react";
import ItemForm from "./ItemForm";
import Filter from "./Filter";
import Item from "./Item";
Expand All @@ -7,6 +7,34 @@ function ShoppingList() {
const [selectedCategory, setSelectedCategory] = useState("All");
const [items, setItems] = useState([]);

useEffect(() => {
fetch("http://localhost:4000/items")
.then((response) => response.json())
.then((itemsData) => setItems(itemsData))
}, [])

function handleDeleteItem(deletedItem) {
const updatedItems = items.filter((item) => item.id !== deletedItem.id)
setItems(updatedItems)
}

function handleUpdateItem(updatedItemData) {
const updatedItems = items.map((item) => {
if (item.id === updatedItemData.id) {
return updatedItemData
} else {
return item
}
})
setItems(updatedItems)
}

function handleAddItems(newItem) {
console.log("in Shopping", newItem)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log("in Shopping", newItem)

setItems([...items, newItem])
}


function handleCategoryChange(category) {
setSelectedCategory(category);
}
Expand All @@ -19,14 +47,19 @@ function ShoppingList() {

return (
<div className="ShoppingList">
<ItemForm />
<ItemForm onAddItem={handleAddItems} />
<Filter
category={selectedCategory}
onCategoryChange={handleCategoryChange}
/>
<ul className="Items">
{itemsToDisplay.map((item) => (
<Item key={item.id} item={item} />
<Item
key={item.id}
item={item}
onUpdateItem={handleUpdateItem}
onDeleteItem={handleDeleteItem}
/>
))}
</ul>
</div>
Expand Down