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

Sharks - Camilla #107

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

Sharks - Camilla #107

wants to merge 6 commits into from

Conversation

camilla122333
Copy link

I implemented the time-stamp feature before I saw the TimeStamp component, so I left it that way. I modified the tests to account for the change in emoji.

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 practicing React! Let me know if you have any questions about the comments.

🟢 for react-chatlog

onUpdateChatData: PropTypes.func
};

export default ChatLog;

Choose a reason for hiding this comment

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

👍 Nicely done

Comment on lines +5 to +14
function getDifferenceInYears(timeStamp) {
const today = new Date();
const year = today.getFullYear();

const pastDay = new Date(timeStamp);
const pastYear = pastDay.getFullYear();

const difference = year - pastYear;
return difference;
}

Choose a reason for hiding this comment

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

Nice job implementing your own solution for determining the time elapsed since a message was sent. If you check the components directory, you'll see TimeStamp component for you to use.

You can see how we implemented finding the time elapsed

<p className="entry-time">Replace with TimeStamp component</p>
<button className="like">🤍</button>
<p>{props.body}</p>
<p className="entry-time">{timeInYears}</p>

Choose a reason for hiding this comment

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

How would you refactor line 36 to use the custom TimeStamp component? What would you need to pass into the component as a prop?

Comment on lines +13 to +23
const updateChatData = (updatedMessage) => {
const messages = chatMessagesData.map((message) => {
if (message.id === updatedMessage.id) {
return updatedMessage;
} else {
return message;
}
});

setChatMessagesData(messages);
};

Choose a reason for hiding this comment

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

We use this method to update how a chat entry renders after someone likes an entry. To constrain the scope of this method so that it is only responsible for re-rendering a chat entry after it's been liked, we might refactor this method so that we're not returning the entire message.

updateChatData could become something like:

const updateHeart = (id) => {
    const chatMessagesCopy = [...chatMessagesData];
    for (let entry of chatMessagesCopy) {
      if (entry.id === id) {
        entry.heart = !entry.heart; 
      }
    }
    setChatMessagesData(chatMessagesCopy);
  };

If you do this, then you can update ChatEntry.js too so you only need to pass the prop id instead of the whole message object

Comment on lines +19 to +29
const onLikeButtonClick = () => {
const updatedMessage = {
id: props.id,
sender: props.sender,
body: props.body,
timeStamp: props.timeStamp,
liked: !props.liked
};

props.onUpdateChatData(updatedMessage);
};

Choose a reason for hiding this comment

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

If you refactor onUpdateChatData as I suggested in App.js, then your onLikeButtonClick() method only needs to modify the liked property of of a ChatEntry. If you follow this approach, you'd also need to introduce useState to keep track of a ChatEntry's like state.

The benefit of only changing the liked property is that when a user clicks on a like, they really are only changing liked. Therefore, we shouldn't let the onLikeButtonClick event handler change id, sender, body, timestamp (which is more editing power than this method really needs).

Reworking a solution so only liked is being affected will require you to do more than change a line or two of code, but it would best reflect what actually needs to happen. We should always prefer to only change the properties/data that need to be changed when we code.

Comment on lines +25 to +33
for (let i = 0; i < chatMessagesData.length; i++) {
let message = chatMessagesData[i];
if (users.indexOf(message['sender']) === -1) {
users.push(message['sender'])
}
if (message['liked']) {
likesCount++;
}
}

Choose a reason for hiding this comment

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

Putting this logic into a method like calculateHeartCount and then calling that function within the paragraph tag would help make this file a little more organized and readable.

<p className="header">{calculateHeartCount()} 💙s</p>

Having said that, your logic as it is right now would need to be reworked so that when the method is called it would return an integer

Comment on lines +35 to +38
if (users.length === 2) {
user1 = users[0].toUpperCase();
user2 = users[1].toUpperCase();
}

Choose a reason for hiding this comment

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

You could also directly hard code these user names into your h1 element and remove this logic here so line 44 would look like:

<h1>CHAT BETWEEN {{chatMessages[0].sender}} AND {{chatMessages[1].sender}} </h1>

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