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

const App = () => {
const [chatMessages, setChatMessages] = useState(messages);

const toggleLike = (id) => {
setChatMessages(chatMessages => chatMessages.map(message => {
if (message.id === id) {
return { ...message, liked: !message.liked };
} else {
return message;
}
}));
Comment on lines +10 to +17

Choose a reason for hiding this comment

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

Nice work on this function! By passing this down to your individual chat messages you are now able to have a single source of truth!

};

const totalLikes = chatMessages.filter(message => message.liked).length;

Choose a reason for hiding this comment

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

You could also do this with the reduce method like so:

const totalLikes = chatMessages.reduce((count, message) => count + (message.liked ? 1 : 0), 0);


return (
<div id="App">
<header>
<h1>Application title</h1>
<h2>{totalLikes} 🤍s</h2>

Choose a reason for hiding this comment

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

Your code is failing a test. It is because this is the wrong heart (needs to be ❤️).

Suggested change
<h2>{totalLikes} 🤍s</h2>
<h2>{totalLikes} ❤️s</h2>

</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={chatMessages} onToggleLike={toggleLike}></ChatLog>

Choose a reason for hiding this comment

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

⭐️

</main>
</div>
);
Expand Down
27 changes: 21 additions & 6 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = ({sender, body, timeStamp, id, liked, onToggleLike}) => {
const handleClick = () => {
onToggleLike(id);
};
Comment on lines +6 to +8

Choose a reason for hiding this comment

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

The button function could also be implemented like so:

<button className='like' onClick={() => likeButtonClicked(id)}>{liked ? '❤️': '🤍'}</button>


const ChatEntry = () => {
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={handleClick}>
{liked ? '❤️' : '🤍'}
</button>
Comment on lines +12 to +20

Choose a reason for hiding this comment

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

Nice work!

</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,
onToggleLike: PropTypes.func.isRequired,
Comment on lines +27 to +32

Choose a reason for hiding this comment

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

⭐️

};

export default ChatEntry;
38 changes: 38 additions & 0 deletions src/components/ChatLog.jsx
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, onToggleLike }) => {
const chatEntries = entries.map((entry) => {
return(
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onToggleLike={onToggleLike}
/>
Comment on lines +7 to +15

Choose a reason for hiding this comment

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

Since the names of the keys on entry are the same as the names your are setting on ChatEntry attributes/you are using all of the values in entry you could do something something like this to save you a few keystrokes:

  const chatComponents = entries.map((entry) => {
    return(
      <ChatEntry
        {...entry}
        onLikeToggle={onLikeBtnToggle}
        key={entry.id}
      />
    );
  });

);
});
return (
<>
{chatEntries}
</>
);
};

ChatLog.propTypes = {
entries: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number.isRequired,
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
liked: PropTypes.bool.isRequired,
})
).isRequired,
onToggleLike: PropTypes.func.isRequired,
};
Comment on lines +25 to +36

Choose a reason for hiding this comment

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

⭐️


export default ChatLog;