-
Notifications
You must be signed in to change notification settings - Fork 153
Snow Leopards - Ja H. #124
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
82a44a2
1c8886d
41d16cc
ca9e966
a817775
2693a95
37c9ba5
4a1420d
850fd04
14cd5dc
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,16 +1,41 @@ | ||
| import React from 'react'; | ||
| import './App.css'; | ||
| import ChatLog from './components/ChatLog'; | ||
| import chatMessages from './data/messages.json'; | ||
| import { useState } from 'react'; | ||
|
|
||
| const App = () => { | ||
| const [chatData, setChatData] = useState(chatMessages); | ||
|
|
||
| const updateChatData = (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 style of setter, since our next state for the chat data depends on the previous state of the chat data. |
||
| chatData.map((chat) => { | ||
|
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 |
||
| if (chat.id === id) { | ||
| return { ...chat, liked: !chat.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. 👍 Nice way to copy our modified message while also toggling the like, without modifying the previous state. We should always make a copy of any data being modified when working with React state. |
||
| } else { | ||
| return chat; | ||
| } | ||
| }) | ||
| ); | ||
| }; | ||
|
|
||
| const findTotalHearts = (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 helper function to tally up the liked message count from state we were already tracking, without the need for an additional piece of state. We could use |
||
| let total = 0; | ||
| for (const element of chatData) { | ||
| if (element.liked === true) { | ||
| total += 1; | ||
| } | ||
| } | ||
| return total; | ||
| }; | ||
|
|
||
| const allHearts = findTotalHearts(chatData); | ||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| </header> | ||
| <header>{allHearts} ❤️s</header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog entries={chatData} onUpdateChat={updateChatData}></ChatLog> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,20 +3,29 @@ import './ChatEntry.css'; | |
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatEntry = (props) => { | ||
| const heartType = props.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. Nice job using the liked value coming from props to update the component display. There's no need to track this with local state, since any time we update the state (using the supplied callback) the component data will update, which we will receive through updated props. |
||
|
|
||
| return ( | ||
| <div className="chat-entry 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. The optional alignment feature would require us to be able to pick whether to use the |
||
| <h2 className="entry-name">Replace with name of sender</h2> | ||
| <h2 className="entry-name">{props.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>{props.body}</p> | ||
| <p className="entry-time">{props.timeStamp}</p> | ||
|
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 passed in the |
||
| <button onClick={() => props.onUpdateChat(props.id)} className="like"> | ||
|
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 your passed-in callback to allow parent-owned logic to run when the like button is clicked. |
||
| {heartType} | ||
| </button> | ||
| </section> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| ChatEntry.propTypes = { | ||
| //Fill with correct proptypes | ||
| id: PropTypes.number.isRequired, | ||
|
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 prop types. Ideally, we would probably mark all of these props as required, but since there was some test weirdness with the props due to how the test data was provided, it's fine that they're not. In general, if a property isn't mark required, we should also ensure that our component works properly in its absence. For example, if the callback isn't supplied, we might avoid calling it, or even avoid rendering the heart as clickable at all. Or if the liked value isn't supplied, we might render the message without the liked indicator at all. |
||
| sender: PropTypes.string.isRequired, | ||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
| liked: PropTypes.bool, | ||
| onUpdateChat: PropTypes.func, | ||
| }; | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| .chat-log { | ||
| margin: auto; | ||
| max-width: 50rem; | ||
| /* background-color: [orchid]; */ | ||
|
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. To set the background color, this would be background-color: orchid;The string color name is bare, without any |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import ChatEntry from './ChatEntry'; | ||
| import './ChatLog.css'; | ||
|
|
||
| const ChatLog = (props) => { | ||
| return ( | ||
| <div className="chat-log"> | ||
| <ul> | ||
| {props.entries.map((chat) => ( | ||
|
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 |
||
| <ChatEntry | ||
| id={chat.id} | ||
| sender={chat.sender} | ||
| body={chat.body} | ||
| timeStamp={chat.timeStamp} | ||
| liked={chat.liked} | ||
| onUpdateChat={props.onUpdateChat} | ||
| key={chat.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. 👍 Good use of the message |
||
| /> | ||
| ))} | ||
| </ul> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| ChatLog.propTypes = { | ||
| chats: PropTypes.arrayOf( | ||
|
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 prop types look good. As with |
||
| PropTypes.shape({ | ||
| sender: PropTypes.string.isRequired, | ||
| id: PropTypes.number.isRequired, | ||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
| liked: PropTypes.bool, | ||
| }) | ||
| ), | ||
|
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. Add |
||
| onUpdateChat: 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.
👍 We need a piece of state to track changes to our messages, initialized from the json file we loaded.