-
Notifications
You must be signed in to change notification settings - Fork 35
Crow 24 - Tsz Tin Vivian Soo #20
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
0839ec6
cb8e69a
7fbc786
eeccf1e
8478048
22c12b3
55d5a88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,66 @@ | ||
| import './App.css'; | ||
| import DATA from './data/messages.json'; | ||
| import ChatLog from './components/ChatLog'; | ||
| import ColorChoice from './components/ColorChoice'; | ||
| import { useState } from 'react'; | ||
|
|
||
|
|
||
| const handleLikedChat = chat => { | ||
| return { ...chat, liked: !chat.liked}; | ||
| }; | ||
|
|
||
| const countLikes = chatData => { | ||
| return chatData.reduce((sum, chat) => { | ||
|
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. Though you will find a number of implementations for this, the |
||
| return sum + (chat.liked ? 1: 0); | ||
| }, 0); | ||
|
Comment on lines
+8
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. Nice work on these functions, especially with having them defined outside of the component since they aren't dependent on the locally scoped data like state. |
||
| }; | ||
|
|
||
| const App = () => { | ||
| const [chatData, setChatData] = useState(DATA); | ||
|
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 [localColor, setLocalColor] = useState(''); | ||
| const [remoteColor, setRemoteColor] = useState(''); | ||
|
Comment on lines
+20
to
+21
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. Perhaps there is a way to combine these two pieces of state into a single piece so we don't have manage 3 separate states. 👀 |
||
|
|
||
| const handleLikeClicked = id => { | ||
| setChatData(chatData => { | ||
| return chatData.map(chat => { | ||
| if (chat.id === id) { | ||
| return handleLikedChat(chat); | ||
| } else { | ||
| return chat; | ||
|
Comment on lines
+26
to
+29
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. Given the relative simplicity here, you could implement a ternary—foregoing the conditional branching which would result in more succinct code yet maintaining the same level of readability. |
||
| } | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| const totalLikes = countLikes(chatData); | ||
|
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 <span className={localColor}>Vladimir</span> and <span className={remoteColor}>Estragon</span> | ||
|
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. Currently, you have the names of the chat participants hardcoded, how would we refactor this to make it more dynamic and interchangeable? |
||
| </h1> | ||
| <div className="color-controls"> | ||
| <div className="color-picker"> | ||
| <span className={localColor}><b>Vladimir's color:</b> </span> | ||
| <ColorChoice onClickColor={setLocalColor} /> | ||
| </div> | ||
| <div> | ||
| <h2>{totalLikes} ❤️s</h2> | ||
| </div> | ||
| <div className="color-picker"> | ||
| <span className={remoteColor}><b>Estragon's color:</b> </span> | ||
| <ColorChoice onClickColor={setRemoteColor} /> | ||
|
Comment on lines
+46
to
+53
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. ✅ |
||
| </div> | ||
| </div> | ||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog | ||
| entries={chatData} | ||
| onLikeToggle={handleLikeClicked} | ||
|
Comment on lines
+59
to
+60
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. ✅ |
||
| localColor={localColor} | ||
| remoteColor={remoteColor} | ||
|
Comment on lines
+61
to
+62
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. ✅ |
||
| /> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,38 @@ | ||
| import './ChatEntry.css'; | ||
| import TimeStamp from './TimeStamp'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatEntry = (props) => { | ||
|
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 you can de-structure your props here! Though this does readily display that you are working with props and not local variables declared in the components. |
||
|
|
||
| const likeButtonClicked = () => { | ||
| props.onLikeToggle(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. 👍🏿 |
||
| }; | ||
|
|
||
| const likeButtonColor = 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. Great use of a ternary! |
||
| const isLocal = props.sender === 'Vladimir'; | ||
|
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. For the same reasons mentioned in |
||
| const colorClass = isLocal ? props.localColor : props.remoteColor; | ||
|
|
||
| const ChatEntry = () => { | ||
| 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> | ||
| <article className={`chat-entry ${isLocal ? 'local' : 'remote'}`}> | ||
| <h2 className="entry-name">{props.sender}</h2> | ||
| <section className={`entry-bubble ${colorClass}`}> | ||
| <p>{props.body}</p> | ||
| <p className="entry-time"><TimeStamp time={props.timeStamp}></TimeStamp></p> | ||
| <button className="like" onClick={likeButtonClicked}>{likeButtonColor}</button> | ||
|
Comment on lines
+16
to
+21
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. ✅ |
||
| </section> | ||
| </replace-with-relevant-semantic-element> | ||
| </article> | ||
| ); | ||
| }; | ||
|
|
||
| ChatEntry.propTypes = { | ||
| // Fill with correct proptypes | ||
| sender: PropTypes.string, | ||
| body: PropTypes.string, | ||
| timeStamp: PropTypes.string, | ||
| liked: PropTypes.bool, | ||
| id: PropTypes.number, | ||
| onLikeToggle: PropTypes.func, | ||
| localColor: PropTypes.string, | ||
| remoteColor: PropTypes.string, | ||
|
Comment on lines
+28
to
+35
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 these proptypes, remember that they have been sunsetted in React and that will be using other type checking frameworks like Typescript. |
||
| }; | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import PropTypes from 'prop-types'; | ||
| import ChatEntry from './ChatEntry'; | ||
|
|
||
| const ChatLog = (props) => { | ||
| const chatComponents = props.entries.map((entry) => { | ||
| return (<ChatEntry | ||
| key={entry.id} | ||
| id={entry.id} | ||
| sender={entry.sender} | ||
| body={entry.body} | ||
| timeStamp={entry.timeStamp} | ||
| liked={entry.liked} | ||
| onLikeToggle={props.onLikeToggle} | ||
| localColor={props.localColor} | ||
| remoteColor={props.remoteColor} | ||
|
Comment on lines
+7
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. Great work, especially on the formatting. To simplify your logic, you could pass in the entire message object yourself. However, you would lose out on the explicitness you have here. 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 for |
||
| /> | ||
| ); | ||
| }); | ||
|
|
||
| return <ul className="chat-log">{chatComponents}</ul>; | ||
|
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. ✅ |
||
| }; | ||
|
|
||
| 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, | ||
| localColor: PropTypes.string.isRequired, | ||
| remoteColor: PropTypes.string.isRequired, | ||
| }; | ||
|
Comment on lines
+23
to
+34
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; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| const ColorChoice = (props) => { | ||
| return ( | ||
| <> | ||
| <button className='red' onClick={() => props.onClickColor('red')}>🔴</button> | ||
| <button className='orange' onClick={() => props.onClickColor('orange')}>🟠</button> | ||
| <button className='yellow' onClick={() => props.onClickColor('yellow')}>🟡</button> | ||
| <button className='green' onClick={() => props.onClickColor('green')}>🟢</button> | ||
| <button className='blue' onClick={() => props.onClickColor('blue')}>🔵</button> | ||
| <button className='purple' onClick={() => props.onClickColor('purple')}>🟣</button> | ||
| </> | ||
|
Comment on lines
+3
to
+10
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. Hmmm, looks like there is a lot of repeating oneself here... how could this be DRY'd up? 🤔 |
||
| ); | ||
| }; | ||
|
|
||
| export default ColorChoice; | ||
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.