-
Notifications
You must be signed in to change notification settings - Fork 153
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
Tigers - Melley Gebretatios #123
base: main
Are you sure you want to change the base?
Changes from all commits
2287f67
9a674b4
f4499ec
d066ed1
b6f0d02
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 ChatEntry from './components/ChatEntry'; | ||||||||
import chatMessages from './data/messages.json'; | ||||||||
import ChatLog from './components/ChatLog'; | ||||||||
|
||||||||
const App = () => { | ||||||||
// brain | ||||||||
const [ChatData, setChatData] = useState(chatMessages); | ||||||||
const [likeCount, setLikeCount] = useState(0); | ||||||||
const updateLike = (id, like) => { | ||||||||
// console.log(id, ChatData); | ||||||||
// console.log(like); | ||||||||
// console.log(id.liked); | ||||||||
Comment on lines
+12
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. Remember to delete any commented out code or print statements used for debugging before turning in your final submission
Suggested change
|
||||||||
|
||||||||
const newMessage = ChatData.find((entry) => entry.id === 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. I think we can refactor this function so we aren't iterating twice. By separating the message out here, we now lose track of where it is in the data structure. So we have to iterate through a second time to put it back in. Let's just do everything at once inside the bottom loop. |
||||||||
// console.log('newMessage'); | ||||||||
// console.log(newMessage); | ||||||||
// console.log(like); | ||||||||
Comment on lines
+17
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.
Suggested change
|
||||||||
newMessage.liked = like; | ||||||||
setChatData([...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. The |
||||||||
if (like) { | ||||||||
setLikeCount(likeCount + 1); | ||||||||
console.log('setLikeCount'); | ||||||||
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 (!like) { | ||||||||
setLikeCount(likeCount - 1); | ||||||||
console.log('setLikeCount2'); | ||||||||
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
|
||||||||
} | ||||||||
}; | ||||||||
// beauty | ||||||||
return ( | ||||||||
<div id="App"> | ||||||||
<header> | ||||||||
<h1>Application title</h1> | ||||||||
<h1>Chat between Vladmir and Estrogen</h1> | ||||||||
<section> | ||||||||
<h2>{likeCount} ❤️s</h2> | ||||||||
</section> | ||||||||
</header> | ||||||||
<main> | ||||||||
{/* Wave 01: Render one ChatEntry component | ||||||||
Wave 02: Render ChatLog component */} | ||||||||
// Wave 02: Render ChatLog component */} | ||||||||
<ChatLog entries={ChatData} updateMessage={updateLike}></ChatLog> | ||||||||
</main> | ||||||||
</div> | ||||||||
); | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,22 +1,51 @@ | ||||||||||||
import React from 'react'; | ||||||||||||
import './ChatEntry.css'; | ||||||||||||
import PropTypes from 'prop-types'; | ||||||||||||
import TimeStamp from './TimeStamp'; | ||||||||||||
|
||||||||||||
const ChatEntry = (props) => { | ||||||||||||
const ChatEntry = ({ id, body, sender, timeStamp, liked, updateMessage }) => { | ||||||||||||
// console.log('ChatEntry'); | ||||||||||||
const onLikeButtonClick = () => { | ||||||||||||
// console.log('onLikeButtonClick'); | ||||||||||||
// console.log(liked); | ||||||||||||
Comment on lines
+7
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.
Suggested change
|
||||||||||||
|
||||||||||||
const updatedMessage = { | ||||||||||||
id: id, | ||||||||||||
body: body, | ||||||||||||
sender: sender, | ||||||||||||
timeStamp: timeStamp, | ||||||||||||
liked: !liked, | ||||||||||||
}; | ||||||||||||
updateMessage(updatedMessage.id, updatedMessage.liked); | ||||||||||||
}; | ||||||||||||
Comment on lines
+12
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. This works just fine, and it's an example we show in Learn. But consider that |
||||||||||||
|
||||||||||||
const likeColor = liked ? '❤️' : '🤍'; | ||||||||||||
// console.log(likeColor); | ||||||||||||
// console.log(onLikeButtonClick); | ||||||||||||
Comment on lines
+23
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.
Suggested change
|
||||||||||||
return ( | ||||||||||||
<div className="chat-entry local"> | ||||||||||||
<h2 className="entry-name">Replace with name of sender</h2> | ||||||||||||
<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}></TimeStamp> | ||||||||||||
</p> | ||||||||||||
<button className="like" onClick={onLikeButtonClick}> | ||||||||||||
{likeColor} | ||||||||||||
</button> | ||||||||||||
</section> | ||||||||||||
</div> | ||||||||||||
); | ||||||||||||
}; | ||||||||||||
|
||||||||||||
ChatEntry.propTypes = { | ||||||||||||
//Fill with correct proptypes | ||||||||||||
// //Fill with correct proptypes | ||||||||||||
id: PropTypes.number, | ||||||||||||
sender: PropTypes.string.isRequired, | ||||||||||||
body: PropTypes.string.isRequired, | ||||||||||||
timeStamp: PropTypes.string.isRequired, | ||||||||||||
liked: PropTypes.bool.isRequired, | ||||||||||||
updateMessage: PropTypes.func.isRequired, | ||||||||||||
}; | ||||||||||||
|
||||||||||||
export default ChatEntry; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from 'react'; | ||
import ChatEntry from './ChatEntry'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const ChatLog = (props) => { | ||
const entries = props.entries; | ||
return ( | ||
<div> | ||
{entries.map((message) => ( | ||
<ChatEntry | ||
id={message.id} | ||
key={message.id} | ||
body={message.body} | ||
sender={message.sender} | ||
timeStamp={message.timeStamp} | ||
liked={message.liked} | ||
updateMessage={props.updateMessage} | ||
></ChatEntry> | ||
))} | ||
; | ||
</div> | ||
); | ||
}; | ||
// return requires an argument | ||
// return <div>{entries}</div>; | ||
|
||
ChatLog.propTypes = { | ||
entries: PropTypes.arrayOf(PropTypes.object), | ||
updateMessage: 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.