-
Notifications
You must be signed in to change notification settings - Fork 114
paige zoisite react chat log #116
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 1 commit
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,33 @@ | ||
| 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) | ||
|
|
||
| // this works bc we are setting the state of the page in like 21 which will rerender the page(app component) everytime the buttone is pressed. | ||
| const likeCount = chatEntries.filter((entry) => entry.liked).length | ||
|
|
||
| const updateLike = (chatEntryId) => { | ||
| const updatedEntries = chatEntries.map((entry) => { | ||
| if (entry.id === chatEntryId) { | ||
| entry.liked = !entry.liked; | ||
| } | ||
| return entry; | ||
| }) | ||
|
|
||
| setChatEntries(updatedEntries); | ||
| } | ||
|
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 way
Our useState data const updateLike = (chatEntryId) => {
const updatedEntries = chatEntries.map((entry) => {
if (entry.id === chatEntryId) {
// Use spread operator to copy all of entry's values to a new object,
// then overwrite the attribute liked with the opposite of what entry's liked value was
return { ...entry, liked: !entry.liked }
}
// We didn't enter the if-statement, so this entry is not changing.
// Return the existing data
return chat;
});
setChatEntries(updatedEntries);
}; |
||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1>Chat 🪵 {likeCount}</h1> | ||
|
||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog entries={chatEntries} updateLike={updateLike} /> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,4 +97,5 @@ button { | |
|
|
||
| .chat-entry.remote .entry-bubble:hover::before { | ||
| background-color: #a9f6f6; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,36 @@ | ||
| import React from 'react'; | ||
| // import TimeStamp from './TimeStamp'; | ||
| import './ChatEntry.css'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatEntry = (props) => { | ||
| const heart = props.liked ? '🤍' : '❤️'; | ||
| console.log(heart) | ||
|
||
|
|
||
| const handleLikeButton = () => { | ||
| props.updateLike(props.id); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="chat-entry local"> | ||
| <h2 className="entry-name">Replace with name of sender</h2> | ||
| <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">{props.timeStamp}</p> | ||
|
||
| <button className="like" onClick={handleLikeButton} >{heart} </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, | ||
| updateLike: PropTypes.func.isRequired, | ||
| // likeDecrease: PropTypes.func.isRequired, | ||
| }; | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import React from 'react'; | ||
| import ChatEntry from './ChatEntry'; | ||
| import TimeStamp from './TimeStamp'; | ||
| import './ChatLog.css'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatLog = (props) => { | ||
| return ( | ||
| <ul> | ||
| {props.entries.map((entry) => ( | ||
| <ChatEntry | ||
| key={entry.id} | ||
| id={entry.id} | ||
| sender={entry.sender} | ||
| body={entry.body} | ||
| timeStamp={entry.time} | ||
|
||
| liked={entry.liked} | ||
| updateLike={props.updateLike} | ||
| /> | ||
| ))} | ||
| </ul> | ||
| ) | ||
| } | ||
|
|
||
| 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, | ||
| // likeDecrease: PropTypes.func.isRequired, | ||
| updateLike: 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.
Great use of
filter& the message state to find the heart count!