-
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 Somy Cho #34
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,40 @@ | ||
import { useState } from 'react'; | ||
import './App.css'; | ||
import messages from './data/messages.json'; | ||
import ChatLog from './components/ChatLog'; | ||
|
||
const App = () => { | ||
const [chatMessages, setChatMessages] = useState(messages); | ||
|
||
const handleLikeToggle = (id) => { | ||
setChatMessages((prevMessages) => | ||
prevMessages.map((message) => | ||
message.id === id | ||
? { ...message, liked: !message.liked } | ||
: message | ||
) | ||
); | ||
}; | ||
|
||
const totalLikes = chatMessages.filter((message) => message.liked).length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could also do this with the reduce method like so: const totalLikes = chatMessages.reduce((count, message) => count + (message.liked ? 1 : 0), 0); |
||
const person1 = messages[0].sender; | ||
const person2 = messages[1].sender; | ||
Comment on lines
+20
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens here if the first two messages from are texts from the same person? |
||
|
||
return ( | ||
<div id="App"> | ||
<header> | ||
<h1>Application title</h1> | ||
<h1>Chatlog</h1> | ||
<h2>{`Chat between ${person1} and ${person2}`}</h2> | ||
<p>{totalLikes} ❤️s</p> | ||
</header> | ||
<main> | ||
{/* Wave 01: Render one ChatEntry component | ||
Wave 02: Render ChatLog component */} | ||
<ChatLog | ||
entries={chatMessages} | ||
onToggleLike={handleLikeToggle} | ||
Comment on lines
+32
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⭐️ |
||
/> | ||
</main> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; | ||
export default App; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,31 @@ | ||
import PropTypes from 'prop-types'; | ||
import TimeStamp from './TimeStamp'; | ||
import './ChatEntry.css'; | ||
|
||
const ChatEntry = () => { | ||
const ChatEntry = ({id, sender, body, timeStamp, liked, onToggleLike}) => { | ||
return ( | ||
<div className="chat-entry local"> | ||
<h2 className="entry-name">Replace with name of sender</h2> | ||
<div className= {`chat-entry ${liked ? 'liked' :'local'}`}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this ternary is a mistake or not, seems like the |
||
<h2 className="entry-name">{sender}</h2> | ||
<section className="entry-bubble"> | ||
<p>Replace with body of ChatEntry</p> | ||
<p className="entry-time">Replace with TimeStamp component</p> | ||
<button className="like">🤍</button> | ||
<p>{body}</p> | ||
<p className="entry-time"> | ||
<TimeStamp time={timeStamp}/> | ||
</p> | ||
<button className="like" onClick={() => onToggleLike(id)}>{liked ? '❤️' : '🤍'}</button> | ||
Comment on lines
+10
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No notes, nice! |
||
</section> | ||
</div> | ||
); | ||
}; | ||
|
||
ChatEntry.propTypes = { | ||
// Fill with correct proptypes | ||
id: PropTypes.number.isRequired, | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool.isRequired, | ||
onToggleLike: PropTypes.func.isRequired, | ||
Comment on lines
+21
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||
}; | ||
|
||
export default ChatEntry; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import PropTypes from 'prop-types'; | ||
import ChatEntry from './ChatEntry.jsx'; | ||
import './ChatLog.css'; | ||
|
||
const ChatLog = ({ entries, onToggleLike }) => { | ||
const chatEntryComponents = entries.map((entry) => { | ||
return ( | ||
<ChatEntry | ||
id={entry.id} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⭐️ |
||
sender={entry.sender} | ||
body={entry.body} | ||
timeStamp={entry.timeStamp} | ||
liked={entry.liked} | ||
Comment on lines
+9
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the names of the keys on const chatComponents = entries.map((entry) => {
return(
<ChatEntry
{...entry}
onLikeToggle={onLikeBtnToggle}
key={entry.id}
/>
);
}); Just be mindful that this isn't as explicit as what you have. |
||
onToggleLike={onToggleLike} | ||
key={entry.id} | ||
/> | ||
); | ||
}); | ||
|
||
return ( | ||
<div className="chat-log"> | ||
{chatEntryComponents} | ||
</div> | ||
); | ||
}; | ||
|
||
ChatLog.propTypes = { | ||
entries: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
id: PropTypes.number.isRequired, | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool.isRequired, | ||
}) | ||
).isRequired, | ||
onToggleLike: PropTypes.func.isRequired, | ||
Comment on lines
+28
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⭐️ |
||
}; | ||
|
||
export default ChatLog; |
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!