-
Notifications
You must be signed in to change notification settings - Fork 39
🐲🪰 Mikaela B. #17
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?
🐲🪰 Mikaela B. #17
Changes from all commits
f242789
2fb7dab
02faa64
a6590cc
f05ae5a
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,14 +1,50 @@ | ||||||||||||||
| // Wave 1: Import ChatEntry to show one message | ||||||||||||||
| // import ChatEntry from './components/ChatEntry'; | ||||||||||||||
| import './App.css'; | ||||||||||||||
| import messages from './data/messages.json'; | ||||||||||||||
| import ChatLog from './components/ChatLog'; | ||||||||||||||
| import { useState } from 'react'; | ||||||||||||||
|
|
||||||||||||||
| const calculateTotalLikes = (chatData) => { | ||||||||||||||
| return chatData.reduce((total, message) => { | ||||||||||||||
| return total + (message.liked ? 1 : 0); | ||||||||||||||
| }, 0); | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| const App = () => { | ||||||||||||||
| // Wave 1: Use first message data to render one ChatEntry | ||||||||||||||
| // const chatMessages = messages[0]; | ||||||||||||||
|
|
||||||||||||||
| const [chatData, setChatData] = useState(messages); | ||||||||||||||
| const totalLikes = calculateTotalLikes(chatData); | ||||||||||||||
|
|
||||||||||||||
| const toggleLike = (id) => { | ||||||||||||||
| setChatData((chatData) => | ||||||||||||||
|
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 callback setter style. In this application, it doesn't really matter whether we use the callback style or the value style, but it's good practice to get in the habit of using the callback style. |
||||||||||||||
| chatData.map((entry) => { | ||||||||||||||
|
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 map here to both handle making a new list so that React sees the message data has changed, and make new data for the clicked message with its like status toggled. |
||||||||||||||
| if (entry.id === id) { | ||||||||||||||
| return { ...entry, liked: !entry.liked }; | ||||||||||||||
|
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 showed this approach in class, but technically, we're mixing a few responsibilities here. rather than this function needing to know how to change the liked status itself, we could move this update logic to a helper function. This would better mirror how we eventually update records when there's an API call involved. In this project, our messages are very simple objects, but if we had more involved operations, it could be worthwhile to create an actual class with methods to work with them, or at least have a set of dedicated helper functions to centralize any such mutation logic. |
||||||||||||||
| } else { | ||||||||||||||
| return entry; | ||||||||||||||
| } | ||||||||||||||
| }) | ||||||||||||||
| ); | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| return ( | ||||||||||||||
| <div id="App"> | ||||||||||||||
| <header> | ||||||||||||||
| <h1>Application title</h1> | ||||||||||||||
| <h1>iYap</h1> | ||||||||||||||
|
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. 🤗 |
||||||||||||||
| <h2>{totalLikes} 💖s</h2> | ||||||||||||||
| </header> | ||||||||||||||
| <main> | ||||||||||||||
| {/* Wave 01: Render one ChatEntry component | ||||||||||||||
| Wave 02: Render ChatLog component */} | ||||||||||||||
| {/* Wave 1: Rendered one ChatEntry | ||||||||||||||
| <ChatEntry | ||||||||||||||
| sender={chatMessages.sender} | ||||||||||||||
| body={chatMessages.body} | ||||||||||||||
| timeStamp={chatMessages.timeStamp} | ||||||||||||||
| /> */} | ||||||||||||||
|
Comment on lines
+40
to
+45
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. This comment might be confusing to other devs because it might make someone wonder if they should uncomment lines 41-44 in order for the ChatEntry component to render. If you want to keep track of what pieces of data a component should have and how the data should flow between components, creating a diagram or documenting it in the project README could be good places for that and would also leave your code uncluttered with comments.
Suggested change
|
||||||||||||||
| {/* Wave 2: Render all messages using ChatLog */} | ||||||||||||||
| <ChatLog entries={chatData} onLike={toggleLike} /> | ||||||||||||||
| </main> | ||||||||||||||
| </div> | ||||||||||||||
| ); | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,34 @@ | ||
| import './ChatEntry.css'; | ||
| import TimeStamp from './TimeStamp'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatEntry = ({ id, sender, body, timeStamp, liked, onLike }) => { | ||
| const heartIcon = liked ? '💖' : '🤍'; | ||
| const isLocal = sender === 'Chip'; | ||
|
|
||
| const ChatEntry = () => { | ||
| return ( | ||
| <div className="chat-entry local"> | ||
| <h2 className="entry-name">Replace with name of sender</h2> | ||
| <div className={`chat-entry ${isLocal ? 'local' : 'remote'}`}> | ||
| <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} /> | ||
|
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 making use of the provided |
||
| </p> | ||
| <button className="like" onClick={() => onLike(id)}> | ||
| {heartIcon} | ||
| </button> | ||
| </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, | ||
| onLike: PropTypes.func.isRequired, | ||
| }; | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import './ChatLog.css'; | ||
| import ChatEntry from './ChatEntry'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatLog = ({ entries, onLike }) => { | ||
| return ( | ||
| <div className="chat-log"> | ||
| {entries.map((entry) => ( | ||
| <ChatEntry | ||
| key={entry.id} | ||
| id={entry.id} | ||
| sender={entry.sender} | ||
| body={entry.body} | ||
| timeStamp={entry.timeStamp} | ||
| liked={entry.liked} | ||
| onLike={onLike} | ||
| /> | ||
| ))} | ||
| </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, | ||
| onLike: PropTypes.func.isRequired, | ||
| }; | ||
|
|
||
| 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
reduce! This is how most JS devs would go about calculating a total amount too.