-
Notifications
You must be signed in to change notification settings - Fork 7k
Create Shopping Cart #20
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
yuriyivanenko
wants to merge
2
commits into
learn-co-curriculum:master
Choose a base branch
from
yuriyivanenko:feature_branch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
import React from "react"; | ||
import ShoppingList from "./ShoppingList"; | ||
import itemData from "../data/items"; | ||
import React, { useState } from "react" | ||
import ShoppingList from "./ShoppingList" | ||
import itemData from "../data/items" | ||
import Header from "./Header" | ||
|
||
function App() { | ||
|
||
// replace 'false' with a state variable that can be toggled between true and false | ||
// this will be used for the Dark Mode Toggle feature | ||
const appClass = false ? "App dark" : "App light" | ||
const [darkMode, setDarkMode] = useState(false) | ||
const appClass = darkMode ? "App dark" : "App light" | ||
const onDarkModeClick = () => setDarkMode(!darkMode) | ||
|
||
return ( | ||
<div className={appClass}> | ||
<header> | ||
<h2>Shopster</h2> | ||
<button>Dark Mode</button> | ||
</header> | ||
<Header onDarkModeClick={onDarkModeClick} darkMode={darkMode}/> | ||
<ShoppingList items={itemData} /> | ||
</div> | ||
); | ||
} | ||
|
||
export default App; | ||
export default App |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from "react"; | ||
|
||
function Filter({ filterCart }) { | ||
return ( | ||
<div className="Filter"> | ||
<select name="filter" onChange={filterCart}> | ||
<option value="All">Filter by category</option> | ||
<option value="Produce">Produce</option> | ||
<option value="Dairy">Dairy</option> | ||
<option value="Dessert">Dessert</option> | ||
</select> | ||
</div> | ||
) | ||
} | ||
|
||
export default Filter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from "react"; | ||
|
||
function Header({ onDarkModeClick, darkMode }) { | ||
return ( | ||
<header> | ||
<h2>Shopster</h2> | ||
<button onClick={onDarkModeClick}>{darkMode === true ? "Dark Mode" : "Light Mode"}</button> | ||
</header> | ||
); | ||
} | ||
|
||
export default Header; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import React from "react"; | ||
|
||
function Item({ name, category }) { | ||
function Item({ name, category, addItemFun, cartClass = "" }) { | ||
return ( | ||
<li className=""> | ||
<li className={cartClass}> | ||
<span>{name}</span> | ||
<span className="category">{category}</span> | ||
<button className="add">Add to Cart</button> | ||
<button className="add" onClick={addItemFun}>{cartClass === "" ? "Add to Cart" : "Remove From Cart"}</button> | ||
</li> | ||
); | ||
) | ||
} | ||
|
||
export default Item; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,24 +1,72 @@ | ||||
import React from "react"; | ||||
import React, { useState } from "react"; | ||||
import Item from "./Item"; | ||||
import Filter from "./Filter"; | ||||
|
||||
function ShoppingList({ items }) { | ||||
const [cart, setCart] = useState(items) | ||||
|
||||
const updateCart = (e) => { | ||||
const action = e.target.innerHTML | ||||
console.log('Func ran', e.target.innerHTML) | ||||
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
|
||||
if(action === 'Add to Cart'){ | ||||
addToCart(e) | ||||
} else { | ||||
removeFromCart(e) | ||||
} | ||||
} | ||||
|
||||
const addToCart = (e) => { | ||||
const itemName = e.target.parentElement.childNodes[0].textContent | ||||
const updatedCart = cart.map(item => { | ||||
if(item.name === itemName){ | ||||
return { | ||||
...item, | ||||
cartClass: "in-cart" | ||||
} | ||||
} else { | ||||
return item | ||||
} | ||||
}) | ||||
setCart(updatedCart) | ||||
} | ||||
|
||||
const removeFromCart = (e) => { | ||||
const itemName = e.target.parentElement.childNodes[0].textContent | ||||
const updatedCart = cart.map(item => { | ||||
if(item.name === itemName){ | ||||
return { | ||||
...item, | ||||
cartClass: "" | ||||
} | ||||
} else { | ||||
return item | ||||
} | ||||
}) | ||||
setCart(updatedCart) | ||||
} | ||||
|
||||
const handleFilter = (e) => { | ||||
const category = e.target.value | ||||
const filteredItems = items.filter(item => item.category === category) | ||||
setCart(filteredItems) | ||||
} | ||||
|
||||
return ( | ||||
<div className="ShoppingList"> | ||||
<div className="Filter"> | ||||
<select name="filter"> | ||||
<option value="All">Filter by category</option> | ||||
<option value="Produce">Produce</option> | ||||
<option value="Dairy">Dairy</option> | ||||
<option value="Dessert">Dessert</option> | ||||
</select> | ||||
</div> | ||||
<Filter filterCart={handleFilter}/> | ||||
<ul className="Items"> | ||||
{items.map((item) => ( | ||||
<Item key={item.id} name={item.name} category={item.category} /> | ||||
{cart.map((item) => ( | ||||
<Item | ||||
cartClass={item.cartClass} | ||||
key={item.id} | ||||
name={item.name} | ||||
category={item.category} | ||||
addItemFun={updateCart} | ||||
/> | ||||
))} | ||||
</ul> | ||||
</div> | ||||
); | ||||
) | ||||
} | ||||
|
||||
export default ShoppingList; | ||||
export default ShoppingList |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.