Skip to content
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
20 changes: 17 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import './App.css';
import ChatLog from './components/ChatLog';
import message from './data/messages.json';
import {useState} from 'react';

const App = () => {
const [chatData, setChatData] = useState(message);
const togglelikes = (id) => {
setChatData( chatData => chatData.map(chat => {
if (chat.id === id){
return {...chat, liked: !chat.liked};
}else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
}else {
} else {

return chat;
Comment on lines +10 to +13
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice managing of data & making sure we return a new object when we need to alter a message in our list.

}
}));
};
const totalLikes = chatData.filter((chat) => chat.liked).length;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Great work calculating the hearts count from the chatsData! Since we don't need the contents of the array we get from filter, another option is to use a higher order function like array.reduce to take our list of messages and reduce it down to a single value.

// This could be returned from a helper function
// likesCount is a variable that accumulates a value as we loop over each entry in chatData
const totalLikes = chatData.reduce((likesCount, currentMessage) => {
    // If currentMessage.liked is true add 1 to likesCount, else add 0
    return (likesCount += currentMessage.liked ? 1 : 0);
}, 0); // The 0 here sets the initial value of likesCount to 0

return (
Comment on lines +7 to 18
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use blank lines to break up sections of code so it's easy as a reader to see where different areas of responsibility in the file are. It can also help flow and ease of reading to organize variables together, followed by functions, then the return value of the file. A possible reorganization could look like:

  const [chatData, setChatData] = useState(message);
  const totalLikes = chatData.filter((chat) => chat.liked).length;

  const togglelikes = (id) => {
    setChatData( chatData => chatData.map(chat => {
      if (chat.id === id){
        return {...chat, liked: !chat.liked};
      }else {
        return chat;
      }
    }));
  };
  
  return (

<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat Log</h1>
<p>{totalLikes} ❤️s </p>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={chatData} chatClicked={togglelikes}/>
</main>
</div>
);
Expand Down
22 changes: 16 additions & 6 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import './ChatEntry.css';
import TimeStamp from './TimeStamp';
import PropTypes from 'prop-types';

const ChatEntry = () => {
const ChatEntry = ({id,sender,body,timeStamp,liked,chatClicked}) => {
const heartColor = liked ? '❤️' : '🤍';
return (
<div className="chat-entry local">
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we wanted to implement the local and remote styles, we could use another ternary operator to determine our sender value and then set the class as local or remote accordingly. It could be helpful to look at how to create an interpolated string in Javascript for that.

<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={() => chatClicked(id)}>{heartColor}</button>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like this pattern of sending just the id to chatClicked since it keeps all the state management and message object creation confined to App.

</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.string.isRequired,
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we take a look in the messages.json file, what is the datatype of the "liked" key in each object? Something I noticed when running the tests both in VS Code and in the Terminal was a message in the test output:

Warning: Failed prop type: Invalid prop liked of type boolean supplied to ChatEntry, expected string.
at ChatEntry (/Users/kelseysteven/Documents/grading/C22/ChatLog/Marjana-react-chatlog/src/components/ChatEntry.jsx:9:22)

What does this error tell us?

chatClicked: PropTypes.func.isRequired,
};

export default ChatEntry;
37 changes: 37 additions & 0 deletions src/components/ChatLog.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import ChatEntry from './ChatEntry';
import PropTypes from 'prop-types';
import './ChatLog.css';

const ChatLog = ({entries, chatClicked}) => {
const chatComponents = entries.map((chat) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice mapping! 🙌🏻

return (
<li key={chat.id}>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rather than wrapping our ChatEntry in a li here, another option could be to change ChatEntry so that it's outermost element was an li instead of a div

<ChatEntry
id={chat.id}
sender={chat.sender}
body={chat.body}
timeStamp={chat.timeStamp}
chatClicked={chatClicked}
liked={chat.liked}
/>
</li>
);
});
return (
<>
{chatComponents}
</>
);
};

ChatLog.propTypes = {
entries: PropTypes.arrayOf(PropTypes.shape({
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice use of PropTypes.arrayOf and PropTypes.shape!

id: PropTypes.number.isRequired,
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
})).isRequired,
chatClicked: PropTypes.func.isRequired,
};

export default ChatLog;