Conversation
…local and remote user.
yangashley
left a comment
There was a problem hiding this comment.
Nice job on react-chatlog! I do see a test failing. Did you get a chance to look into why it's failing?
Good work getting the local and remote conversation feature.
I made a couple suggestions about coding patterns that I think are important to recognize and follow for React.
Let me know if you have questions about my review comments.
| const localUser = chatMessages[0].sender | ||
| const remoteUser = chatMessages[1].sender |
There was a problem hiding this comment.
What if textMessage[0] and textMessage[1] were both messages from the same person? How else could you find a way to get the names of the senders?
| const localUser = chatMessages[0].sender | ||
| const remoteUser = chatMessages[1].sender | ||
|
|
||
| const [chatData, setChatData] = useState(chatMessages); |
There was a problem hiding this comment.
👍 nice job setting up state for chat messages.
| let totalLikes = 0 | ||
| for (const message of chatData) { | ||
| if (message.liked) { | ||
| totalLikes +=1 | ||
| }; | ||
| }; |
There was a problem hiding this comment.
We can use the reduce method to calculate the total likes too:
const totalLikes = () => {
return chatData.reduce((total, text) => {
return text.liked ? total + 1 : total;
}, 0)
};| chatData={chatData} | ||
| onUpdateChat={onUpdateChat}> |
| const onUpdateChat = (updatedChat) => { | ||
| const messages = chatData.map(chat => { | ||
| if (chat.id === updatedChat.id) { | ||
| return updatedChat; | ||
| } else { | ||
| return chat; | ||
| } | ||
| }); |
There was a problem hiding this comment.
Rather than receiving a new entry with it's like status already changed here, rewrite this method to accept only an id. The logic about how to copy the message and change the liked status would be better if it was written in App.js, since the state must have all the information necessary to make a new message.
const onUpdateChat = id => {
const messages = chatData.map(chat => {
if (chat.id === id){
return {...chat, liked: !chat.liked};
}
else {
return chat;
}
});| const ChatLogEntries = props.chatData.map((entry) => { | ||
| return ( | ||
| <div key={entry.id}> | ||
| <ChatEntry | ||
| id={entry.id} | ||
| sender={entry.sender} | ||
| timeStamp={entry.timeStamp} | ||
| body={entry.body} | ||
| liked={entry.liked} | ||
| onUpdateChat={props.onUpdateChat} | ||
| chatEntry={entry.sender===props.chatData[0].sender ? 'chat-entry local' : 'chat-entry remote'} | ||
| ></ChatEntry> | ||
| </div> | ||
| ); | ||
| }); |
There was a problem hiding this comment.
Small note that variables don't begin with a capital letter. The prop key should go on each ChatEntry, not on the entire div. When you have an array of components (like you have an array of ChatEntries), each component in the array gets a prop key.
Also, instead of writing a method ChatLogEntries to reference the return value from calling map() on props.chatData, it's more common to directly iterate over the chat entries in your component like this without using a variable:
const ChatLog = (props) => {
return (
<div>
{props.entries.map((message) => (
<ChatEntry
key={entry.id} // the key should go on each ChatEntry
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onUpdateChat={props.onUpdateChat}
/>
))}
</div>
)
};The logic on line 20 to determine the class name for remote or local should live in ChatEntry.js I think you can omit line 20 completely and have this logic live in the child component. I think this because each child component should know who its sender is and thus should be in charge of determining the class.
| const updatedChat = { | ||
| id: props.id, | ||
| sender: props.sender, | ||
| body: props.body, | ||
| timeStamp: props.timeStamp, | ||
| liked: !props.liked | ||
| }; | ||
|
|
||
| props.onUpdateChat(updatedChat); | ||
| }; | ||
|
|
||
| const heartColor = props.liked ? '❤️' : '🤍'; | ||
|
|
||
| return ( | ||
| <div className="chat-entry local"> | ||
| <h2 className="entry-name">Replace with name of sender</h2> | ||
| <div className={props.chatEntry}> |
There was a problem hiding this comment.
Instead of getting the class 'remote' or 'local' from props, we can calculate it here.
<div className={props.sender === 'Estragon' ? 'chat-entry remote' : 'chat-entry local'}>
| sender: PropTypes.string.isRequired, | ||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
| liked: PropTypes.bool, | ||
| onUpdateChat: PropTypes.func.isRequired |
There was a problem hiding this comment.
The PropType check is missing id and chatEntry that are passed into each ChatEntry component on lines 14 and 20 of ChatLog.js
| const onLikeButtonClick = () => { | ||
| const updatedChat = { | ||
| id: props.id, | ||
| sender: props.sender, | ||
| body: props.body, | ||
| timeStamp: props.timeStamp, | ||
| liked: !props.liked | ||
| }; | ||
|
|
||
| props.onUpdateChat(updatedChat); | ||
| }; |
There was a problem hiding this comment.
When you just pass in an id to props.onUpdateChat(), then this method can be refactored to just be one line:
const onLikeButtonClick = () => {
props.onUpdateChat(props.id);
};| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatEntry = (props) => { | ||
| const timeEntry=props.timeStamp |
There was a problem hiding this comment.
Nitpick - there should be whitespace around the equal sign. When we pass props to a component, then by convention we should omit the spaces around the equal sign, but when we're declaring variables we should have them.
No description provided.