Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/App.js
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)

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

}
const [chatData, setChatData]= useState(chatCopy)

const updateLikes = (id, updatedLike) => {
console.log('updatelikes is being called');

Choose a reason for hiding this comment

The 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>

Choose a reason for hiding this comment

The 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 countLikes() has an O(n) time complexity. Consider how you could refactor your code to improve performance.

</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={chatData} updateLikes={updateLikes}></ChatLog>
</main>
</div>
);
Expand Down
20 changes: 14 additions & 6 deletions src/components/ChatEntry.js
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)}>

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small note, but I would recommend setting id and updateLikes as required in your prop types since they are needed for the like button to work.

};

export default ChatEntry;
1 change: 1 addition & 0 deletions src/components/ChatLog.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
margin: auto;
max-width: 50rem;
}

38 changes: 38 additions & 0 deletions src/components/ChatLog.js
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}) => {

Choose a reason for hiding this comment

The 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;