-
Notifications
You must be signed in to change notification settings - Fork 113
Madison Jackson React Chatlog #114
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
ea938f9
358fc5d
07e8dc3
8a89eee
8910db7
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,43 @@ | ||
| import React from 'react'; | ||
| import React, { useState } from 'react'; | ||
| import './App.css'; | ||
| import chatMessages from './data/messages.json'; | ||
| import ChatEntry from './components/ChatEntry'; | ||
| import ChatLog from './components/ChatLog'; | ||
|
|
||
|
|
||
| const App = () => { | ||
|
|
||
| const [chatData, setChatData] = useState(chatMessages); | ||
| let [likeCount, setLikeCount] = useState(0); | ||
| // let likeCount = 0; | ||
|
|
||
| const onLikeClick = (id) => { | ||
| setChatData(chatData.map(chat => { | ||
| if (chat.id === id){ | ||
| chat.liked = !chat.liked | ||
| if (chat.liked === true){ | ||
| setLikeCount(likeCount += 1) | ||
| }; | ||
| if (chat.liked === false){ | ||
| setLikeCount(likeCount -= 1) | ||
| }; | ||
|
Comment on lines
+18
to
+23
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. Our state |
||
|
|
||
| return chat; | ||
| } | ||
| return chat; | ||
| })); | ||
| }; | ||
|
Comment on lines
+14
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. The way
Our useState data const onLikeClick = (id) => {
const newEntries = chatData.map((chat) => {
if (chat.id === id) {
// 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 {...chat, liked: !chat.liked}
}
// We didn't enter the if-statement, so this entry is not changing.
// Return the existing data
return chat;
});
setChatData(newEntries);
}; |
||
|
|
||
|
|
||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1>ChatBug</h1> | ||
| <h2>{likeCount} ❤️s</h2> | ||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <div><ChatLog entries={chatData} onClick={onLikeClick}></ChatLog></div> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,29 @@ | ||
| import React from 'react'; | ||
| import './ChatEntry.css'; | ||
| import PropTypes from 'prop-types'; | ||
| import TimeStamp from './TimeStamp'; | ||
|
|
||
| const ChatEntry = (props) => { | ||
| const ChatEntry = ({id, sender, body, timeStamp, liked, onClick}) => { | ||
| const heartColor = liked === false ? '🤍' : '❤️'; | ||
| const likeButton = () => {onClick(id)}; | ||
| 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={likeButton}>{heartColor}</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, | ||
| onClick: PropTypes.func.isRequired, | ||
| }; | ||
|
|
||
| export default ChatEntry; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import ChatEntry from './ChatEntry'; | ||
| import './ChatLog.css' | ||
|
|
||
| const ChatLog = (props) => { | ||
| const getChat = props.entries.map((chat) => { | ||
| return ( | ||
| <ChatEntry id={chat.id} | ||
| sender={chat.sender} | ||
| body={chat.body} | ||
| timeStamp={chat.timeStamp} | ||
| liked={chat.liked} | ||
| onClick={props.onClick} | ||
| key={chat.id} | ||
| /> | ||
| ) | ||
| }) | ||
| return <div className='chat-log'>{getChat}</div> | ||
|
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'm seeing some |
||
| }; | ||
|
|
||
| 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. Great use of PropTypes & PropTypes.shape! |
||
| id: PropTypes.number.isRequired, | ||
| sender: PropTypes.string.isRequired, | ||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
| liked: PropTypes.bool.isRequired, | ||
| }) | ||
| ).isRequired, | ||
| onClick: 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.
Gentle reminder we want to clean up commented out code so it doesn't get into our final codebase.