-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Code Along For Crud #12
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 1 commit
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,36 @@ | ||||||||
| import React from "react"; | ||||||||
|
|
||||||||
| function Item({ item }) { | ||||||||
| 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 | ||||||||
| }), | ||||||||
|
||||||||
| body: JSON.stringify({isInCart: !item.isInCart | |
| }), | |
| body: JSON.stringify({ isInCart: !item.isInCart }), |
| 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
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. 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 | ||
|
|
||
| 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,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) | ||||
|
||||
| console.log("in Shopping", 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.
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!