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
Categories on the DOM #13
Open
drteresavasquez
wants to merge
6
commits into
complete
Choose a base branch
from
categories-on-dom
base: complete
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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
6 commits
Select commit
Hold shift + click to select a range
9ad07dc
form
drteresavasquez 20e8610
added categories to DOM
drteresavasquez 20ea947
ac
drteresavasquez 47f31c7
categories
drteresavasquez 66b3f8a
adding categories and new component
drteresavasquez d578cf6
updated proptype
drteresavasquez File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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,61 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Todo from './Todo'; | ||
|
||
export default function CategorizedTodos({ todos, setTodos, setEditItem }) { | ||
const [categorizedTodos, setCategorizedTodos] = useState({}); | ||
|
||
const categoryGroups = () => { | ||
// const sortedObj = {}; | ||
// array.forEach((todo) => { | ||
// // LOOKING to see if a key of item exists | ||
// if (todo.category in sortedObj) { | ||
// // if so, set the value of the key array to the spreaded object and the current todo | ||
// sortedObj[todo.category] = [...sortedObj[todo.category], todo]; | ||
// } else { | ||
// // if not, create the category and set the value as the current todo | ||
// sortedObj[todo.category] = [todo]; | ||
// } | ||
// }); | ||
|
||
// DOING THE SAME AS ABOVE USING REDUCE | ||
const sortedObj = todos.reduce((todoObject, currentObject) => { | ||
const main = todoObject; | ||
// if the current category already exists, push the currentObject into the array...otherwise, set the value to an array and push the currentObject into it. | ||
(main[currentObject.category] = main[currentObject.category] || []).push( | ||
currentObject, | ||
); | ||
return main; | ||
}, {}); | ||
|
||
setCategorizedTodos(sortedObj); | ||
}; | ||
|
||
useEffect(() => { | ||
categoryGroups(); | ||
}, [todos]); | ||
|
||
return ( | ||
<div> | ||
{Object.keys(categorizedTodos).map((category) => ( | ||
<div key={category}> | ||
<h4>Category {category}</h4> | ||
{categorizedTodos[category].map((todo) => ( | ||
<Todo | ||
key={todo.firebaseKey} | ||
taco={todo} | ||
setTodos={setTodos} | ||
setEditItem={setEditItem} | ||
/> | ||
))} | ||
</div> | ||
))} | ||
</div> | ||
); | ||
} | ||
|
||
CategorizedTodos.propTypes = { | ||
todos: PropTypes.arrayOf(PropTypes.object).isRequired, | ||
setTodos: PropTypes.func.isRequired, | ||
setEditItem: PropTypes.func.isRequired, | ||
}; |
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.
The More You Know
You could also use
Object.entries()
It gives you access to the key and value in an array so you'd have to destructure like this.So on line 43 you don't have to grab the value using bracket notation. You'd instead do this
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.
I just looked it up, and the way you did it was more performant 🙃
https://gists.cwidanage.com/2018/06/how-to-iterate-over-object-entries-in.html