Skip to content
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

Zoisite Yvett J #117

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Zoisite Yvett J #117

wants to merge 6 commits into from

Conversation

yvett-codes
Copy link

No description provided.

Copy link

@yangashley yangashley 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 wrapping this project up 🥳

I left a couple comments related to how you can lift the state of messages up from ChatEntry up to App.js. My point spans several comments so have a look at them together. Ultimately, we should use state to keep track of chatMessages, but we don't need it for keeping track of likes.

Let me know if you have questions about my review comments! You're done with core curriculum 💪

Comment on lines +9 to +15
const likeMessage = () => {
setLikedMessages(likedMessages => likedMessages + 1);
};

const unlikeMessage = () => {
setLikedMessages(likedMessages => likedMessages - 1);
};

Choose a reason for hiding this comment

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

You can actually keep track of the number of likes across all the messages without using state, thus we should prefer to not use state to reduce the complexity of the project.

Instead, you can create a function that iterates over all the chats and adds up the number of likes, something like:

const getHeartCount = () => {
    return chatMessages.reduce((total, chat) => {
      return chat.liked ? total + 1 : total;
    }, 0)
};

// then on line 21, you can write this:
<h2>{getHeartCount()} ❤️s</h2>

This approach for calculating the total likes across all messages, would require you to keep track of the messages as state though and you'd need an event handler for updating when a message is liked. I'll leave more details about that in another comment.

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

Choose a reason for hiding this comment

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

Instead of just passing in chatMessages from the messages.json file, we should use state as a single source of truth for keeping track of the data related to messages. Therefore, we should have a piece of state on line 8 like:

const [chatMessageData, setChatMessagData] = useState(chatMessages);

You'd also need an event handler that you can pass down to ChatEntry.js that would help you toggle the liked value on a single chat. It would be better for the logic about how to copy the message and change the liked status to be written in App.js

const updateEntryData = id => {
const entries = chatMessageData.map(entry => {
      if (entry.id === id){
        return {...entry, liked: !entry.liked};
      } 
      else {
        return entry;
      }
    });
    setChatMessagData(entries);
}

<ChatLog
entries={chatMessages}
likeMessage={likeMessage}
unlikeMessage={unlikeMessage}

Choose a reason for hiding this comment

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

Following my comment above about creating an event handler in app.js to help you keep track of a message's 'liked' status, you would need to pass that event handler from App to ChatLog and finally into ChatEntry.

<ChatLog
    entries={chatMessageData}
    updateEntryData={updateEntryData}
/>

<button className="like">🤍</button>
<p>{body}</p>
<p className="entry-time">
<TimeStamp time={timeStamp}/>

Choose a reason for hiding this comment

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

Nice job using the provided TimeStamp component

import PropTypes from 'prop-types';

const ChatEntry = (props) => {
const ChatEntry = ( { sender, body, timeStamp, likeMessage, unlikeMessage } ) => {
const [hearted, setHearted] = useState(false);

Choose a reason for hiding this comment

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

Don't need state here to keep track of a message's 'liked' status.

Comment on lines +9 to +12
const toggleHeart = () => {
setHearted(hearted => !hearted);
hearted ? unlikeMessage(): likeMessage();
};

Choose a reason for hiding this comment

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

Following my suggestion about passing down an event handler that can be used to change a message's 'liked' status, you would use that event handler in toggleHeart here:

  const toggleHeart = () => {
    props.updateEntryData(props.id);
  };

Comment on lines +28 to +30
ChatLog.propTypes = {
entries: PropTypes.array.isRequired
};

Choose a reason for hiding this comment

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

Be sure to write out the full propTypes in the future to include shapeOf

Comment on lines +33 to +37
id: PropTypes.number.isRequired,
sender: PropTypes.string.isRequired,
body: PropTypes.number.isRequired,
timeStamp: PropTypes.string.isRequired,
liked: PropTypes.bool.isRequired

Choose a reason for hiding this comment

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

propTypes is missing a couple values that are passed in from ChatLog to ChatEntry: likeMessage and unlikeMessage

hearted ? unlikeMessage(): likeMessage();
};

const heartedIcon = hearted ? '❤️' : '🤍';

Choose a reason for hiding this comment

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

👍

const chatEntries = entries.map(entry => {
return (
<ChatEntry
id={entry.id}

Choose a reason for hiding this comment

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

We should also use the id for the key prop since we're iterating over many entries to create many ChatEntrys:

 <ChatEntry
     id={entry.id}
     key={entry.id}
     // the rest of your props for your component
/>

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