-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Display Hogs, Sort Hogs, Filter Hogs #16
base: master
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,12 +1,17 @@ | ||
import React from "react"; | ||
import React, {useState} from "react"; | ||
import Nav from "./Nav"; | ||
import Menu from './Menu'; | ||
|
||
import hogs from "../porkers_data"; | ||
|
||
function App() { | ||
const [greaseFilter, setGreaseFilter] = useState('') | ||
const [sortByWeight, setSortByWeight] = useState('') | ||
|
||
return ( | ||
<div className="App"> | ||
<Nav /> | ||
<Nav setGreaseFilter={setGreaseFilter} setSortByWeight={setSortByWeight}/> | ||
<Menu hogs={hogs} greaseFilter={greaseFilter} sortByWeight={sortByWeight}/> | ||
</div> | ||
); | ||
} | ||
Comment on lines
7
to
17
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. Spacing got a little crazy here! |
||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||||||||
import React from "react"; | ||||||||||||
|
||||||||||||
const Hog = ({hog, setSelectedHog, selectedHog}) => { | ||||||||||||
|
||||||||||||
const handleClick = (e) => { | ||||||||||||
Comment on lines
+3
to
+5
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() | ||||||||||||
setSelectedHog(selectedHog===hog ? '' : hog) | ||||||||||||
} | ||||||||||||
|
||||||||||||
if(hog!==selectedHog){ | ||||||||||||
return ( | ||||||||||||
<div className='pigTile'> | ||||||||||||
<div id={hog.name} className='ui eight wide column' onClick={handleClick}> | ||||||||||||
<h3>{hog.name}</h3> | ||||||||||||
<img className='pigImg' src={hog.image}/> | ||||||||||||
</div> | ||||||||||||
</div> | ||||||||||||
)} else { | ||||||||||||
return( | ||||||||||||
Comment on lines
+10
to
+19
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. What in the world happened here?! |
||||||||||||
<div className='pigTile'> | ||||||||||||
<div id={hog.name} className='ui eight wide column' onClick={handleClick}> | ||||||||||||
<h3>{hog.name}</h3> | ||||||||||||
<div> | ||||||||||||
<p>Greased: {hog.greased ? 'Yes' : 'No'}</p> | ||||||||||||
<p>Specialty: {hog.specialty}</p> | ||||||||||||
<p>Weight: {hog.weight}</p> | ||||||||||||
</div> | ||||||||||||
<img className='pigImg' src={hog.image}/> | ||||||||||||
</div> | ||||||||||||
</div> | ||||||||||||
) | ||||||||||||
} | ||||||||||||
}; | ||||||||||||
|
||||||||||||
export default Hog; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, {useState} from "react"; | ||
import Hog from './Hog'; | ||
|
||
const Menu = ({ hogs, greaseFilter, sortByWeight}) => { | ||
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. What would be a better approach would be to handle the filtering and sorting in the parent component and only pass in the filtered and sorted set of hogs. Less props and better separation of concerns. |
||
const [selectedHog, setSelectedHog] = useState(false) | ||
|
||
const hogList = sortByWeight ? | ||
[...hogs].sort((a, b) => a.weight - b.weight) | ||
: [...hogs].sort((a, b) => a.name.localeCompare(b.name)) | ||
|
||
|
||
const displayHogs = () => | ||
greaseFilter ? | ||
hogList.map(hog => hog.greased ? | ||
<Hog key={hog.name} | ||
hog={hog} | ||
selectedHog={selectedHog} | ||
setSelectedHog={setSelectedHog}/> : | ||
'') : | ||
hogList.map(hog => | ||
<Hog key={hog.name} | ||
hog={hog} | ||
selectedHog={selectedHog} | ||
setSelectedHog={setSelectedHog}/>) | ||
|
||
|
||
return ( | ||
<div className="ui grid container"> | ||
{displayHogs()} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Menu; |
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.