-
Notifications
You must be signed in to change notification settings - Fork 45
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
C22 Sphinx - Aleida Vieyra #37
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really good job on this project Aleida! It shows you have a good understanding of React and state!
const toggleLikeBtnClick= (id) => { | ||
// Refactoring to incorporate func passing state update | ||
setEntries(entries => entries.map(entry=>{ | ||
if (entry.id == id){ | ||
return {...entry, liked: !entry.liked}; | ||
} | ||
else{ | ||
return entry; | ||
} | ||
})); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work on this function! By passing this down to your individual chat messages you are now able to have a single source of truth!
setEntries(entries => entries.map(entry=>{ | ||
if (entry.id == id){ | ||
return {...entry, liked: !entry.liked}; | ||
} | ||
else{ | ||
return entry; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setEntries(entries => entries.map(entry=>{ | |
if (entry.id == id){ | |
return {...entry, liked: !entry.liked}; | |
} | |
else{ | |
return entry; | |
} | |
setEntries(entries => entries.map(entry => { | |
if (entry.id == id) { | |
return {...entry, liked: !entry.liked}; | |
} else { | |
return entry; | |
} |
})); | ||
}; | ||
|
||
const totalLikesCount = entries.reduce((count, entry) => (entry.liked? count+1: count), 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect use of the reduce
function!
const identifyAllChatMembers = () => { | ||
const senders = [...new Set(entries.map(entry => entry.sender))]; | ||
let participant1 = ''; | ||
let participant2 = ''; | ||
for (const sender of senders) { | ||
if (!participant1) { | ||
participant1 = sender; | ||
} | ||
else if (participant1 && !participant2) { | ||
participant2 = sender; | ||
} | ||
}; | ||
return `${participant1} and ${participant2}`; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essentially we are just getting the sender values of the first and second message objects in the list of messages. Instead of looping through the messages and making a set from their senders we could simply access them and then store in a string like so:
const participant1 = entries[0].sender;
const participant2 = entries[1].sender;
const chatTitle = `${participant1} and ${participant2}`;
EDIT: I see now that your method would work in cases where the maybe the first two messages are from a single user. Double texting, I could never! haha
<ChatLog | ||
entries={entries} | ||
onLikeBtnToggle={toggleLikeBtnClick} | ||
/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perf!
const chatComponents = entries.map((entry) => { | ||
return( | ||
<ChatEntry | ||
{...entry} | ||
onLikeToggle={onLikeBtnToggle} | ||
key={entry.id} | ||
/> | ||
); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay spread syntax! 😍
ChatLog.propTypes = { | ||
// Implement | ||
entries: PropTypes.arrayOf(PropTypes.shape({ | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool.isRequired | ||
})).isRequired, | ||
onLikeBtnToggle: PropTypes.func.isRequired, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⭐️
<p className="entry-time"> | ||
<TimeStamp time={timeStamp}/> | ||
</p> | ||
<button className='like' onClick={likeButtonClicked}>{liked? '❤️': '🤍'}</button> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<button className='like' onClick={likeButtonClicked}>{liked? '❤️': '🤍'}</button> | |
<button className='like' onClick={likeButtonClicked}>{liked ? '❤️': '🤍'}</button> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The button function could also be implemented like so:
<button className='like' onClick={() => likeButtonClicked(id)}>{liked ? '❤️': '🤍'}</button>
id: PropTypes.number.isRequired, | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool.isRequired, | ||
onLikeToggle: PropTypes.func.isRequired, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨
No description provided.