-
Notifications
You must be signed in to change notification settings - Fork 17
Monica --- Ocelots #6
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
d6ff761
310377e
4731886
b6acad3
838c018
a425ea1
be73175
4f1a56c
caac5d7
60796de
2fc427f
5806689
d4cf21c
7bc579a
b6cd3e4
8e12c17
1392545
9b9fc2b
33c00c0
f4f72af
8421b75
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,16 +1,37 @@ | ||
| import React from 'react'; | ||
| import React, {useState} from 'react'; | ||
| import './App.css'; | ||
| import ChatLog from './components/ChatLog'; | ||
| import chatMessages from './data/messages.json'; | ||
|
|
||
| const App = () => { | ||
| const [chatEntries, setChatEntries] = useState(chatMessages); | ||
| const [numberLikes, setNumberLikes] = useState(0); | ||
|
|
||
| const updateChatEntry = (chatToUpdate) => { | ||
| const chats = chatEntries.map((entry) => { | ||
| if(entry.id === chatToUpdate.id){ | ||
| setNumberLikes(chatToUpdate.liked ? numberLikes+1 : numberLikes-1) | ||
| return chatToUpdate; | ||
| } | ||
| return entry; | ||
| }); | ||
| setChatEntries(chats); | ||
| }; | ||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <div className='applicationTitle'> | ||
| <h1>Chat between <span className='green'>Vladimir</span> and <span className='blue'>Estragon</span> </h1> | ||
|
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 is a little bit of personal taste, but I suggest dropping the spans to new, indented lines to make it easier to see at a glance what is nested inside the |
||
| </div> | ||
| <div className='numberOfLikes'> | ||
| <h1 className='numberLikes'>{numberLikes} ❤️s</h1> | ||
| </div> | ||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog entries = {chatEntries} | ||
| onUpdateChat = {updateChatEntry} | ||
| /> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,44 @@ | ||
| import React from 'react'; | ||
| import './ChatEntry.css'; | ||
| import PropTypes from 'prop-types'; | ||
| import TimeStamp from './TimeStamp'; | ||
|
|
||
| const ChatEntry = ({id, sender, body, timeStamp, onUpdateChat, liked}) => { | ||
| const updateClickState = () => { | ||
| onUpdateChat({ | ||
| id, | ||
| sender, | ||
| body, | ||
| timeStamp, | ||
| liked : !liked | ||
| }) | ||
| } | ||
|
Comment on lines
+6
to
+14
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 would consider passing the This made me think of a related concept in secure design for APIs. Imagine we had an API for creating and updating messages, and it has an endpoint |
||
| const heartStyle = liked ? '❤️' : '🤍'; | ||
| let entryClass = '' | ||
|
|
||
| if(sender === 'Vladimir'){ | ||
| entryClass = 'local'; | ||
| }else{ | ||
| entryClass = 'remote'; | ||
| } | ||
|
Comment on lines
+18
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. This is a great place for a javascript ternary operator: const entryClass = (sender === 'Vladimir') ? 'local' : 'remote'; |
||
|
|
||
| const ChatEntry = (props) => { | ||
| return ( | ||
| <div className="chat-entry local"> | ||
| <h2 className="entry-name">Replace with name of sender</h2> | ||
| <div className={`chat-entry ${entryClass}`}> | ||
| <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={updateClickState}>{heartStyle}</button> | ||
| </section> | ||
| </div> | ||
| ); | ||
| }; | ||
| )}; | ||
|
|
||
| ChatEntry.propTypes = { | ||
| //Fill with correct proptypes | ||
| id: PropTypes.number, | ||
| sender: PropTypes.string, | ||
| body: PropTypes.string, | ||
| timeStamp: PropTypes.string, | ||
| liked: PropTypes.bool, | ||
| onUpdateChat:PropTypes.func | ||
| }; | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import PropTypes from 'prop-types'; | ||
| import ChatEntry from './ChatEntry'; | ||
| import './ChatLog.css'; | ||
|
|
||
| const ChatLog = ({entries, onUpdateChat}) => { | ||
| const messages = entries.map((message, index) => { | ||
| return( | ||
| <section key = {index}> | ||
|
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 the messages all have unique ids, then we could use those values for the |
||
| <ChatEntry | ||
| sender = {message.sender} | ||
| body = {message.body} | ||
| timeStamp = {message.timeStamp} | ||
| id = {message.id} | ||
| liked = {message.liked} | ||
| onUpdateChat = {onUpdateChat} | ||
| /> | ||
| </section> | ||
| ); | ||
| }); | ||
| return(messages); | ||
| }; | ||
|
|
||
| ChatLog.propTypes = { | ||
| entries: PropTypes.arrayOf( | ||
| PropTypes.shape({ | ||
|
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. Really nice use of PropTypes. |
||
| id: PropTypes.number, | ||
| sender: PropTypes.string, | ||
| body: PropTypes.string, | ||
| timeStamp: PropTypes.string, | ||
| liked: PropTypes.bool, | ||
| onUpdateChat: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.
Since the liked status of a message lives in the
chatEntriesdata we should avoid holding an extra piece of state that we need to manually keep in sync. We can use a higher order function likearray.reduceto take our list of messages and reduce it down to a single value (our like count).