-
Notifications
You must be signed in to change notification settings - Fork 45
C22 Phoenix - Liubov D. #22
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
b2205c4
c436ffa
2542b4b
867ddf2
cb0a381
65cd2f1
c6b9f45
358bbba
53a5705
d9eb229
19d6bad
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,42 @@ | ||
import './App.css'; | ||
import ChatLog from './components/ChatLog'; | ||
import chatLogDataFromFile from '../src/data/messages.json'; | ||
import { useState } from 'react'; | ||
|
||
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. 👀 It's not necessary to introduce additional state to track the total number of likes, and really we would prefer to avoid doing so. The total number of likes can be calculated from the message data, since each message tracks whether it's liked. The total number of likes is an aggregate property of the message data. It can be tempting to introduce additional state since it can seem inefficient to iterate over all the message data just to calculate that value. But keep in mind that we have to do this anyway just to render the message data into components, so the additional cost is minimal, and avoids potential problems that could lead to states getting out of sync. For a small application like this, the risk is small, but as apps get more complex, we should look for ways to reduce the state used as much as possible. |
||
|
||
const [chatLogData, setChatLogData] = useState(chatLogDataFromFile); | ||
|
||
const toggleChatEntryLiked = (chatEntryId) => { | ||
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 our state is defined here, we also need to define our mutating function here so that it can "see" the setter function. All we need to receive is the |
||
const chatLogDataAfterToggle = chatLogData.map(chatEntry => { | ||
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 (chatEntry.id === chatEntryId) { | ||
if (chatEntry.liked) { | ||
setLikesCount(likesCount - 1); | ||
} else { | ||
setLikesCount(likesCount + 1); | ||
} | ||
Comment on lines
+14
to
+18
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. If we exclude the additional state to track the total likes, we wouldn't need this logic here. We would need a similar check in whatever logic we use to calculate the total on the next render, but being able to remove the extra state makes moving the logic worth it. |
||
return { ...chatEntry, liked: !chatEntry.liked }; | ||
} else { | ||
return chatEntry; | ||
} | ||
}); | ||
|
||
setChatLogData(chatLogDataAfterToggle); | ||
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. In this case, calculating the next version of the message data using the current state variable and passing that updated version to the state setter shouldn't cause any problems, but we still generally prefer using the callback style of setters. Using that approach, we pass a function to the setter whose parameter will receive the latest state value, and which returns the new value to use for the state. setChatLogData(chatData => chatLogData.map(chatEntry => {
if (chatEntry.id === id) {
return {...chatEntry, liked: !chatEntry.liked};
} else {
return chatEntry;
};
})); |
||
}; | ||
|
||
return ( | ||
<div id="App"> | ||
<header> | ||
<h1>Application title</h1> | ||
<h1> | ||
Chat between Vladimir and Estragon | ||
<br /> | ||
<br /> | ||
{likesCount} ❤️s | ||
</h1> | ||
</header> | ||
<main> | ||
{/* Wave 01: Render one ChatEntry component | ||
Wave 02: Render ChatLog component */} | ||
<ChatLog entries={chatLogData} onToggleChatEntryLiked={toggleChatEntryLiked}></ChatLog> | ||
</main> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,35 @@ | ||
import './ChatEntry.css'; | ||
import TimeStamp from './TimeStamp.jsx'; | ||
import PropTypes from 'prop-types'; | ||
import { useState } from 'react'; | ||
|
||
const ChatEntry = (props) => { | ||
const [isLiked, setIsLiked] = useState(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. 👀 We don't need (and shouldn't make) an additional piece of state to copy the |
||
|
||
const likeButtonClicked = () => { | ||
setIsLiked(!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. 👀 This logic is only required because of the local state used for the |
||
props.onToggleLiked(props.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. 👍 Passing the |
||
}; | ||
|
||
const ChatEntry = () => { | ||
return ( | ||
<div className="chat-entry local"> | ||
<h2 className="entry-name">Replace with name of sender</h2> | ||
<div className={props.sender == 'Vladimir' ? 'chat-entry local' : 'chat-entry remote'} id={props.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. Nice logic to decide whether to treat a message as local or remote. How could we generalize this so that it didn't need to look explicitly for Vladimir? This project only passes in a single conversation, but ideally, our components should work in other situations. In the general case, does the |
||
<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}></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. Nice use of the supplied Note that the <TimeStamp time={props.timeStamp} /> |
||
<button className="like" onClick={likeButtonClicked}>{isLiked ? '❤️' : '🤍'}</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. 👀 We can figure out which emoji to use for the liked status based on the <button className="like" onClick={likeButtonClicked}>{props.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. 👍 We need a wrapper of some kind rather than calling the received callback through props, since our callback function is expecting a message id as its parameter. If we tried to use it directly as the click event handler, React would end up passing it a clink event, since any function registered as an event handler will always be given the event detail information as its argument. |
||
</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, | ||
onToggleLiked: PropTypes.func | ||
Comment on lines
+27
to
+32
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 The remaining props were up to you, and the tests don't know about them. As a result, using To properly mark any other props Alternatively, for any props that we leave not required, we should also have logic in our component to not try to use the value if it's undefined. |
||
}; | ||
|
||
export default ChatEntry; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import ChatEntry from './ChatEntry'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const ChatLog = (props) => { | ||
const chatEntryComponents = props.entries.map(chatEntry => { | ||
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 |
||
return ( | ||
<ChatEntry | ||
key={chatEntry.id} | ||
id={chatEntry.id} | ||
Comment on lines
+8
to
+9
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 |
||
sender={chatEntry.sender} | ||
body={chatEntry.body} | ||
timeStamp={chatEntry.timeStamp} | ||
liked={chatEntry.liked} | ||
onToggleLiked={props.onToggleChatEntryLiked} | ||
></ChatEntry> | ||
); | ||
}); | ||
|
||
return <>{chatEntryComponents}</>; | ||
}; | ||
|
||
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 | ||
}) | ||
), | ||
onToggleChatEntryLiked: PropTypes.func | ||
}; | ||
|
||
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.
This file is alreasy in
src
, so we don't need to move up a folder and come back down again