Conversation
mikellewade
left a comment
There was a problem hiding this comment.
Nice work! Your project lets me know that you have a goo understanding of React and useState!
| const toggleLike = (id) => { | ||
| setChatMessages(chatMessages => chatMessages.map(message => { | ||
| if (message.id === id) { | ||
| return { ...message, liked: !message.liked }; | ||
| } else { | ||
| return message; | ||
| } | ||
| })); |
There was a problem hiding this comment.
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!
| })); | ||
| }; | ||
|
|
||
| const totalLikes = chatMessages.filter(message => message.liked).length; |
There was a problem hiding this comment.
You could also do this with the reduce method like so:
const totalLikes = chatMessages.reduce((count, message) => count + (message.liked ? 1 : 0), 0);| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h2>{totalLikes} 🤍s</h2> |
There was a problem hiding this comment.
Your code is failing a test. It is because this is the wrong heart (needs to be ❤️).
| <h2>{totalLikes} 🤍s</h2> | |
| <h2>{totalLikes} ❤️s</h2> |
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog entries={chatMessages} onToggleLike={toggleLike}></ChatLog> |
| <ChatEntry | ||
| key={entry.id} | ||
| id={entry.id} | ||
| sender={entry.sender} | ||
| body={entry.body} | ||
| timeStamp={entry.timeStamp} | ||
| liked={entry.liked} | ||
| onToggleLike={onToggleLike} | ||
| /> |
There was a problem hiding this comment.
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}
/>
);
});| 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, | ||
| onToggleLike: PropTypes.func.isRequired, | ||
| }; |
| const handleClick = () => { | ||
| onToggleLike(id); | ||
| }; |
There was a problem hiding this comment.
The button function could also be implemented like so:
<button className='like' onClick={() => likeButtonClicked(id)}>{liked ? '❤️': '🤍'}</button>| <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> | ||
| </p> | ||
| <button className="like" onClick={handleClick}> | ||
| {liked ? '❤️' : '🤍'} | ||
| </button> |
| id: PropTypes.number.isRequired, | ||
| sender: PropTypes.string.isRequired, | ||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
| liked: PropTypes.bool.isRequired, | ||
| onToggleLike: PropTypes.func.isRequired, |
No description provided.