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
29 changes: 23 additions & 6 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import './App.css';
import ChatLog from './components/ChatLog';
import { useState } from 'react';
import messages from './data/messages.json';

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

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

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;

Comment on lines +21 to +22

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>
<h1>Vladmir and Estragon</h1>
<h2>{totalLikes} ❤️s</h2>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
</main>
<ChatLog entries={chatMessages} onLike={toggleLike} />

Choose a reason for hiding this comment

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

Love seeing parents passing things on to their children!

</div>
);
};

export default App;
export default App;
29 changes: 22 additions & 7 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
import './ChatEntry.css';
import TimeStamp from './TimeStamp';
import PropTypes from 'prop-types';

const ChatEntry = (props) => {
const handleLikeClick = () => {
props.onLike(props.id); // Notify the parent component (App) to toggle like state
};
Comment on lines +5 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">{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>
<p className="entry-time">
<TimeStamp time={props.timeStamp} />
</p>
<button className="like" onClick={handleLikeClick}>
{props.liked ? '❤️' : '🤍'}

Choose a reason for hiding this comment

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

ternaries, love to see them! 😍

</button>
</section>
</div>
);
};

ChatEntry.propTypes = {
// Fill with correct proptypes
sender: PropTypes.string,
id: PropTypes.number,
body: PropTypes.string,
timeStamp: PropTypes.string,
liked: PropTypes.bool,
onLike: PropTypes.func, // Ensure the `onLike` prop is passed
};

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

const ChatLog = (props) => {
const chatEntries = props.entries.map((entry) => {
return (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
timeStamp={entry.timeStamp}
body={entry.body}
liked={entry.liked}
onLike={props.onLike}
/>
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}
      />
    );
  });

Just be mindful that this won't be as explicit as what you have, a trade off!

);
});

return <main>{chatEntries}</main>;

Choose a reason for hiding this comment

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

Nice wrapping this in the <main> tag. I would actually suggest to add in this App.jsx so you could wrap ChatLog in it as well. We don't show anything outside of ChatEntry components but if we did then those would be outside of our <main> tag. We could just keep moving them inside here but why do that when we could wrap the entire component.

};

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

Choose a reason for hiding this comment

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


export default ChatLog;