-
Notifications
You must be signed in to change notification settings - Fork 40
Dragonfly -- Solhee J. #32
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.jsx'; | ||
| import messagesData from './data/messages.json'; | ||
| import { useState } from 'react'; | ||
|
|
||
| const App = () => { | ||
| const [messageData, setMessageData] = useState(messagesData); | ||
|
|
||
| const toggleLiked = (messageId) => { | ||
| const messages = messageData.map(message => { | ||
| if (message.id === messageId) { | ||
| return { ...message, liked: !message.liked }; | ||
| } else { | ||
| return message; // message like status not changed | ||
| } | ||
| }); | ||
| setMessageData(messages); | ||
| }; | ||
|
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. Great to see you 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 readability. |
||
| const totalLikes = messageData.filter(message => message.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 const totalLikes = chatMessages.reduce((count, message) => count + (message.liked ? 1 : 0), 0); |
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1>React Chatlog</h1> | ||
| <section id="heartWidget">{totalLikes} ❤️s</section> | ||
|
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={messageData} | ||
| onLikeToggle={toggleLiked} | ||
| ></ChatLog> | ||
|
Comment on lines
+27
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. Formatting your component like this is one way to maintain readability! ⭐️🫡 |
||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,20 +1,33 @@ | ||||||
| import './ChatEntry.css'; | ||||||
| import TimeStamp from './TimeStamp.jsx'; | ||||||
| import PropTypes from 'prop-types'; | ||||||
|
|
||||||
| const ChatEntry = () => { | ||||||
| const ChatEntry = ({id, sender, body, timeStamp, liked, onLikeToggle}) => { | ||||||
|
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
|
||||||
| const likeButtonClicked = () => { | ||||||
| onLikeToggle(id); | ||||||
| }; | ||||||
| const heartColor = liked ? '❤️' : '🤍'; | ||||||
|
Comment on lines
+6
to
+9
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 ( | ||||||
| <div className="chat-entry local"> | ||||||
| <h2 className="entry-name">Replace with name of sender</h2> | ||||||
| <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}></TimeStamp> | ||||||
|
Comment on lines
+12
to
+16
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. 🫡 |
||||||
| </p> | ||||||
| <button className="like" onClick={likeButtonClicked}>{heartColor}</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. The button function could also be implemented like so: <button className='like' onClick={() => likeButtonClicked(id)}>{liked ? '❤️': '🤍'}</button> |
||||||
| </section> | ||||||
| </div> | ||||||
| ); | ||||||
| }; | ||||||
|
|
||||||
| ChatEntry.propTypes = { | ||||||
| // Fill with correct proptypes | ||||||
| sender: PropTypes.string.isRequired, | ||||||
| body: PropTypes.string.isRequired, | ||||||
| timeStamp: PropTypes.string.isRequired, | ||||||
| id: PropTypes.number, | ||||||
| liked: PropTypes.bool, | ||||||
| onLikeToggle: PropTypes.func | ||||||
| }; | ||||||
|
|
||||||
| export default ChatEntry; | ||||||
| export default ChatEntry; | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| .chat-log { | ||
| margin: auto; | ||
| max-width: 50rem; | ||
| max-width: 50rem | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import './ChatLog.css'; | ||
| import PropTypes from 'prop-types'; | ||
| import ChatEntry from './ChatEntry.jsx'; | ||
|
|
||
| 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. @ellenjin, is there a reason why you didn't de-structure your props here? Only asking out of curiosity! |
||
| 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. I also prefer to use a variable to hold my mapped components! Just a heads, you might see the pattern where the mapping is done in the JSX object. Some developers prefer that since they feel like it helps them understand the overall structure of the component. |
||
| return ( | ||
| <ChatEntry className="chat-log" | ||
| key={message.id} | ||
| id={message.id} | ||
| sender={message.sender} | ||
| body={message.body} | ||
| timeStamp={message.timeStamp} | ||
| liked={message.liked} | ||
| onLikeToggle={props.onLikeToggle} | ||
| ></ChatEntry> | ||
|
Comment on lines
+8
to
+16
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 ( | ||
| <> | ||
| {messageComponents} | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| ChatLog.propTypes = { | ||
| entries: PropTypes.arrayOf( | ||
| PropTypes.shape({ | ||
| sender: PropTypes.string.isRequired, | ||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
| id: PropTypes.number, | ||
| liked: PropTypes.bool | ||
| }) | ||
| ).isRequired, | ||
| onLikeToggle: PropTypes.func | ||
| }; | ||
|
Comment on lines
+27
to
+38
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, |
||
| export default ChatLog; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -188,4 +188,4 @@ | |
| "timeStamp":"2018-05-29T23:17:34+00:00", | ||
| "liked": false | ||
| } | ||
| ] | ||
| ] | ||
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 the
useStatehook to initialize state for yourAppcomponent.