-
Notifications
You must be signed in to change notification settings - Fork 40
Dragonfly - Olya Mateshov #34
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,17 +1,44 @@ | ||||||||||
| import './App.css'; | ||||||||||
| import { useState } from 'react'; | ||||||||||
| import messagesData from './data/messages.json'; | ||||||||||
| import ChatLog from './components/ChatLog'; | ||||||||||
|
|
||||||||||
| const App = () => { | ||||||||||
| const [messageList, setMessageList] = useState(messagesData); | ||||||||||
|
|
||||||||||
| const likeToggleFunc = (messageId) => { | ||||||||||
| const messages = messageList.map(message => { | ||||||||||
| if (message.id === messageId) { | ||||||||||
| return { ...message, liked: !message.liked }; | ||||||||||
| } else { | ||||||||||
| return message; | ||||||||||
| } | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| setMessageList(messages); | ||||||||||
| }; | ||||||||||
|
Comment on lines
+9
to
+19
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(entries.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 some kind of readability. |
||||||||||
|
|
||||||||||
| const totalLikes = messageList | ||||||||||
| .filter((message) => message.liked) | ||||||||||
| .length; | ||||||||||
|
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 could also do this with the const totalLikes = chatMessages.reduce((count, message) => {
return count + (message.liked ? 1 : 0)
}, 0);As for the formatting, I think you could leave this code on a single since it isn't longer than 80 characters.
Suggested change
|
||||||||||
|
|
||||||||||
| return ( | ||||||||||
| <div id="App"> | ||||||||||
| <header> | ||||||||||
| <h1>Application title</h1> | ||||||||||
| <h1>Chat between Vladimir and Estragon</h1> | ||||||||||
| <div> | ||||||||||
| {totalLikes} ❤️s | ||||||||||
|
Comment on lines
+28
to
+30
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. ⭐️ |
||||||||||
| </div> | ||||||||||
| </header> | ||||||||||
| <main> | ||||||||||
| {/* Wave 01: Render one ChatEntry component | ||||||||||
| Wave 02: Render ChatLog component */} | ||||||||||
| <ChatLog | ||||||||||
| entries={messageList} | ||||||||||
| likeToggleFunc={likeToggleFunc} | ||||||||||
| localSender="Vladimir" | ||||||||||
|
Comment on lines
+35
to
+37
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. A couple of things here:
|
||||||||||
| /> | ||||||||||
| </main> | ||||||||||
| </div> | ||||||||||
| ); | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| export default App; | ||||||||||
| export default App; | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,35 @@ | ||
| import PropTypes from 'prop-types'; | ||
| import './ChatEntry.css'; | ||
| import TimeStamp from './TimeStamp'; | ||
|
|
||
| const ChatEntry = (props) => { | ||
| const likeButtonClicked = () => { | ||
| props.likeToggleFunc(props.id); | ||
| }; | ||
|
Comment on lines
+6
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. ⭐️ |
||
|
|
||
| const ChatEntry = () => { | ||
| return ( | ||
| <div className="chat-entry local"> | ||
| <h2 className="entry-name">Replace with name of sender</h2> | ||
| <div className={props.senderType === 'local' ? 'chat-entry local' : 'chat-entry remote'}> | ||
|
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 assign this ternary logic to a variable before your |
||
| <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">Time Sent: <TimeStamp time={props.timeStamp} /></p> | ||
|
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. 👍🏿 |
||
| <button className="like" onClick={likeButtonClicked}> | ||
| {props.liked ? '❤️' : '🤍'} | ||
|
Comment on lines
+16
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. The button function could also be implemented like so: <button className='like' onClick={() => props.likeToggleFunc(props.id)}>{liked ? '❤️': '🤍'}</button> |
||
| </button> | ||
| </section> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| ChatEntry.propTypes = { | ||
| // Fill with correct proptypes | ||
| id: PropTypes.number.isRequired, | ||
| sender: PropTypes.string.isRequired, | ||
| senderType: PropTypes.oneOf(['local', 'remote']).isRequired, | ||
|
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. 🫡 |
||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
| liked: PropTypes.bool.isRequired, | ||
| likeToggleFunc: PropTypes.func.isRequired, | ||
| }; | ||
|
|
||
| export default ChatEntry; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,6 @@ | |
| margin: auto; | ||
| max-width: 50rem; | ||
| } | ||
| .chat-log ul { | ||
| list-style: none; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import ChatEntry from './ChatEntry'; | ||
| import './ChatLog.css'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatLog = (props) => { | ||
| const messageComponents = props.entries.map((message) => { | ||
|
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. Once again, the naming here is switching back and forth between |
||
| const senderType = message.sender === props.localSender ? 'local' : 'remote'; | ||
|
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. 👍🏿 |
||
|
|
||
| return ( | ||
| <li key={message.id}> | ||
| <ChatEntry | ||
| id={message.id} | ||
| sender={message.sender} | ||
| senderType={senderType} | ||
| body={message.body} | ||
| timeStamp={message.timeStamp} | ||
| liked={message.liked} | ||
| likeToggleFunc={props.likeToggleFunc} | ||
| /> | ||
| </li> | ||
|
Comment on lines
+10
to
+20
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"> | ||
| <ul>{messageComponents}</ul> | ||
|
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> | ||
| ); | ||
| }; | ||
|
|
||
| ChatLog.propTypes = { | ||
| entries: PropTypes.arrayOf( | ||
| PropTypes.shape({ | ||
| id: PropTypes.number.isRequired, | ||
| sender: PropTypes.string.isRequired, | ||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
| liked: PropTypes.bool.isRequired, | ||
| }) | ||
| ).isRequired, | ||
| likeToggleFunc: PropTypes.func.isRequired, | ||
| localSender: PropTypes.string.isRequired, | ||
|
|
||
| }; | ||
|
Comment on lines
+31
to
+44
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 including prop types for this component! Just a quick reminder: in React v19, PropTypes are deprecated. Moving forward, it's best to use TypeScript for type checking, as it's the recommended and more robust solution for ensuring type safety across your application. |
||
|
|
||
| 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.
⭐️