Skip to content

Conversation

@nadxelleHernandez
Copy link

My test aren't going to pass because I use an object to keep track of each message. I also used a prop function to get the correct color for each message according if they were local or not.

Copy link

@kelsey-steven-ada kelsey-steven-ada left a comment

Choose a reason for hiding this comment

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

Looks good 🎉 I've left some feedback as comments, please check them out when you can and reach out here or on Slack if there's anything I can clarify =]

Comment on lines +30 to +31
const [localColor, setLocalColor] = useState('');
const [remoteColor, setRemoteColor] = useState('');

Choose a reason for hiding this comment

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

If both local and remote start out with black as the default color, that could be the initial value for these state variables.

msg.body,
msg.timeStamp,
msg.liked,
isLocal

Choose a reason for hiding this comment

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

Neat way to add the local/remote data to the messages. In this case where we only have 2 senders, I might suggest using the value of sender to determine remote vs local over holding a new piece of data, but this approach will scale better.

const [chatEntries, setChatEntries] = useState(messages);
const [localColor, setLocalColor] = useState('');
const [remoteColor, setRemoteColor] = useState('');
const [totalLikes, setTotalLikes] = useState(initialLikes);

Choose a reason for hiding this comment

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

Since the liked status of a message lives in the chatEntries data we should avoid holding an extra piece of state that we need to manually keep in sync. We can use a higher order function like array.reduce to take our list of messages and reduce it down to a single value.

// This could be returned from a helper function
// totalLikes is a variable that accumulates a value as we loop over each entry in chatEntries
const likesCount = chatEntries.reduce((totalLikes, currentMessage) => {
    // If currentMessage.liked is true add 1 to totalLikes, else add 0
    return (totalLikes += currentMessage.liked ? 1 : 0);
}, 0); // The 0 here sets the initial value of totalLikes to 0

<h1>Application title</h1>
<h1>
Chat Between{' '}
<span className={localColor}>{chatEntries[0].sender}</span> and{' '}

Choose a reason for hiding this comment

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

Nice use of new lines to keep the contents of the h1 clear.

Comment on lines +11 to +22
const clickLike = (e) => {
onUpdateEntry(
new ChatMessage(
message.id,
message.sender,
message.body,
message.timeStamp,
!message.liked,
message.isLocal
)
);
};

Choose a reason for hiding this comment

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

I would consider passing the id of the message clicked to onUpdateEntry and having the App code handle the new message creation. When ChatEntry creates the new object for the App state, it takes some responsibility for managing those contents. If we want the responsibility of managing the state to live solely with App, we would want it to handle defining the new message object.

This made me think of a related concept in secure design for APIs. Imagine we had an API for creating and updating messages, and it has an endpoint /<msg_id>/like meant to update a true/false liked value. We could have that endpoint accept a body in the request and let the user send an object with data for the message's record (similar to passing a message object from ChatEntry to App), but the user could choose to send any data for those values. If the endpoint only takes in an id and handles updating the liked status for the message itself, there is less opportunity for user error or malicious action.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you! I understand!

);
};

ColorChoice.propTypes = {

Choose a reason for hiding this comment

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

Great use of PropTypes across the files =]

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