-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fetch to Add, Delete, and Update #10
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| 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"}> | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| {item.isInCart ? "Remove From" : "Add to"} Cart | ||||||||||||||||
| </button> | ||||||||||||||||
| <button className="remove">Delete</button> | ||||||||||||||||
| <button onClick={handleDeleteClick}className="remove">Delete</button> | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| </li> | ||||||||||||||||
| ); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| 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"; | ||||||||||||||||||||
|
|
@@ -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); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
| </ul> | ||||||||||||||||||||
| </div> | ||||||||||||||||||||
|
|
||||||||||||||||||||
There was a problem hiding this comment.
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.