-
Notifications
You must be signed in to change notification settings - Fork 44
Phoenix_Marjana #30
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?
Phoenix_Marjana #30
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,14 +1,28 @@ | ||
| import './App.css'; | ||
| import ChatLog from './components/ChatLog'; | ||
| import message from './data/messages.json'; | ||
| import {useState} from 'react'; | ||
|
|
||
| const App = () => { | ||
| const [chatData, setChatData] = useState(message); | ||
| const togglelikes = (id) => { | ||
| setChatData( chatData => chatData.map(chat => { | ||
| if (chat.id === id){ | ||
| return {...chat, liked: !chat.liked}; | ||
| }else { | ||
| return chat; | ||
|
Comment on lines
+10
to
+13
Collaborator
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 managing of data & making sure we return a new object when we need to alter a message in our list. |
||
| } | ||
| })); | ||
| }; | ||
| const totalLikes = chatData.filter((chat) => chat.liked).length; | ||
|
Collaborator
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. Great work calculating the hearts count from the chatsData! Since we don't need the contents of the array we get from // This could be returned from a helper function
// likesCount is a variable that accumulates a value as we loop over each entry in chatData
const totalLikes = chatData.reduce((likesCount, currentMessage) => {
// If currentMessage.liked is true add 1 to likesCount, else add 0
return (likesCount += currentMessage.liked ? 1 : 0);
}, 0); // The 0 here sets the initial value of likesCount to 0 |
||
| return ( | ||
|
Comment on lines
+7
to
18
Collaborator
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. Please use blank lines to break up sections of code so it's easy as a reader to see where different areas of responsibility in the file are. It can also help flow and ease of reading to organize variables together, followed by functions, then the return value of the file. A possible reorganization could look like: const [chatData, setChatData] = useState(message);
const totalLikes = chatData.filter((chat) => chat.liked).length;
const togglelikes = (id) => {
setChatData( chatData => chatData.map(chat => {
if (chat.id === id){
return {...chat, liked: !chat.liked};
}else {
return chat;
}
}));
};
return ( |
||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1>Chat Log</h1> | ||
| <p>{totalLikes} ❤️s </p> | ||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog entries={chatData} chatClicked={togglelikes}/> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,30 @@ | ||
| import './ChatEntry.css'; | ||
| import TimeStamp from './TimeStamp'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatEntry = () => { | ||
| const ChatEntry = ({id,sender,body,timeStamp,liked,chatClicked}) => { | ||
| const heartColor = liked ? '❤️' : '🤍'; | ||
| return ( | ||
| <div className="chat-entry local"> | ||
|
Collaborator
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 wanted to implement the local and remote styles, we could use another ternary operator to determine our |
||
| <h2 className="entry-name">Replace with name of sender</h2> | ||
| <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}/> | ||
| </p> | ||
| <button className="like" onClick={() => chatClicked(id)}>{heartColor}</button> | ||
|
Collaborator
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 this pattern of sending just the |
||
| </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.string.isRequired, | ||
|
Collaborator
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 take a look in the
What does this error tell us? |
||
| chatClicked: PropTypes.func.isRequired, | ||
| }; | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import ChatEntry from './ChatEntry'; | ||
| import PropTypes from 'prop-types'; | ||
| import './ChatLog.css'; | ||
|
|
||
| const ChatLog = ({entries, chatClicked}) => { | ||
| const chatComponents = entries.map((chat) => { | ||
|
Collaborator
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 mapping! 🙌🏻 |
||
| return ( | ||
| <li key={chat.id}> | ||
|
Collaborator
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. Rather than wrapping our ChatEntry in a |
||
| <ChatEntry | ||
| id={chat.id} | ||
| sender={chat.sender} | ||
| body={chat.body} | ||
| timeStamp={chat.timeStamp} | ||
| chatClicked={chatClicked} | ||
| liked={chat.liked} | ||
| /> | ||
| </li> | ||
| ); | ||
| }); | ||
| return ( | ||
| <> | ||
| {chatComponents} | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| ChatLog.propTypes = { | ||
| entries: PropTypes.arrayOf(PropTypes.shape({ | ||
|
Collaborator
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 |
||
| id: PropTypes.number.isRequired, | ||
| sender: PropTypes.string.isRequired, | ||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
| })).isRequired, | ||
| chatClicked: 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.