-
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
Completed Hogwild #17
base: master
Are you sure you want to change the base?
Changes from all commits
e5992a8
6e21b75
a912e50
529a210
87a1d76
1132dac
8b54625
8802446
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,27 @@ | ||
import React from "react"; | ||
import React, { useState } from "react"; | ||
|
||
import Nav from "./Nav"; | ||
import PigList from "./PigList"; | ||
import Filter from "./Filter"; | ||
import Sort from "./Sort"; | ||
import HideHogs from "./HideHogs"; | ||
|
||
import hogs from "../porkers_data"; | ||
import PigForm from "./PigForm"; | ||
|
||
function App() { | ||
const [allHogs, setAllHogs] = useState(hogs) | ||
|
||
return ( | ||
<div className="App"> | ||
<Nav /> | ||
<PigForm hogs={hogs} allHogs={allHogs} setAllHogs={setAllHogs}/> | ||
<Filter hogs={hogs} allHogs={allHogs} setAllHogs={setAllHogs} /> | ||
<Sort allHogs={allHogs} setAllHogs={setAllHogs} hogs={hogs}/> | ||
<HideHogs allHogs={allHogs} setAllHogs={setAllHogs} hogs={hogs}/> | ||
<PigList className='ui grid container' hogs={hogs} allHogs={allHogs}/> | ||
</div> | ||
); | ||
) | ||
} | ||
|
||
export default App; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,27 @@ | ||||||||||||||||||
import React, { useState } from 'react' | ||||||||||||||||||
|
||||||||||||||||||
function Filter({ allHogs, setAllHogs, hogs }) { | ||||||||||||||||||
const [buttonText, setButtonText] = useState('Greased Hogs: ON') | ||||||||||||||||||
|
||||||||||||||||||
const handleClick = () => { | ||||||||||||||||||
const greasedHogs = allHogs.filter((hog) => hog.greased === true) | ||||||||||||||||||
|
||||||||||||||||||
if(buttonText === 'Greased Hogs: ON'){ | ||||||||||||||||||
setAllHogs(greasedHogs) | ||||||||||||||||||
setButtonText('Greased Hogs: OFF') | ||||||||||||||||||
} else if(buttonText === 'Greased Hogs: OFF') { | ||||||||||||||||||
setAllHogs(hogs) | ||||||||||||||||||
setButtonText('Greased Hogs: ON') | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
return ( | ||||||||||||||||||
<> | ||||||||||||||||||
<button onClick={handleClick}> | ||||||||||||||||||
{buttonText} | ||||||||||||||||||
</button> | ||||||||||||||||||
</> | ||||||||||||||||||
Comment on lines
+19
to
+23
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
|
||||||||||||||||||
) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
export default Filter |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React, { useState } from 'react' | ||
|
||
function HideHogs({ setAllHogs, hogs }) { | ||
const [buttonText, setButtonText] = useState('HIDE HOGS') | ||
|
||
const handleClick = () => { | ||
if(buttonText === 'HIDE HOGS'){ | ||
setAllHogs([]) | ||
setButtonText('SHOW HOGS') | ||
} else if(buttonText === 'SHOW HOGS') { | ||
setAllHogs(hogs) | ||
setButtonText('HIDE HOGS') | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<button onClick={handleClick}> | ||
{buttonText} | ||
</button> | ||
</> | ||
Comment on lines
+17
to
+21
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. Same thing here. |
||
) | ||
} | ||
|
||
export default HideHogs |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import React, { useState } from 'react' | ||
|
||
function PigForm({ setAllHogs, hogs }) { | ||
const [newHogName, setNewHogName] = useState('') | ||
const [newHogSpecialty, setNewHogSpecialty] = useState('') | ||
const [newHogWeight, setNewHogWeight] = useState('') | ||
const [newHogGreased, setNewHogGreased] = useState('') | ||
const [newHogMedal, setNewHogMedal] = useState('') | ||
const [newHogImg, setNewHogImg] = useState('') | ||
|
||
function handleHogNameChange(event) { | ||
setNewHogName(event.target.value) | ||
} | ||
|
||
function handleHogSpecialtyChange(event) { | ||
setNewHogSpecialty(event.target.value) | ||
} | ||
|
||
function handleHogWeightChange(event) { | ||
setNewHogWeight(event.target.value) | ||
} | ||
|
||
function handleHogGreasedChange(event) { | ||
setNewHogGreased(event.target.value) | ||
} | ||
|
||
function handleHogsMedalChange(event) { | ||
setNewHogMedal(event.target.value) | ||
} | ||
|
||
function handleHogsImgChange(event) { | ||
setNewHogImg(event.target.value) | ||
} | ||
Comment on lines
+4
to
+33
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. If you review the form abstraction lesson you can find a much simpler way to do this. |
||
|
||
function handleSubmit(event) { | ||
event.preventDefault() | ||
|
||
const addHog = { | ||
name: newHogName, | ||
specialty: newHogSpecialty, | ||
greased: newHogGreased, | ||
weight: newHogWeight, | ||
"highest medal achieved": newHogMedal, | ||
image: newHogImg | ||
} | ||
|
||
const newHogList = [...hogs, addHog] | ||
setAllHogs(newHogList) | ||
} | ||
|
||
return ( | ||
<ul> | ||
<form onSubmit={handleSubmit}> | ||
<li> | ||
<label> | ||
Name | ||
<input | ||
type="text" | ||
name="text" | ||
value={newHogName} | ||
onChange={handleHogNameChange} | ||
/> | ||
</label> | ||
<label> | ||
Specialty | ||
<input | ||
type="text" | ||
name="text" | ||
value={newHogSpecialty} | ||
onChange={handleHogSpecialtyChange} | ||
/> | ||
</label> | ||
<label> | ||
Weight | ||
<input | ||
placeholder='Please enter a number' | ||
type="text" | ||
name="text" | ||
value={newHogWeight} | ||
onChange={handleHogWeightChange} | ||
/> | ||
</label> | ||
</li> | ||
<li> | ||
<label> | ||
Pigture | ||
<input | ||
placeholder='Please enter an img url' | ||
type="text" | ||
name="text" | ||
value={newHogImg} | ||
onChange={handleHogsImgChange} | ||
/> | ||
</label> | ||
</li> | ||
<li> | ||
<label> | ||
Greased? | ||
<select | ||
name="category" | ||
value={newHogGreased} | ||
onChange={handleHogGreasedChange} | ||
> | ||
<option value=""disabled selected>Select</option> | ||
<option value="Yes">Yes</option> | ||
<option value="No">No</option> | ||
</select> | ||
</label> | ||
</li> | ||
<label> | ||
Highest Medal Achieved | ||
<select | ||
name="category" | ||
value={newHogMedal} | ||
onChange={handleHogsMedalChange} | ||
> | ||
<option value=""disabled selected>Select Medal</option> | ||
<option value="Gold">Gold</option> | ||
<option value="Silver">Silver</option> | ||
<option value="Bronze">Bronze</option> | ||
</select> | ||
</label> | ||
<li> | ||
<input type="submit" value="Add Hog" /> | ||
</li> | ||
</form> | ||
</ul> | ||
) | ||
} | ||
|
||
export default PigForm |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react' | ||
import Tile from './Tile' | ||
|
||
function PigList({ allHogs }) { | ||
const createHogList = allHogs.map((hog) => { | ||
return ( | ||
<Tile | ||
className='pigTile ui eight wide column' | ||
key={hog.name} | ||
name={hog.name} | ||
img={hog.image} | ||
specialty={hog.specialty} | ||
weight={hog.weight} | ||
greased={hog.greased} | ||
medal={hog['highest medal achieved']} | ||
/> | ||
) | ||
}) | ||
|
||
return ( | ||
<div className='ui grid container'> | ||
{createHogList} | ||
</div> | ||
) | ||
} | ||
|
||
export default PigList |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { useState } from 'react' | ||
|
||
function Sort({ allHogs, setAllHogs, hogs }) { | ||
const [sortButtonName, setSortButtonName] = useState('Sort By Name: ON') | ||
const [sortButtonWeight, setSortButtonWeight] = useState('Sort By Weight: ON') | ||
|
||
const sortedHogs = allHogs.toSorted((a, b) => { | ||
const hogA = a.name | ||
const hogB = b.name | ||
if (hogA < hogB) { | ||
return -1 | ||
} | ||
if (hogA > hogB) { | ||
return 1 | ||
} | ||
return 0 | ||
}) | ||
|
||
const handleNameClick = () => { | ||
if(sortButtonName === 'Sort By Name: ON'){ | ||
setAllHogs(sortedHogs) | ||
setSortButtonName('Sort By Name: OFF') | ||
} else if(sortButtonName === 'Sort By Name: OFF') { | ||
setAllHogs(hogs) | ||
setSortButtonName('Sort By Name: ON') | ||
} | ||
} | ||
|
||
const sortedHogsWeight = allHogs.toSorted((a, b) => { | ||
const hogA = a.weight | ||
const hogB = b.weight | ||
if (hogA < hogB) { | ||
return -1 | ||
} | ||
if (hogA > hogB) { | ||
return 1 | ||
} | ||
return 0 | ||
}) | ||
|
||
const handleWeightClick = () => { | ||
if(sortButtonWeight === 'Sort By Weight: ON'){ | ||
setAllHogs(sortedHogsWeight) | ||
setSortButtonWeight('Sort By Weight: OFF') | ||
} else if(sortButtonWeight === 'Sort By Weight: OFF') { | ||
setAllHogs(hogs) | ||
setSortButtonWeight('Sort By Weight: ON') | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<button onClick={handleNameClick}> | ||
{sortButtonName} | ||
</button> | ||
<button onClick={handleWeightClick}> | ||
{sortButtonWeight} | ||
</button> | ||
</> | ||
Comment on lines
+52
to
+59
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. When you use a fragment, you should still indent the content by 2 spaces. |
||
) | ||
} | ||
|
||
export default Sort |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React, { useState } from 'react' | ||
|
||
function Tile({ name, img, specialty, weight, greased, medal, className }) { | ||
const [selectedHog, setSelectedHog] = useState(false) | ||
|
||
function handleClick() { | ||
setSelectedHog(!selectedHog) | ||
} | ||
|
||
return ( | ||
<div className={className} onClick={handleClick}> | ||
<h3>{name}</h3> | ||
<img src={img} alt={name} /> | ||
{selectedHog &&( | ||
<div> | ||
<p>Specialty: {specialty}</p> | ||
<p>Weight: {weight}</p> | ||
<p>Greased: {greased ? 'Yes' : 'No'}</p> | ||
<p>Highest Medal Achieved: {medal}</p> | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default Tile |
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.
Woah what's going on with the spacing here?!
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.
Thats a great question. I'll go back and clean this up.