Skip to content

Conversation

@scnkera
Copy link

@scnkera scnkera commented Dec 16, 2024

No description provided.

Copy link

@mikellewade mikellewade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Your project lets me know that you have a goo understanding of React and useState!

Comment on lines +9 to +19
const toggleLike = (id) => {
const updatedMessages = chatMessages.map((message) => {
if (message.id === id) {
// message.liked = !message.liked;
return {...message, liked:!message.liked};
} else {
return message;
}
});
setChatMessages(updatedMessages);
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on this function! By passing this down to your individual chat messages you are now able to have a single source of truth!

Comment on lines +21 to +22
const totalLikes = chatMessages.filter((message) => message.liked).length;

Choose a reason for hiding this comment

The 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);

{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
</main>
<ChatLog entries={chatMessages} onLike={toggleLike} />

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love seeing parents passing things on to their children!

Comment on lines +7 to +15
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
timeStamp={entry.timeStamp}
body={entry.body}
liked={entry.liked}
onLike={props.onLike}
/>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the names of the keys on entry are the same as the names your are setting on ChatEntry attributes/you are using all of the values in entry you could do something something like this to save you a few keystrokes:

  const chatComponents = entries.map((entry) => {
    return(
      <ChatEntry
        {...entry}
        onLikeToggle={onLikeBtnToggle}
        key={entry.id}
      />
    );
  });

Just be mindful that this won't be as explicit as what you have, a trade off!

);
});

return <main>{chatEntries}</main>;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice wrapping this in the <main> tag. I would actually suggest to add in this App.jsx so you could wrap ChatLog in it as well. We don't show anything outside of ChatEntry components but if we did then those would be outside of our <main> tag. We could just keep moving them inside here but why do that when we could wrap the entire component.

Comment on lines +22 to +33
ChatLog.propTypes = {
entries: PropTypes.arrayOf(
PropTypes.shape({
sender: PropTypes.string,
id: PropTypes.number,
body: PropTypes.string,
timeStamp: PropTypes.string,
liked: PropTypes.bool,
})
).isRequired,
onLike: PropTypes.func.isRequired,
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +5 to +8
const ChatEntry = (props) => {
const handleLikeClick = () => {
props.onLike(props.id); // Notify the parent component (App) to toggle like state
};

Choose a reason for hiding this comment

The 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>

<TimeStamp time={props.timeStamp} />
</p>
<button className="like" onClick={handleLikeClick}>
{props.liked ? '❤️' : '🤍'}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ternaries, love to see them! 😍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants