-
Notifications
You must be signed in to change notification settings - Fork 44
C22_Sphinx_Rong Jiang #28
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,46 @@ | ||
| import { useState } from 'react'; | ||
| import './App.css'; | ||
| import ChatLog from './components/ChatLog'; | ||
| import messages from './data/messages.json' | ||
|
|
||
|
|
||
| // import data from messages.json | ||
| const LOG = messages; | ||
|
|
||
|
|
||
| const App = () => { | ||
| const [entries, setEntries] = useState(LOG); | ||
|
|
||
| // update chat entry => like change | ||
| const toggleLike = (id) => { | ||
| const updatedEntries = entries.map(entry => { | ||
| if (entry.id === id) { | ||
| // toggle like | ||
| return {... entry, liked: !entry.liked}; | ||
| } else { | ||
| // no change | ||
| return entry; | ||
| } | ||
| }); | ||
| // update entries | ||
| setEntries(updatedEntries); | ||
| } | ||
|
|
||
| const totalLikes = entries.filter(entry => entry.liked).length; | ||
|
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. Typically, you'd see const totalLikes = entries.reduce((totalLikes, entry) => (totalLikes + entry.liked); |
||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1>Chat Between {entries[0].sender} and {entries[1].sender} </h1> | ||
| <h2 id="heartWidget">{totalLikes} ❤️s </h2> | ||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <div> | ||
| <ChatLog | ||
| entries={entries} | ||
| onLikeToggle={toggleLike} | ||
| /> | ||
| </div> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,20 +1,44 @@ | ||||||||||||||||||||||
| import './ChatEntry.css'; | ||||||||||||||||||||||
| import PropTypes from 'prop-types'; | ||||||||||||||||||||||
| import TimeStamp from './TimeStamp'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const ChatEntry = ({id, sender, body, timeStamp,liked, onLikeToggle}) => { | ||||||||||||||||||||||
|
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.
Suggested change
Spaces around curly braces when destructuring |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
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. Linter says "Block must not be padded by blank lines". Remove line 6
Suggested change
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. |
||||||||||||||||||||||
| const handleHeartClick = () => { | ||||||||||||||||||||||
| onLikeToggle(id); | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| let isLocalUser = false | ||||||||||||||||||||||
|
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.
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (sender === 'Vladimir') { | ||||||||||||||||||||||
| isLocalUser = true; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
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.
Suggested change
Linter calls out that you're missing semi-colons. if you haven't configured your linter, you might want to do that so it can highlight syntax/style issues.
Comment on lines
+11
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. Would prefer to have this logic put in a method and the method get called somewhere.
Suggested change
Then on line 18, you can call the function: <div className={`chat-entry ${setLocalOrRemote()}`}> |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const ChatEntry = () => { | ||||||||||||||||||||||
| return ( | ||||||||||||||||||||||
| <div className="chat-entry local"> | ||||||||||||||||||||||
| <h2 className="entry-name">Replace with name of sender</h2> | ||||||||||||||||||||||
| <div className={isLocalUser?'chat-entry local':'chat-entry remote'}> | ||||||||||||||||||||||
| <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 className='body'>{body}</p> | ||||||||||||||||||||||
| <TimeStamp time = {timeStamp}></TimeStamp> | ||||||||||||||||||||||
|
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 provided Timestamp component here. Convention is to not have spaces around
Suggested change
|
||||||||||||||||||||||
| <button | ||||||||||||||||||||||
| className='like' //className from test | ||||||||||||||||||||||
|
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. Prefer not to have comments in code unless absolutely necessary. |
||||||||||||||||||||||
| onClick={handleHeartClick}> | ||||||||||||||||||||||
| {liked ? '❤️' : '🤍'} | ||||||||||||||||||||||
| </button> | ||||||||||||||||||||||
| </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, | ||||||||||||||||||||||
| onLikeToggle: PropTypes.func.isRequired, | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| export default ChatEntry; | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,37 @@ | ||||||
| import './ChatLog.css'; | ||||||
| import PropTypes from 'prop-types'; | ||||||
| import ChatEntry from './ChatEntry'; | ||||||
|
|
||||||
|
|
||||||
| const ChatLog = ({entries, onLikeToggle}) => { | ||||||
|
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.
Suggested change
|
||||||
| return ( | ||||||
| <div className="chat-log"> | ||||||
| {entries.map(entry => ( | ||||||
| <ChatEntry | ||||||
| key={entry.id} | ||||||
| id={entry.id} | ||||||
| sender={entry.sender} | ||||||
| body={entry.body} | ||||||
| timeStamp={entry.timeStamp} | ||||||
| liked={entry.liked} | ||||||
| onLikeToggle={onLikeToggle} | ||||||
| /> | ||||||
| ))} | ||||||
| </div> | ||||||
| ); | ||||||
| }; | ||||||
|
Comment on lines
+7
to
+22
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 more typical to see the result of using const chatLogs = entries.map((entry) => {
return (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
handleLike={handleLike}
currentUser={currentUser}
/>
);
});
};
return (
<div className="chat-log">
{chatLogs}
</div>
); |
||||||
|
|
||||||
| 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, | ||||||
| onLikeToggle: 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.
You can directly name
messagesasLOGon line 4.Be sure to terminate a statement with semi-colons.