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

Olive - React Chatlog - Sea Turtles #95

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

Conversation

olive-lavine
Copy link

No description provided.

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.

First React project down! 🎉 I've left some feedback & suggestions, please take a look when you have time & reach out if there's anything I can clarify 😄

Comment on lines +20 to +28
const getLikeCount = () => {
let likeCount = 0;
for (const entry of chatEntries) {
if (entry.liked === true) {
likeCount += 1;
}
}
return likeCount;
};

Choose a reason for hiding this comment

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

Great use of the existing data to calculate the total likes! Another option would be to use the array function reduce:

    return messageData.reduce((totalLikes, message) => {
      // If messages.liked is true add 1 to totalLikes, else add 0
      return (totalLikes += message.liked ? 1 : 0);
    }, 0); // The 0 here sets the initial value of totalLikes to 0

const updateChatEntry = (updatedEntry) => {
const entries = chatEntries.map((entry) => {
if (entry.id === updatedEntry.id) {
return updatedEntry;

Choose a reason for hiding this comment

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

Nice managing of data & making sure we return a new object when we need to alter a message in our list.

Comment on lines +31 to +35
const participants = new Set();
for (const entry of chatEntries) {
participants.add(entry.sender);
}
return [...participants].join(' and ');

Choose a reason for hiding this comment

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

Great use of Set to get the unique senders! If we weren't significantly concerned about space, we could consider mapping chatEntries to an array of only the sender values, then use a Set to remove duplicates:

const allNames = chatEntries.map(message => message.sender);
const participants = [...new Set(allNames)];
return participants.join(' and ');

Comment on lines +7 to +15
const onLikeButtonClick = () => {
const updatedChatEntry = {
id: props.id,
sender: props.sender,
body: props.body,
timeStamp: props.timeStamp,
liked: !props.liked,
};
props.onLike(updatedChatEntry);

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 props.onLike and having the App code handle the new object 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 /liked/<id> meant to update the 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.

Comment on lines +19 to +20
const displaySide =
props.sender === 'Vladimir' ? 'chat-entry local' : 'chat-entry remote';

Choose a reason for hiding this comment

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

Nice ternary operators & splitting up lines!

return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className={displaySide}>

Choose a reason for hiding this comment

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

Another option would be to have an interpolated string here that always holds chat-entry and use a placeholder where we pass only the remote or local class name (so we don't repeat chat-entry anywhere):

const displaySide = (props.sender === 'Vladimir') ? 'local' : 'remote';
...
<div className={`chat-entry ${displaySide}`}>

Comment on lines +39 to +43
// id: PropTypes.number.isRequired,
sender: PropTypes.string.isRequired,
body: PropTypes.string.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.

Nice use of PropTypes and isRequired! Especially when working with other folks, I would recommend keeping all the props we can expect to give to a component listed & uncommented, and only use isRequired on the props that MUST be passed for the component to render (or our tests to pass 😆).

};

ChatLog.propTypes = {
entries: PropTypes.arrayOf(PropTypes.object).isRequired,

Choose a reason for hiding this comment

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

We can be even more specific with our PropTypes for validation. If we know that we need an array, and that array's objects need to hold certain data, we can check the objects as well! Inside arrayOf we can pass PropTypes.shape() with information about the object's contents. A modified example from stack overflow is below:

list: PropTypes.arrayOf(
    PropTypes.shape({
        id: PropTypes.number.isRequired,
        customTitle: PropTypes.string.isRequired,
        btnStyle:PropTypes.object,
    })
).isRequired,

Source: https://stackoverflow.com/questions/59038307/reactjs-proptypes-validation-for-array-of-objects

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