diff --git a/src/components/App.js b/src/components/App.js index 344d48d4..19311eb4 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -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 (
- ); + ) } export default App; diff --git a/src/components/Filter.js b/src/components/Filter.js new file mode 100644 index 00000000..253adbf1 --- /dev/null +++ b/src/components/Filter.js @@ -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 ( + <> + + + ) +} + +export default Filter diff --git a/src/components/HideHogs.js b/src/components/HideHogs.js new file mode 100644 index 00000000..6d28552c --- /dev/null +++ b/src/components/HideHogs.js @@ -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 ( + <> + + + ) +} + +export default HideHogs diff --git a/src/components/PigForm.js b/src/components/PigForm.js new file mode 100644 index 00000000..1392e231 --- /dev/null +++ b/src/components/PigForm.js @@ -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) + } + + 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 ( + + ) +} + +export default PigForm diff --git a/src/components/PigList.js b/src/components/PigList.js new file mode 100644 index 00000000..1ca0b83d --- /dev/null +++ b/src/components/PigList.js @@ -0,0 +1,27 @@ +import React from 'react' +import Tile from './Tile' + +function PigList({ allHogs }) { + const createHogList = allHogs.map((hog) => { + return ( + + ) + }) + + return ( +
+ {createHogList} +
+ ) +} + +export default PigList diff --git a/src/components/Sort.js b/src/components/Sort.js new file mode 100644 index 00000000..d83f05b6 --- /dev/null +++ b/src/components/Sort.js @@ -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 ( + <> + + + + ) +} + +export default Sort diff --git a/src/components/Tile.js b/src/components/Tile.js new file mode 100644 index 00000000..63d700bc --- /dev/null +++ b/src/components/Tile.js @@ -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 ( +
+

{name}

+ {name} + {selectedHog &&( +
+

Specialty: {specialty}

+

Weight: {weight}

+

Greased: {greased ? 'Yes' : 'No'}

+

Highest Medal Achieved: {medal}

+
+ )} +
+ ) +} + +export default Tile diff --git a/src/index.css b/src/index.css index a89ad3a1..66098174 100644 --- a/src/index.css +++ b/src/index.css @@ -112,6 +112,11 @@ ul { color: #FBB; } +.pigTile img { + width: inherit; + height: inherit; +} + .minPigTile { width: 250px; height: 250px; @@ -140,7 +145,6 @@ ul { clear: both; } - @keyframes App-logo-spin { from { transform: rotate(0deg); @@ -149,4 +153,4 @@ ul { to { transform: rotate(360deg); } -} \ No newline at end of file +}