Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import React from "react";
import React, {useState} from "react";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import React, {useState} 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spacing got a little crazy here!

Expand Down
35 changes: 35 additions & 0 deletions src/components/Hog.js
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const Hog = ({hog, setSelectedHog, selectedHog}) => {
const handleClick = (e) => {
const Hog = ({hog, setSelectedHog, selectedHog}) => {
const handleClick = (e) => {

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

Choose a reason for hiding this comment

The 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;
34 changes: 34 additions & 0 deletions src/components/Menu.js
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}) => {

Choose a reason for hiding this comment

The 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;
23 changes: 22 additions & 1 deletion src/components/Nav.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import React from "react";
import piggy from "../assets/porco.png";

const Nav = () => {
const Nav = ({setGreaseFilter, setSortByWeight}) => {

const handleClickSort = (e) => {
e.preventDefault()
const sortByWeight = e.target.classList.contains('On')
sortByWeight ? e.target.classList.remove('On') : e.target.classList.add('On')
sortByWeight ? e.target.textContent = 'Sort Hogs By Name' : e.target.textContent = 'Sort Hogs By Weight'
setSortByWeight(sortByWeight ? true : false)
}

const handleClickFilter = (e) => {
e.preventDefault()
const toggleFilter = e.target.classList.contains('On')
toggleFilter ? e.target.classList.remove('On') : e.target.classList.add('On')
toggleFilter ? e.target.textContent = 'Click for the full list of hogs' : e.target.textContent = 'Click if you love greasy hogs'
setGreaseFilter(toggleFilter ? true : false)
}

return (
<div className="navWrapper">
<span className="headerText">HogWild</span>
Expand All @@ -11,6 +28,10 @@ const Nav = () => {
<span className="normalText">
A React App for County Fair Hog Fans
</span>
<div>
<button onClick={handleClickFilter}>Click if you love greasy hogs</button>
<button onClick={handleClickSort}>Sort Hogs By Weight</button>
</div>
</div>
);
};
Expand Down
9 changes: 8 additions & 1 deletion src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,11 @@ ul {
to {
transform: rotate(360deg);
}
}
}

.pigImg {
width: 310px;
height: 300px;
object-fit: cover;
aspect-ratio: 1.5;
}