-
Notifications
You must be signed in to change notification settings - Fork 40
Dragonfly - Sno Ochoa #39
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
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,14 +1,38 @@ | ||
| import './App.css'; | ||
| import ChatLog from './components/ChatLog'; | ||
| import chatMessages from './data/messages.json'; | ||
| import { useState } from 'react'; | ||
|
|
||
| const calculateTotalLikes = (messages) => { | ||
| return messages.reduce((total, msg) => { | ||
| return total + (msg.liked ? 1 : 0); | ||
| }, 0); | ||
| }; | ||
|
|
||
| const App = () => { | ||
| const [messages, setMessages] = useState(chatMessages); | ||
|
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 work on using the |
||
|
|
||
| const toggleLike = (id) => { | ||
| const updatedMessages = messages.map((msg) => { | ||
| if (msg.id === id) { | ||
| return { ...msg, liked: !msg.liked }; | ||
| } else { | ||
| return msg; | ||
| } | ||
| }); | ||
| setMessages(updatedMessages); | ||
| }; | ||
|
Comment on lines
+15
to
+24
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 work on using const toggleLike = (id) =>
setEntries(PrevEntries => PrevEntries.map(entry =>
entry.id === id ? { ...entry, liked: !entry.liked } : entry
));When you find yourself with simple checks like these, more often than not a ternary could be implemented instead for conciseness but still maintaining readability. |
||
|
|
||
| const totalLikes = calculateTotalLikes(messages); | ||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <div id='App'> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1>React Chatlog</h1> | ||
| <h2>{totalLikes} ❤️s</h2> | ||
|
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. ⭐️ |
||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog entries={messages} onLikeToggle={toggleLike} /> | ||
|
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 work here! 🫡 You may see conventions where you put the props for a component on separate lines like so: <ChatLog
entries={entries}
onToggleLike={toggleLike}
/>This is one way to make components with many props more readable! |
||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,33 @@ | ||
| import './ChatEntry.css'; | ||
| import TimeStamp from './TimeStamp'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatEntry = (props) => { | ||
| const heart = props.liked ? '❤️' : '🤍'; | ||
|
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. 🫡 |
||
|
|
||
| const ChatEntry = () => { | ||
| return ( | ||
| <div className="chat-entry local"> | ||
| <h2 className="entry-name">Replace with name of 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> | ||
| <div className='chat-entry local'> | ||
| <h2 className='entry-name'>{props.sender}</h2> | ||
| <section className='entry-bubble'> | ||
| <p>{props.body}</p> | ||
| <p className='entry-time'> | ||
| <TimeStamp time={props.timeStamp} /> | ||
| </p> | ||
| <button className='like' onClick={() => props.onLikeToggle(props.id)}> | ||
| {heart} | ||
| </button> | ||
|
Comment on lines
+9
to
+18
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. Good! You used your props here to make your presentational component dynamic! |
||
| </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, | ||
| onLikeToggle: PropTypes.func.isRequired, | ||
| }; | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import './ChatLog.css'; | ||
| import ChatEntry from './ChatEntry'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatLog = (props) => { | ||
|
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 see that you didn't de-structure your props, this could have the benefit of readily alerting you and other developers that the data you are working with comes from the props object and should be re-assigned or mutated. |
||
| return ( | ||
| <div className='chat-log'> | ||
| {props.entries.map((msg) => ( | ||
| <ChatEntry | ||
| key={msg.id} | ||
| id={msg.id} | ||
| sender={msg.sender} | ||
| body={msg.body} | ||
| timeStamp={msg.timeStamp} | ||
| liked={msg.liked} | ||
| onLikeToggle={props.onLikeToggle} | ||
| /> | ||
|
Comment on lines
+9
to
+17
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. Since the names of the keys on const chatComponents = entries.map((entry) => {
return(
<ChatEntry
{...entry}
onLikeToggle={onLikeBtnToggle}
key={entry.id}
/>
);
});Though, what you have is more verbose! The suggestion I have above can be used when we want to use pass every |
||
| ))} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| ChatLog.propTypes = { | ||
| entries: PropTypes.arrayOf( | ||
| PropTypes.shape({ | ||
| sender: PropTypes.string.isRequired, | ||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
| liked: PropTypes.bool.isRequired, | ||
| }) | ||
| ).isRequired, | ||
| onLikeToggle: PropTypes.func.isRequired, | ||
| }; | ||
|
Comment on lines
+23
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. Good job on including prop types for this component! Just a quick reminder: in React v19, |
||
|
|
||
| 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.
Perfect use of the
.reducemethod here! I see that you moved this function outside of theAppcomponent as well. Since it isn't strongly tied to this file you could move it inside of its own utilities file. However, I think it's placement here is fine too!