-
Notifications
You must be signed in to change notification settings - Fork 111
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
C17 Otters - Ada Barries #104
base: main
Are you sure you want to change the base?
Changes from all commits
d4a4d45
8f6073d
b5129d1
8917064
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,39 @@ | ||
import React from 'react'; | ||
import './App.css'; | ||
import chatMessages from './data/messages.json'; | ||
import ChatLog from './components/ChatLog'; | ||
import { useState } from 'react'; | ||
|
||
|
||
const App = () => { | ||
const [entries, setEntries] = useState(chatMessages); | ||
let [likeCount, setLikes] = useState(0); | ||
|
||
const changeHeartColor = (id) => { | ||
const newEntries = entries.map((entry) => ({...entry})); | ||
for (const entry of newEntries) { | ||
if (entry.id === id) { | ||
entry.liked = !entry.liked; | ||
if (entry.liked) { | ||
likeCount += 1; | ||
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. Careful not to update the state variable directly! This could have unintended side effects. You should always use the Here's a good discussion on this: https://stackoverflow.com/questions/37755997/why-cant-i-directly-modify-a-components-state-really Alsoooo your app currently only increases |
||
} | ||
} | ||
} | ||
setEntries(newEntries); | ||
setLikes(likeCount); | ||
}; | ||
|
||
return ( | ||
<div id="App"> | ||
<header> | ||
<h1>Application title</h1> | ||
<h1>Chat Log</h1> | ||
<h2>{likeCount} ❤️s</h2> | ||
</header> | ||
<main> | ||
{/* Wave 01: Render one ChatEntry component | ||
Wave 02: Render ChatLog component */} | ||
<ChatLog | ||
entries={entries} | ||
heartCallback={changeHeartColor} | ||
/> | ||
</main> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,30 @@ | ||
import React from 'react'; | ||
import './ChatEntry.css'; | ||
import PropTypes from 'prop-types'; | ||
import TimeStamp from './TimeStamp'; | ||
|
||
const ChatEntry = (props) => { | ||
const heartToggle = () => { | ||
props.heartCallback(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> | ||
<TimeStamp className="entry-time" time={props.timeStamp}></TimeStamp> | ||
<button className="like" onClick={heartToggle}>{props.liked ? '❤️' : '🤍' }</button> | ||
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 ternary operator 🎉 |
||
</section> | ||
</div> | ||
); | ||
}; | ||
} | ||
|
||
ChatEntry.propTypes = { | ||
//Fill with correct proptypes | ||
id: PropTypes.number, | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool, | ||
}; | ||
|
||
export default ChatEntry; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from 'react'; | ||
import './ChatLog.css'; | ||
import ChatEntry from './ChatEntry'; | ||
import PropTypes from 'prop-types'; | ||
|
||
|
||
const ChatLog = (props) => { | ||
const chatComponents = props.entries.map((entry) => { | ||
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 ✨ |
||
return ( | ||
<div key={entry.id}> | ||
<ChatEntry | ||
id={entry.id} | ||
sender={entry.sender} | ||
body={entry.body} | ||
timeStamp={entry.timeStamp} | ||
liked={entry.liked} | ||
heartCallback={props.heartCallback} | ||
/> | ||
</div> | ||
); | ||
}); | ||
return ( | ||
<div> | ||
{chatComponents} | ||
</div> | ||
) | ||
} | ||
|
||
ChatLog.propTypes = { | ||
entries: PropTypes.array.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.
Nice copy! As a heads up, you can create a copy with Destructuring alone like this: