-
Notifications
You must be signed in to change notification settings - Fork 117
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
Zoisite - Jackie Aponte #104
base: main
Are you sure you want to change the base?
Changes from all commits
ac9769d
80b4554
b24cec5
5a18708
6f5116a
bc77ca8
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,46 @@ | ||
import React from 'react'; | ||
import React, {useState} from 'react'; | ||
import './App.css'; | ||
import chatMessages from './data/messages.json'; | ||
import ChatLog from './components/ChatLog.js' | ||
|
||
|
||
const App = () => { | ||
const localUser = chatMessages[0].sender | ||
const remoteUser = chatMessages[1].sender | ||
|
||
const [chatData, setChatData] = useState(chatMessages); | ||
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 setting up state for chat messages. |
||
|
||
const onUpdateChat = (updatedChat) => { | ||
const messages = chatData.map(chat => { | ||
if (chat.id === updatedChat.id) { | ||
return updatedChat; | ||
} else { | ||
return chat; | ||
} | ||
}); | ||
Comment on lines
+13
to
+20
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 receiving a new entry with it's like status already changed here, rewrite this method to accept only an id. The logic about how to copy the message and change the liked status would be better if it was written in App.js, since the state must have all the information necessary to make a new message. const onUpdateChat = id => {
const messages = chatData.map(chat => {
if (chat.id === id){
return {...chat, liked: !chat.liked};
}
else {
return chat;
}
}); |
||
|
||
setChatData(messages); | ||
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. 👀 Since the next version of the chat data depends on the current version, prefer using the callback style of setChatMessagData: setTextMessage(prevEntries => (
// logic to use current chats to make a new copy with the desired liked status flipped (the map logic above)
// return the new chats to update the state
)) You can see an example of how we did it in Flasky here |
||
}; | ||
|
||
let totalLikes = 0 | ||
for (const message of chatData) { | ||
if (message.liked) { | ||
totalLikes +=1 | ||
}; | ||
}; | ||
Comment on lines
+25
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. We can use the reduce method to calculate the total likes too: const totalLikes = () => {
return chatData.reduce((total, text) => {
return text.liked ? total + 1 : total;
}, 0)
}; |
||
|
||
|
||
return ( | ||
<div id="App"> | ||
<header> | ||
<h1>Application title</h1> | ||
<h1>Chat between {localUser} and {remoteUser}</h1> | ||
<section>{totalLikes} ❤️s</section> | ||
</header> | ||
<main> | ||
{/* Wave 01: Render one ChatEntry component | ||
Wave 02: Render ChatLog component */} | ||
<ChatLog | ||
chatData={chatData} | ||
onUpdateChat={onUpdateChat}> | ||
Comment on lines
+41
to
+42
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> | ||
</main> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,43 @@ | ||
import React from 'react'; | ||
import './ChatEntry.css'; | ||
import TimeStamp from './TimeStamp.js'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const ChatEntry = (props) => { | ||
const timeEntry=props.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. Nitpick - there should be whitespace around the equal sign. When we pass props to a component, then by convention we should omit the spaces around the equal sign, but when we're declaring variables we should have them. |
||
|
||
const onLikeButtonClick = () => { | ||
const updatedChat = { | ||
id: props.id, | ||
sender: props.sender, | ||
body: props.body, | ||
timeStamp: props.timeStamp, | ||
liked: !props.liked | ||
}; | ||
|
||
props.onUpdateChat(updatedChat); | ||
}; | ||
Comment on lines
+9
to
+19
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. When you just pass in an id to props.onUpdateChat(), then this method can be refactored to just be one line: const onLikeButtonClick = () => {
props.onUpdateChat(props.id);
}; |
||
|
||
const heartColor = props.liked ? '❤️' : '🤍'; | ||
|
||
return ( | ||
<div className="chat-entry local"> | ||
<h2 className="entry-name">Replace with name of sender</h2> | ||
<div className={props.chatEntry}> | ||
Comment on lines
+10
to
+24
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. Instead of getting the class 'remote' or 'local' from props, we can calculate it here.
|
||
<h2 className="entry-name">{props.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>{props.body}</p> | ||
<p className="entry-time"><TimeStamp time={timeEntry}></TimeStamp></p> | ||
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 passing the correct prop to the provided TimeStamp component 👍 |
||
<button className="like" onClick={onLikeButtonClick}>{heartColor}</button> | ||
</section> | ||
</div> | ||
); | ||
}; | ||
|
||
ChatEntry.propTypes = { | ||
//Fill with correct proptypes | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool, | ||
onUpdateChat: PropTypes.func.isRequired | ||
Comment on lines
+36
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. The PropType check is missing |
||
}; | ||
|
||
export default ChatEntry; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react'; | ||
import './ChatLog.css'; | ||
import PropTypes from 'prop-types'; | ||
import ChatEntry from './ChatEntry.js' | ||
|
||
const ChatLog = (props) => { | ||
if (!props.chatData) { | ||
return null | ||
} | ||
Comment on lines
+7
to
+9
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. We don't need this check here. If a value is undefined, React will just render a blank screen |
||
const ChatLogEntries = props.chatData.map((entry) => { | ||
return ( | ||
<div key={entry.id}> | ||
<ChatEntry | ||
id={entry.id} | ||
sender={entry.sender} | ||
timeStamp={entry.timeStamp} | ||
body={entry.body} | ||
liked={entry.liked} | ||
onUpdateChat={props.onUpdateChat} | ||
chatEntry={entry.sender===props.chatData[0].sender ? 'chat-entry local' : 'chat-entry remote'} | ||
></ChatEntry> | ||
</div> | ||
); | ||
}); | ||
Comment on lines
+10
to
+24
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. Small note that variables don't begin with a capital letter. The prop key should go on each ChatEntry, not on the entire div. When you have an array of components (like you have an array of ChatEntries), each component in the array gets a prop key. Also, instead of writing a method const ChatLog = (props) => {
return (
<div>
{props.entries.map((message) => (
<ChatEntry
key={entry.id} // the key should go on each ChatEntry
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onUpdateChat={props.onUpdateChat}
/>
))}
</div>
)
}; The logic on line 20 to determine the class name for remote or local should live in ChatEntry.js I think you can omit line 20 completely and have this logic live in the child component. I think this because each child component should know who its sender is and thus should be in charge of determining the class. |
||
|
||
return ( | ||
<div> | ||
{ChatLogEntries} | ||
</div> | ||
); | ||
}; | ||
|
||
ChatLog.propTypes = { | ||
chat: PropTypes.arrayOf(PropTypes.shape({ | ||
id: PropTypes.number.isRequired, | ||
senderData: PropTypes.string.isRequired, | ||
bodyData: PropTypes.string.isRequired, | ||
likedData: PropTypes.bool.isRequired | ||
})), | ||
onUpdateChat: PropTypes.func.isRequired | ||
}; | ||
Comment on lines
+33
to
+41
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. The PropType check here is just a little off. Also, prefer to name the keys the same as they appear in messages.json so that senderData is just sender (like it appears in the data file.
|
||
|
||
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.
What if textMessage[0] and textMessage[1] were both messages from the same person? How else could you find a way to get the names of the senders?