-
Notifications
You must be signed in to change notification settings - Fork 111
Shelby Faulconer, Sea Turtles, C17 #99
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
base: main
Are you sure you want to change the base?
Changes from all commits
144ed48
9e54c65
9ba4a95
cd1e650
c5094b6
2106549
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,16 +1,48 @@ | ||
| import React from 'react'; | ||
| import React, {useState} from 'react'; | ||
| import './App.css'; | ||
| import chatMessages from './data/messages.json'; | ||
| import ChatLog from './components/ChatLog' | ||
|
|
||
| const App = () => { | ||
|
|
||
| const [entries, setEntries] = useState(chatMessages); | ||
|
|
||
| const toggleLike = (id) => { | ||
| const entriesCopy = entries.map((entry) => { | ||
| if (entry.id === id) { | ||
| return { | ||
| id: entry.id, | ||
| sender: entry.sender, | ||
| body: entry.body, | ||
| timeStamp: entry.timeStamp, | ||
| liked: !entry.liked | ||
| }; | ||
| } else return entry; | ||
| }); | ||
| setEntries(entriesCopy); | ||
| }; | ||
|
|
||
| const countLikes = () => { | ||
| let counter = 0; | ||
| for (let entry of entries) { | ||
| if (entry.liked) { | ||
| counter += 1; | ||
| }; | ||
| }; | ||
| return counter; | ||
| }; | ||
|
Comment on lines
+25
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. Great use of the existing data to calculate the total likes! Another option would be to use the array function return messageData.reduce((totalLikes, currentMessage) => {
// If currentMessage.liked is true add 1 to totalLikes, else add 0
return (totalLikes += currentMessage.liked ? 1 : 0);
}, 0); // The 0 here sets the initial value of totalLikes to 0 |
||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1>Chat Log</h1> | ||
| <h2>{countLikes()} ❤️s</h2> | ||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog | ||
| entries={entries} | ||
| setLiked={toggleLike} | ||
| /> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,29 @@ | ||
| import React from 'react'; | ||
| import './ChatEntry.css'; | ||
| import PropTypes from 'prop-types'; | ||
| import TimeStamp from './TimeStamp'; | ||
|
|
||
| const ChatEntry = (props) => { | ||
| const heartIcon = (props.liked) ? '❤️' : '🤍'; | ||
| const messageAlign = (props.sender === 'Vladimir') ? 'remote' : 'local'; | ||
|
Comment on lines
+7
to
+8
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. Ternary operators! 🎉 |
||
| return ( | ||
| <div className="chat-entry local"> | ||
| <h2 className="entry-name">Replace with name of sender</h2> | ||
| <div className={`chat-entry ${messageAlign}`}> | ||
| <h2 className="entry-name">{props.sender}</h2> | ||
| <section className="entry-bubble"> | ||
| <p>Replace with body of ChatEntry</p> | ||
| <p className="entry-time">Replace with TimeStamp component</p> | ||
| <button className="like">🤍</button> | ||
| <p>{props.body}</p> | ||
| <p className="entry-time"><TimeStamp time={props.timeStamp}/></p> | ||
| <button className="like" onClick={() => {props.setLiked(props.id)}}>{heartIcon}</button> | ||
|
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. I like this pattern of sending just the |
||
| </section> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| ChatEntry.propTypes = { | ||
| //Fill with correct proptypes | ||
| id: PropTypes.number.isRequired, | ||
| sender: PropTypes.string.isRequired, | ||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
| liked: PropTypes.bool.isRequired, | ||
|
Comment on lines
+22
to
+26
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. Nice use of |
||
| }; | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import React from 'react'; | ||
| import ChatEntry from './ChatEntry'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatLog = (props) => { | ||
|
|
||
| const chatComponents = props.entries.map((entry) => { | ||
| return ( | ||
| <ChatEntry | ||
| id = {entry.id} | ||
| key = {entry.id} | ||
| sender={entry.sender} | ||
| body={entry.body} | ||
| timeStamp={entry.timeStamp} | ||
| liked={entry.liked} | ||
| setLiked={props.setLiked} | ||
| /> | ||
| ); | ||
| }); | ||
| return ( | ||
| <section className="chat-log">{chatComponents}</section> | ||
| ); | ||
| }; | ||
|
|
||
| ChatLog.propTypes = { | ||
| entries: PropTypes.arrayOf(PropTypes.object).isRequired, | ||
|
Comment on lines
+22
to
+26
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. We can be even more specific with our PropTypes for validation. If we know that we need an array, and that array's objects need to hold certain data, we can check the objects as well! Inside list: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number.isRequired,
customTitle: PropTypes.string.isRequired,
btnStyle:PropTypes.object,
})
).isRequired,Source: https://stackoverflow.com/questions/59038307/reactjs-proptypes-validation-for-array-of-objects Similar to some of the feedback for ChatEntry's PropTypes, it would be good to list the function ChatLog accepts here as well. |
||
|
|
||
| }; | ||
|
|
||
| export default ChatLog; | ||
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 managing of data & making sure we make a new object when we need to alter a message in our list!
A best practice for copying over an object's values is to use the spread operator, then overwrite any key/value pairs we want to change: