-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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?
Conversation
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.
I know it's just a code along, but I'd still like to see the code be "production ready" when you submit it. This time I won't, but going forward, I'm going to make you go back and cleanup your syntax. I really need you to start paying more attention to it. Otherwise, the concepts are there and you're going great, so keep that up!
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
// add fetch request |
} | ||
|
||
|
||
function handleDeleteClick() { |
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.
} | |
function handleDeleteClick() { | |
} | |
function handleDeleteClick() { |
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 comment
The reason will be displayed to describe this comment to others. Learn more.
<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> |
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.
<button onClick={handleDeleteClick}className="remove">Delete</button> | |
<button onClick={handleDeleteClick} className="remove">Delete</button> |
function ItemForm({ onAddItem }) { | ||
const [name, setName] = useState(""); | ||
const [category, setCategory] = useState("Produce"); | ||
|
||
|
||
|
||
function handleSubmit(e) { |
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.
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) { |
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)); | ||
} |
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.
This whole function needs to be indented.
<Item key={item.id} item={item} | ||
onUpdateItem={handleUpdateItem} | ||
onDeleteItem={handleDeleteItem}/> | ||
|
||
))} |
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.
<Item key={item.id} item={item} | |
onUpdateItem={handleUpdateItem} | |
onDeleteItem={handleDeleteItem}/> | |
))} | |
<Item key={item.id} item={item} | |
onUpdateItem={handleUpdateItem} | |
onDeleteItem={handleDeleteItem}/> | |
))} |
Used fetch requests and state to add, delete, and update things on both the database and the DOM.