Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions db.json

Choose a reason for hiding this comment

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

Be mindful of committing changes like this.

Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
30 changes: 27 additions & 3 deletions src/components/Item.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import React from "react";

function Item({ item }) {
function Item({ item, onUpdateItem, onDeleteItem }) {
function handleAddToCartClick() {
// add fetch request

Choose a reason for hiding this comment

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

Suggested change
// 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() {
Comment on lines +17 to +20

Choose a reason for hiding this comment

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

Suggested change
}
function handleDeleteClick() {
}
function handleDeleteClick() {

fetch(`http://localhost:4000/items/${item.id}`, {
method: "DELETE",
})
.then((r) => r.json())
.then((r) => 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 onClick={handleAddToCartClick}className={item.isInCart ? "remove" : "add"}>

Choose a reason for hiding this comment

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

Suggested change
<button onClick={handleAddToCartClick}className={item.isInCart ? "remove" : "add"}>
<button onClick={handleAddToCartClick} className={item.isInCart ? "remove" : "add"}>

{item.isInCart ? "Remove From" : "Add to"} Cart
</button>
<button className="remove">Delete</button>
<button onClick={handleDeleteClick}className="remove">Delete</button>

Choose a reason for hiding this comment

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

Suggested change
<button onClick={handleDeleteClick}className="remove">Delete</button>
<button onClick={handleDeleteClick} className="remove">Delete</button>

</li>
);
}
Expand Down
24 changes: 22 additions & 2 deletions src/components/ItemForm.js
Original file line number Diff line number Diff line change
@@ -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) {
Comment on lines +3 to +9

Choose a reason for hiding this comment

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

Suggested change
function ItemForm({ onAddItem }) {
const [name, setName] = useState("");
const [category, setCategory] = useState("Produce");
function handleSubmit(e) {
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));
}
Comment on lines +9 to +25

Choose a reason for hiding this comment

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

This whole function needs to be indented.


return (
<form className="NewItem">
<form className="NewItem" onSubmit={handleSubmit}>
<label>
Name:
<input
Expand Down
35 changes: 32 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,32 @@ function ShoppingList() {
const [selectedCategory, setSelectedCategory] = useState("All");
const [items, setItems] = useState([]);

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

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

function handleAddItem(newItem) {
setItems([...items, newItem]);
}

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

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

return (
<div className="ShoppingList">
<ItemForm />
<ItemForm onAddItem={handleAddItem}/>
<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}/>

))}
Comment on lines +55 to 59

Choose a reason for hiding this comment

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

Suggested change
<Item key={item.id} item={item}
onUpdateItem={handleUpdateItem}
onDeleteItem={handleDeleteItem}/>
))}
<Item key={item.id} item={item}
onUpdateItem={handleUpdateItem}
onDeleteItem={handleDeleteItem}/>
))}

</ul>
</div>
Expand Down