Skip to content

Hooks Fetch Crud Code Along #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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: 32 additions & 4 deletions src/components/Item.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
import React from "react";

function Item({ item }) {
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((response) => response.json())
.then((updatedItem) => onUpdateItem(updatedItem))

}

function handleDeleteClick(){
fetch(`http://localhost:4000/items/${item.id}`, {
method: "DELETE"
})
.then((response) => response.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={handleAddToCartClick}
>
{item.isInCart ? "Remove From" : "Add to"} Cart
</button>
<button className="remove">Delete</button>
<button className="remove" onClick={handleDeleteClick}>Delete</button>
</li>
);
}

export default Item;
export default Item;
25 changes: 22 additions & 3 deletions src/components/ItemForm.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import React, { useState } from "react";

function ItemForm() {
function ItemForm({ onAddItem }) {
const [name, setName] = useState("");
const [category, setCategory] = useState("Produce");
const [category, setCategory] = useState("");

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

return (
<form className="NewItem">
<form className="NewItem" onSubmit={handleSubmit}>
<label>
Name:
<input
Expand Down
42 changes: 37 additions & 5 deletions src/components/ShoppingList.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,64 @@
import React, { useState } from "react";
import React, { useEffect, useState } from "react";
import Item from "./Item";
import ItemForm from "./ItemForm";
import Filter from "./Filter";
import Item from "./Item";

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))
.catch((error) => {
console.error("Error fetching items:", error);
});
}, []);
function handleAddItem(newItem) {
setItems([...items, newItem]);
}

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

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

function handleCategoryChange(category) {
setSelectedCategory(category);
}

const itemsToDisplay = items.filter((item) => {
if (selectedCategory === "All") return true;

return item.category === selectedCategory;
});

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}
/>
))}
</ul>
</div>
Expand Down