-
Notifications
You must be signed in to change notification settings - Fork 40
Dragonflies - Malik #27
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,33 @@ | ||
| import './App.css'; | ||
| import ChatLog from './components/ChatLog'; | ||
| import data from '/src/data/messages.json'; | ||
| import { useState } from 'react'; | ||
|
|
||
|
|
||
| const App = () => { | ||
|
|
||
| const [entries, setEntries] = useState(data) | ||
|
|
||
| function toggleLike(id) { | ||
| const updatedEntries = entries.map(entry => { | ||
| if (entry.id === id) { | ||
| return { ...entry, liked: !entry.liked } | ||
| } | ||
| return entry; | ||
| }); | ||
| setEntries(updatedEntries); | ||
| }; | ||
|
|
||
| const totalLikes = entries.filter(entry => entry.liked).length; | ||
|
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. You could also do this with the reduce method like so: const totalLikes = chatMessages.reduce((count, message) => count + (message.liked ? 1 : 0), 0); |
||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1>Chat Between Vladimir and Estragon</h1> | ||
| <h3>{totalLikes} ❤️s</h3> | ||
|
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={entries} onToggleLike={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. 🫡 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,31 @@ | ||||||||||||||||||||||
| import './ChatEntry.css'; | ||||||||||||||||||||||
| import TimeStamp from './TimeStamp'; | ||||||||||||||||||||||
| import propTypes from 'prop-types'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const ChatEntry = () => { | ||||||||||||||||||||||
| const ChatEntry = ({ id, sender, body, timeStamp, liked, onToggleLike }) => { | ||||||||||||||||||||||
|
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 de-structuring your props! ⭐️ |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const localOrRemote = sender === 'Vladimir' ? 'chat-entry local' : 'chat-entry remote'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return ( | ||||||||||||||||||||||
| <div className="chat-entry local"> | ||||||||||||||||||||||
| <h2 className="entry-name">Replace with name of sender</h2> | ||||||||||||||||||||||
| <div className={localOrRemote}> | ||||||||||||||||||||||
|
Comment on lines
+7
to
+10
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. ⭐️ |
||||||||||||||||||||||
| <h2 className="entry-name">{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>{body}</p> | ||||||||||||||||||||||
| <p className="entry-time"> | ||||||||||||||||||||||
| <TimeStamp time={timeStamp} /> | ||||||||||||||||||||||
|
Comment on lines
+11
to
+15
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 job on interpolating your JS (props) into your JSX object return body and leveraging your |
||||||||||||||||||||||
| </p> | ||||||||||||||||||||||
| <button className="like" onClick={() => onToggleLike(id)}>{liked ? '❤️' : '🤍'}</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. 👍🏿 |
||||||||||||||||||||||
| </section> | ||||||||||||||||||||||
| </div> | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ChatEntry.propTypes = { | ||||||||||||||||||||||
| // Fill with correct proptypes | ||||||||||||||||||||||
| sender: propTypes.string.isRequired, | ||||||||||||||||||||||
| body: propTypes.string.isRequired, | ||||||||||||||||||||||
| timeStamp: propTypes.string.isRequired, | ||||||||||||||||||||||
| liked: propTypes.bool.isRequired, | ||||||||||||||||||||||
| onToggleLike: propTypes.func | ||||||||||||||||||||||
|
Comment on lines
+24
to
+28
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. ⭐️
Suggested change
|
||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| export default ChatEntry; | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import './ChatLog.css'; | ||
| import ChatEntry from './ChatEntry'; | ||
|
|
||
| const ChatLog = ({ entries, onToggleLike }) => { | ||
| const chatEntries = entries.map(entry => ( | ||
| <ChatEntry | ||
| key={entry.id} | ||
| id={entry.id} | ||
| sender={entry.sender} | ||
| body={entry.body} | ||
| timeStamp={entry.timeStamp} | ||
| liked={entry.liked} | ||
| onToggleLike={onToggleLike} | ||
| /> | ||
| )); | ||
|
Comment on lines
+5
to
+15
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 |
||
|
|
||
| return ( | ||
| <section className='chat-log'>{chatEntries}</section> | ||
| ) | ||
|
|
||
| }; | ||
|
|
||
| export default ChatLog; | ||
|
Comment on lines
+21
to
+23
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. You didn’t include prop types for this component. While PropTypes is deprecated in React v19, it's still important to ensure consistent type safety across your application. Recall that React encourages you to adopt a type system like |
||
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 work on using
.mapto map over your entries! Very functional! Since you are using a simple conditional block here, we could make this more succinct by using a ternary instead like so:When you find yourself with simple checks like these, more often than not a ternary could be implemented instead for conciseness but still maintaining some kind of readability.