-
Notifications
You must be signed in to change notification settings - Fork 35
Mengqiao Han Chatlog #26
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
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 chatdata from './data/messages.json'; | ||
| import { useState } from 'react'; | ||
| import ChatLog from './components/Chatlog'; | ||
| //import ChatEntry from './components/ChatEntry'; | ||
|
|
||
|
|
||
| const App = () => { | ||
| const [chatData, setChats] = useState(chatdata); | ||
| const toggleLike = (id) => { | ||
| setChats(chatData => | ||
| chatData.map(chat =>{ | ||
| if (chat.id === id) { | ||
| return{ ...chat, liked: !chat.liked }; | ||
| } else { | ||
| return chat; | ||
| } | ||
| } | ||
| ) | ||
| ); | ||
| }; | ||
|
|
||
| const totalLikes = () => { | ||
| return chatData.reduce((count, chat) => { | ||
| return chat.liked ? count + 1 : count; | ||
| }, 0); | ||
| }; | ||
|
Comment on lines
+23
to
+27
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 on this function implementation, @Bipentium25. Using the |
||
|
|
||
| const local = chatData[0].sender; | ||
| const remote = chatData.find(chat => chat.sender !== local).sender; | ||
|
Comment on lines
+29
to
+30
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. ✅ |
||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1>Chat Between {local} and {remote}</h1> | ||
| <section><h2 className='widget'>{totalLikes()} ❤️s</h2></section> | ||
|
Comment on lines
+35
to
+36
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. 👍🏿 |
||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog entries={chatData} toggleLike={toggleLike} local={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. Nice! I see that you leveraged a |
||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,31 @@ | ||
| import './ChatEntry.css'; | ||
| import PropTypes from 'prop-types'; | ||
| import TimeStamp from './TimeStamp'; | ||
|
|
||
| const ChatEntry = () => { | ||
| const ChatEntry = ({ id, sender, body, timeStamp, liked, toggleLike = () => {}, isLocal = false }) => { | ||
| const heart = 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. Perfect use case for a ternary operator! This is how we implement conditional rendering. |
||
| return ( | ||
| // Replace the outer tag name with a semantic element that fits our use case | ||
| <replace-with-relevant-semantic-element className="chat-entry local"> | ||
| <h2 className="entry-name">Replace with name of 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> | ||
| <div className={`chat-entry ${isLocal ? 'local' : 'remote'}`}> | ||
|
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. Another great use of ternary operators! |
||
| <h2 className='entry-name'>{sender}</h2> | ||
| <section className='entry-bubble'> | ||
| <p>{body}</p> | ||
| <p className='entry-time'><TimeStamp time={timeStamp}></TimeStamp></p> | ||
|
Comment on lines
+10
to
+13
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. 👌 |
||
| <button className='like' onClick={() => toggleLike(id)}>{heart}</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. Perfect! |
||
| </section> | ||
| </replace-with-relevant-semantic-element> | ||
| </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, | ||
| toggleLike: PropTypes.func.isRequired, | ||
| isLocal: PropTypes.bool, | ||
|
|
||
|
Comment on lines
+21
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. Don't forget that in React PropTypes have been sunsetted and you will want to use Typescript in practice now! |
||
| }; | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||||||||||||||||||
| import ChatEntry from './ChatEntry'; | ||||||||||||||||||||||||||
| import './ChatLog.css'; | ||||||||||||||||||||||||||
| import PropTypes from 'prop-types'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const ChatLog = ({entries, toggleLike = () => {}, 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. @Bipentium25, interested to know why you chose to implement 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. Oh I see now, is it to more readily show that
Author
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 had a problem back then when I was uploading the chatlog for test on learn. And I had a error message "Warning: Failed prop type: The prop toggleLike is marked as required in ChatLog, but its value is undefined." and was suggested doing this by chatgpt" |
||||||||||||||||||||||||||
| const chatlogFromData = entries.map(chatEntry => { | ||||||||||||||||||||||||||
| return( | ||||||||||||||||||||||||||
| <ul key={chatEntry.id}> | ||||||||||||||||||||||||||
| <ChatEntry | ||||||||||||||||||||||||||
| id = {chatEntry.id} | ||||||||||||||||||||||||||
| sender = {chatEntry.sender} | ||||||||||||||||||||||||||
| body = {chatEntry.body} | ||||||||||||||||||||||||||
| timeStamp = {chatEntry.timeStamp} | ||||||||||||||||||||||||||
| liked = {chatEntry.liked} | ||||||||||||||||||||||||||
| toggleLike={toggleLike} | ||||||||||||||||||||||||||
|
Comment on lines
+10
to
+15
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. Convention is have no whitespace surrounding your
Suggested change
|
||||||||||||||||||||||||||
| isLocal={chatEntry.sender === 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. 👍🏿 |
||||||||||||||||||||||||||
| </ChatEntry> | ||||||||||||||||||||||||||
| </ul> | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| return( | ||||||||||||||||||||||||||
| <ul className="chatlog"> | ||||||||||||||||||||||||||
| {chatlogFromData} | ||||||||||||||||||||||||||
|
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. ✅ |
||||||||||||||||||||||||||
| </ul> | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
| 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, | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| toggleLike: PropTypes.func.isRequired, | ||||||||||||||||||||||||||
| local: PropTypes.string.isRequired, | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
Comment on lines
+27
to
+40
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. ✅ |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| 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.