-
Notifications
You must be signed in to change notification settings - Fork 153
Kemal - Tigers #130
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?
Kemal - Tigers #130
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,16 +1,30 @@ | ||
| import React from 'react'; | ||
| import React, { useState } from 'react'; | ||
| import './App.css'; | ||
| import chatMessages from './data/messages.json'; | ||
| import ChatEntry from './components/ChatEntry.js'; | ||
| import ChatLog from './components/ChatLog.js'; | ||
|
|
||
|
|
||
|
|
||
| const App = () => { | ||
| const [likesCount, setLikesCount] = useState(0); | ||
|
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. While this version of state rather cleverly avoids having a state variable of all the chat entries. There is a slight downside, though... Right now all the entries in messages.json have However, with this code, those likes would not appear in this number, and in fact, if you could unlike the message, the number would go negative. (In this case you can't "unlike" that message because the Of course, there's a simple solution to that, and that's to count the likes before making your call to And in that case, rather than having two state variables, one for the entries and one for the count of likes, it would likely be better to only maintain the one for the entries as the number of likes can be determined from the entries. Having redundant state like that is a common source for bugs as the state that should stay in sync falls out of sync. Not necessarily enough to be a bug, but often what a reviewer would call a "code smell". But we're well in hypothetical world at this point, just wanted to point that out. I think for our case your implementation is fine! |
||
| const updateLikes = (isLike) => { | ||
| if (isLike) { | ||
| setLikesCount(likesCount + 1); | ||
| } else { | ||
| setLikesCount(likesCount - 1) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h2>{likesCount} ❤️s</h2> | ||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog entries={chatMessages} updateLikes={updateLikes}/> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,44 @@ | ||
| import React from 'react'; | ||
| import './ChatEntry.css'; | ||
| import PropTypes from 'prop-types'; | ||
| import TimeStamp from './TimeStamp.js'; | ||
| import { useState } from 'react'; | ||
|
|
||
|
|
||
|
|
||
|
|
||
| const ChatEntry = (props) => { | ||
| const [likeButton, setLikeButton] = useState('🤍') | ||
|
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. As mentioned, since the like button setting gets set here to be the empty heart regardless of the actual value in But like I said above in |
||
| const toggleClicked = () => { | ||
| if (likeButton === '🤍' ) { | ||
| setLikeButton('❤️') | ||
| props.updateLikes(true) | ||
| } else { | ||
| setLikeButton('🤍'); | ||
| props.updateLikes(false) | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| return ( | ||
| <div className="chat-entry local"> | ||
| <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"> <TimeStamp time={props.timeStamp}/> </p> | ||
| <button className="like" onClick= {toggleClicked}>{likeButton}</button> | ||
| </section> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| // {props.updateLikes} | ||
| ChatEntry.propTypes = { | ||
| //Fill with correct proptypes | ||
| sender: PropTypes.string.isRequired, | ||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
|
Comment on lines
+39
to
+41
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 would make sure to include I would even go further and mark |
||
| }; | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import React from 'react'; | ||
| import ChatEntry from './ChatEntry'; | ||
|
|
||
|
|
||
|
|
||
| const ChatLog = (props) => { | ||
| const entries = props.entries; | ||
|
|
||
| return ( | ||
| <div className= 'chat-log'> | ||
| { | ||
| entries.map((entry,id) => ( | ||
| <ChatEntry | ||
| id={entry.id} | ||
| sender={entry.sender} | ||
| body={entry.body} | ||
| timeStamp={entry.timeStamp} | ||
| liked={entry.liked} | ||
| updateLikes={props.updateLikes} | ||
| key={id} | ||
| /> | ||
| ) | ||
| )} | ||
| </div> | ||
| ) | ||
| }; | ||
|
|
||
|
|
||
|
Comment on lines
+27
to
+28
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. There's no |
||
| 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.
Minor style: while this is more blank lines than I would normally use, there's nothing wrong with using more. I recommend, though, that you pick a consistent number of lines between elements. For example, there are three lines here between the
importsand the code, but inChatEntrythere are four.But of course, having some extra blank lines is a nanoscopic issue...