-
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
Add CRU Functionality #15
Open
yuriyivanenko
wants to merge
9
commits into
learn-co-curriculum:master
Choose a base branch
from
yuriyivanenko:feature_branch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d5d0524
Render pigs to the screen in tiles
yuriyivanenko 51ea6ac
Show hog details when tile is clicked
yuriyivanenko 069c5aa
Allow users to filter for greased pigs
yuriyivanenko e269e94
Refactor filter fucntionality
yuriyivanenko 65c89cf
Add sort functionality to app
yuriyivanenko 0168076
Add ability to hide hogs and show them
yuriyivanenko 5254a50
Add form to add new pigs to list
yuriyivanenko aaf424c
Clean up code
yuriyivanenko b1f18cd
Change styling for cards using semantic ui
yuriyivanenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React, { useState } from "react"; | ||
|
||
const AddHogForm = ({ handleNewHog }) => { | ||
|
||
const [formData, setFormData] = useState({ | ||
name: '', | ||
specialty: '', | ||
greased: false, | ||
weight: '', | ||
medal: 'none', | ||
image: '' | ||
}) | ||
|
||
const handleChange = (e) => { | ||
const { name, value, type, checked } = e.target | ||
setFormData({ | ||
...formData, | ||
[name]: type === 'checkbox' ? checked : value | ||
}) | ||
} | ||
|
||
const handleSubmit = (e) => { | ||
e.preventDefault(); | ||
handleNewHog(formData) | ||
} | ||
|
||
return ( | ||
<form className="ui form" style={{maxWidth:'400px'}} onSubmit={handleSubmit}> | ||
<div className="field"> | ||
<label>Name</label> | ||
<input | ||
type="text" | ||
name="name" | ||
value={formData.name} | ||
onChange={handleChange} | ||
/> | ||
</div> | ||
<div className="field"> | ||
<label>Specialty</label> | ||
<input | ||
type="text" | ||
name="specialty" | ||
value={formData.specialty} | ||
onChange={handleChange} | ||
/> | ||
</div> | ||
<div className="field"> | ||
<label>Greased</label> | ||
<input | ||
type="checkbox" | ||
name="greased" | ||
checked={formData.greased} | ||
onChange={handleChange} | ||
/> | ||
</div> | ||
<div className="field"> | ||
<label>Weight</label> | ||
<input | ||
type="number" | ||
name="weight" | ||
value={formData.weight} | ||
onChange={handleChange} | ||
/> | ||
</div> | ||
<div className="field"> | ||
<label>Medal</label> | ||
<select | ||
className="ui dropdown" | ||
name="medal" | ||
value={formData.medal} | ||
onChange={handleChange} | ||
> | ||
<option value="none">None</option> | ||
<option value="wood">Wood</option> | ||
<option value="bronze">Bronze</option> | ||
<option value="silver">Silver</option> | ||
<option value="gold">Gold</option> | ||
<option value="diamond">Diamond</option> | ||
<option value="platinum">Platinum</option> | ||
</select> | ||
</div> | ||
<div className="field"> | ||
<label>Image Source</label> | ||
<input | ||
type="text" | ||
name="image" | ||
value={formData.image} onChange={handleChange} | ||
/> | ||
</div> | ||
<button className="ui button" type="submit">Submit</button> | ||
</form> | ||
); | ||
}; | ||
|
||
export default AddHogForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,23 @@ | ||
import React from "react"; | ||
import Nav from "./Nav"; | ||
import React, { useState } from "react" | ||
|
||
import hogs from "../porkers_data" | ||
|
||
import Nav from "./Nav" | ||
import HogList from "./HogList" | ||
import AddHogForm from "./AddHogForm" | ||
|
||
import hogs from "../porkers_data"; | ||
|
||
function App() { | ||
const [hogsList, setHogsList] = useState(hogs) | ||
const handleNewHog = (newHog) => setHogsList([...hogs, newHog]) | ||
|
||
return ( | ||
<div className="App"> | ||
<Nav /> | ||
<AddHogForm handleNewHog={handleNewHog} /> | ||
<HogList hogs={hogsList} /> | ||
</div> | ||
); | ||
) | ||
} | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React, { useState } from "react"; | ||
import SortDropdown from "./SortDropdown"; | ||
import HogTile from "./HogTile"; | ||
|
||
const HogList = ({ hogs }) => { | ||
const [showGreased, setShowGreased] = useState(false) | ||
const [sortValue, setSortValue] = useState('') | ||
const [nonHiddenList, setNonHiddenList] = useState(hogs) | ||
|
||
const handleFilterGreased = () => setShowGreased(showGreased => !showGreased) | ||
const handleSort = (sortValue) => setSortValue(sortValue) | ||
|
||
const handleHideHog = (hogToHide) => { | ||
setNonHiddenList(nonHiddenList.filter(hog => hog.name !== hogToHide)) | ||
} | ||
|
||
const handleShowAllHogs = () => setNonHiddenList(hogs) | ||
|
||
const sortedList = [...nonHiddenList].sort((a, b) => { | ||
if(sortValue === 'name'){ | ||
return a.name.localeCompare(b.name) | ||
} else if(sortValue === 'weight') { | ||
return a.weight - b.weight | ||
} | ||
}) | ||
|
||
const filteredList = showGreased ? sortedList.filter(hog => hog.greased) : sortedList | ||
|
||
return ( | ||
<> | ||
<div> | ||
<button className="ui button" | ||
style={showGreased ? {backgroundColor: 'lightblue'} : null} | ||
onClick={handleFilterGreased}> | ||
Filter Greased Hogs | ||
</button> | ||
<button className="ui button" | ||
style={{marginLeft: '20px', marginBottom: '20px'}} | ||
onClick={handleShowAllHogs}> | ||
Show All Hogs | ||
</button> | ||
</div> | ||
<SortDropdown handleSort={handleSort} /> | ||
<div className="ui grid container"> | ||
<div className="ui eight wide column"> | ||
{filteredList.map(hog => { | ||
return <HogTile key={hog.name} hog={hog} handleHideHog={handleHideHog} /> | ||
})} | ||
</div> | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
export default HogList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React, { useState } from "react"; | ||
|
||
const HogTile = ({ hog, handleHideHog }) => { | ||
const [showDetails, setShowDetails] = useState(false) | ||
const handleShowDetails = () => setShowDetails(!showDetails) | ||
|
||
return ( | ||
<div key={hog.name} className="ui card" > | ||
<div className="image"> | ||
<img onClick={handleShowDetails} src={hog.image}/> | ||
</div> | ||
<div className="content"> | ||
<p className="header">{hog.name}</p> | ||
{showDetails ? | ||
<div className="meta"> | ||
<span>Specialty: {hog.specialty}</span><br/> | ||
<span>Weight: {hog.weight}</span><br/> | ||
<span>{hog.greased ? 'Greased' : 'Not Greased'}</span><br/> | ||
<span> | ||
<i className="certificate icon"></i> | ||
{hog["highest medal achieved"]} | ||
</span> | ||
</div> | ||
: null} | ||
<button className="ui button" onClick={() => handleHideHog(hog.name)}>Hide This Hog!</button> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default HogTile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from "react"; | ||
|
||
const SortDropdown = ({ handleSort }) => { | ||
const handleChange = (e) => handleSort(e.target.value) | ||
|
||
return ( | ||
<select name="sort" onChange={handleChange}> | ||
<option value="">Sort</option> | ||
<option value="name">Name</option> | ||
<option value="weight">Weight</option> | ||
</select> | ||
) | ||
} | ||
|
||
export default SortDropdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nice!