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

Completed Hogwild #17

Open
wants to merge 8 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
17 changes: 15 additions & 2 deletions src/components/App.js
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>
);
)
}
Comment on lines 15 to 25

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?!

Copy link
Author

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.


export default App;
27 changes: 27 additions & 0 deletions src/components/Filter.js
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

Choose a reason for hiding this comment

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

Suggested change
<>
<button onClick={handleClick}>
{buttonText}
</button>
</>
<button onClick={handleClick}>
{buttonText}
</button>

)
}

export default Filter
25 changes: 25 additions & 0 deletions src/components/HideHogs.js
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

Choose a reason for hiding this comment

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

Same thing here.

)
}

export default HideHogs
131 changes: 131 additions & 0 deletions src/components/PigForm.js
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

Choose a reason for hiding this comment

The 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
27 changes: 27 additions & 0 deletions src/components/PigList.js
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
63 changes: 63 additions & 0 deletions src/components/Sort.js
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

Choose a reason for hiding this comment

The 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
26 changes: 26 additions & 0 deletions src/components/Tile.js
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
8 changes: 6 additions & 2 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ ul {
color: #FBB;
}

.pigTile img {
width: inherit;
height: inherit;
}

.minPigTile {
width: 250px;
height: 250px;
Expand Down Expand Up @@ -140,7 +145,6 @@ ul {
clear: both;
}


@keyframes App-logo-spin {
from {
transform: rotate(0deg);
Expand All @@ -149,4 +153,4 @@ ul {
to {
transform: rotate(360deg);
}
}
}