-
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
Melissa Pena - Cheetahs #118
base: main
Are you sure you want to change the base?
Changes from all commits
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,52 @@ | ||
import React from 'react'; | ||
import { useState } from 'react'; | ||
import './App.css'; | ||
import ChatLog from './components/ChatLog'; | ||
import chatMessages from './data/messages.json'; | ||
|
||
const App = () => { | ||
const chatCopy =[] | ||
for (const message of chatMessages){ | ||
chatCopy.push(message) | ||
} | ||
const [chatData, setChatData]= useState(chatCopy) | ||
|
||
const updateLikes = (id, updatedLike) => { | ||
console.log('updatelikes is being called'); | ||
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. Be sure to remove unused code, print tests, and any comments that shouldn't be published before finalizing a project. It's good practice to keep a clean main branch that's production ready. This helps with the readability and maintainability of a project. 🧹😌 |
||
const newChatEntries = []; | ||
for (const chat of chatData) { | ||
if (chat.id !== id) { | ||
|
||
newChatEntries.push(chat); | ||
} else { | ||
const newChat = { | ||
...chat, | ||
liked: updatedLike | ||
}; | ||
|
||
newChatEntries.push(newChat); | ||
|
||
} | ||
|
||
setChatData(newChatEntries); | ||
|
||
} | ||
}; | ||
const countLikes = () => { | ||
return chatData.reduce((accumulator, count) => { | ||
return count.liked ? accumulator + 1 : accumulator; | ||
}, 0); | ||
}; | ||
|
||
|
||
return ( | ||
<div id="App"> | ||
<header> | ||
<h1>Application title</h1> | ||
<h1>Instant Messenger</h1> | ||
<h2>{countLikes()} ❤️s</h2> | ||
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 calculating likes! Right now, your app will re-calculate your likes count every time your App component re-renders. This totally works, but it could slow down performance as the number of messages begins to grow larger since |
||
</header> | ||
<main> | ||
{/* Wave 01: Render one ChatEntry component | ||
Wave 02: Render ChatLog component */} | ||
<ChatLog entries={chatData} updateLikes={updateLikes}></ChatLog> | ||
</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 ChatEntry = ({id, sender,body, timeStamp,liked, updateLikes}) => { | ||
const myHeart = liked ? '❤️': '🤍' ; | ||
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}/></p> | ||
<button className="like" onClick={() => updateLikes(id, !liked)}> | ||
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 adding the callback function in an anonymous function here! |
||
{myHeart} | ||
</button> | ||
</section> | ||
</div> | ||
); | ||
}; | ||
|
||
ChatEntry.propTypes = { | ||
//Fill with correct proptypes | ||
id:PropTypes.number, | ||
sender:PropTypes.string.isRequired, | ||
body:PropTypes.string.isRequired, | ||
timeStamp:PropTypes.string.isRequired, | ||
updateLikes:PropTypes.func | ||
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, but I would recommend setting |
||
}; | ||
|
||
export default ChatEntry; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
margin: auto; | ||
max-width: 50rem; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import ChatEntry from './ChatEntry'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const ChatLog = ({entries,updateLikes}) => { | ||
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. Make sure you're cleaning up the code style before finalizing a project! This component seems to be using 4 space tabs instead of the 2 space convention as well as some other small style inconsistencies. Good code style is important for readability and maintainability. 🧹 😌 |
||
|
||
const chatEntrycomponents = entries.map((chats) =>{ | ||
|
||
return ( | ||
<li key={chats.id}> | ||
< ChatEntry id = {chats.id} sender={chats.sender} body={chats.body} timeStamp={chats.timeStamp} liked={chats.liked} updateLikes={updateLikes}/> | ||
</li> | ||
) | ||
}); | ||
|
||
return ( | ||
<ul> | ||
{chatEntrycomponents} | ||
</ul> | ||
); | ||
|
||
|
||
}; | ||
|
||
ChatLog.propTypes = { | ||
entries: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
id:PropTypes.number, | ||
sender:PropTypes.string.isRequired, | ||
body:PropTypes.string.isRequired, | ||
timeStamp:PropTypes.string.isRequired, | ||
liked:PropTypes.bool.isRequired, | ||
|
||
}) | ||
), | ||
updateLikes: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.
Glad to see you're trying to ensure you don't modify the original data! Imports are actually read only though. So it's safe to use the message data directly without making a copy first.
Side note - the
push()
method doesn't actually create a copy of the data. It passes a reference to the original object. This means if you make changes to the "copy", the original will also be updated. You don't see this causing any issues in your code since imports are read only.In the future, if you need to make a copy of an object, I'd recommend using the spread operator. Here's a great guide on shallow copies vs deep copies in JS: https://code.tutsplus.com/articles/the-best-way-to-deep-copy-an-object-in-javascript--cms-39655