-
Notifications
You must be signed in to change notification settings - Fork 35
C24 Possum Li Yan #23
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
base: main
Are you sure you want to change the base?
Changes from all commits
41498e5
b33012a
6a95c9b
fcc7035
c2f2bfe
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,48 @@ | ||
| import './App.css'; | ||
| import messages from './data/messages.json'; | ||
| import ChatLog from './components/ChatLog'; | ||
| import { useState } from 'react'; | ||
|
|
||
| const SENDER_COLORS = { | ||
| Vladimir: 'red', | ||
| Estragon: 'green', | ||
| }; | ||
|
|
||
| const App = () => { | ||
| const [chatMessages, setChatMessages] = useState(messages); | ||
|
|
||
| const toggleLikeChoice = (id) => { | ||
| setChatMessages(chatMessages => { | ||
| return chatMessages.map(message => { | ||
| if (message.id === id) { | ||
| return { ...message, liked: !message.liked }; | ||
| } else { | ||
| return message; | ||
| } | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| const countLikes = chatMessages.filter(msg => msg.liked).length; | ||
|
|
||
| const participants = Array.from(new Set(chatMessages.map(msg => msg.sender))); | ||
| const headerTitle = `Chat between ${participants.join(' and ')}`; | ||
|
Comment on lines
+28
to
+29
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. I like that you dynamically calculate the participants names for the header. This could accommodate a situation where the senders are not named Vladimir or Estragon. How might you use |
||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1 className="header">{headerTitle}</h1> | ||
| <section id="heartWidget" className="widget">{countLikes} ❤️s</section> | ||
|
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.
|
||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <main className="main"> | ||
| <ChatLog | ||
| entries={chatMessages} | ||
| toggleLikeChoice={toggleLikeChoice} | ||
| senderColors={SENDER_COLORS} | ||
| /> | ||
| </main> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default App; | ||
| export default App; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,32 @@ | ||
| import './ChatEntry.css'; | ||
| import PropTypes from 'prop-types'; | ||
| import TimeStamp from './TimeStamp'; | ||
|
|
||
| const ChatEntry = () => { | ||
| const ChatEntry = ({ id, sender, body, timeStamp, toggleLikeChoice, liked, color }) => { | ||
|
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. Nice job destructuring the variables instead of just passing |
||
| const isLocal = sender === 'Vladimir'; | ||
| return ( | ||
| // Replace the outer tag name with a semantic element that fits our use case | ||
| <replace-with-relevant-semantic-element className="chat-entry local"> | ||
| <h2 className="entry-name">Replace with name of 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> | ||
| <article className={`chat-entry ${isLocal ? 'local' : 'remote'}`}> | ||
|
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. Nice work computing the value of |
||
| <h2 className="entry-name">{sender}</h2> | ||
| <section className={`entry-bubble ${color}`}> | ||
| <p>{body}</p> | ||
| <span className="entry-time"> | ||
| <TimeStamp time={timeStamp} /> | ||
|
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. Nice use of the |
||
| </span> | ||
| <button className="like" onClick={() => toggleLikeChoice(id)}>{liked ? '❤️' : '🤍'}</button> | ||
|
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. 👍 Passing the |
||
| </section> | ||
| </replace-with-relevant-semantic-element> | ||
| </article> | ||
| ); | ||
| }; | ||
|
|
||
| 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, | ||
| toggleLikeChoice: PropTypes.func, | ||
| color: PropTypes.string | ||
| }; | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||
| import ChatEntry from './ChatEntry'; | ||||||
| import './ChatLog.css'; | ||||||
| import PropTypes from 'prop-types'; | ||||||
|
|
||||||
| const ChatLog = ({ entries, toggleLikeChoice = () => {}, senderColors = {} }) => { | ||||||
|
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. We should pass a function as a prop to a child component just by variable name (like how you did for in the We don't need to provide a default no-op function. We might use no-op functions when we build a library that other developers will use in their programs and that library surfaces an option for developer-supplied functions. Here, if no function is passed then our component shouldn't work. The functions passed as props are part of the component’s contract and the parent is responsible for passing it. If it’s missing, that’s a usage error rather than something to silently ignore.
Suggested change
|
||||||
| const chatEntryComponents = entries.map(entry => { | ||||||
| return ( | ||||||
| <ChatEntry | ||||||
| key={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. 👍 The |
||||||
| id={entry.id} | ||||||
| sender={entry.sender} | ||||||
| body={entry.body} | ||||||
| timeStamp={entry.timeStamp} | ||||||
| toggleLikeChoice={toggleLikeChoice} | ||||||
| liked={entry.liked} | ||||||
| color={senderColors?.[entry.sender]} | ||||||
| /> | ||||||
| ); | ||||||
| }); | ||||||
|
|
||||||
| return ( | ||||||
| <section className="chat-log"> | ||||||
| {chatEntryComponents} | ||||||
| </section> | ||||||
| ); | ||||||
| }; | ||||||
|
|
||||||
| 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, | ||||||
| toggleLikeChoice: PropTypes.func, | ||||||
| senderColors: PropTypes.object, | ||||||
| }; | ||||||
|
|
||||||
| 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 job using the callback-style for updating the state.