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
2 changes: 2 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#App header {
background-color: #222;
color: #fff;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
position: fixed;
width: 100%;
Expand All @@ -28,6 +29,7 @@

#App header section {
background-color: #e0ffff;
color: black;
}

#App .widget {
Expand Down
25 changes: 22 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import './App.css';
import ChatLog from './components/ChatLog.jsx';
import messagesData from './data/messages.json';
import { useState } from 'react';

const App = () => {
const [messageData, setMessageData] = useState(messagesData);

Choose a reason for hiding this comment

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

Nice work on using the useState hook to initialize state for your App component.


const toggleLiked = (messageId) => {
const messages = messageData.map(message => {
if (message.id === messageId) {
return { ...message, liked: !message.liked };
} else {
return message; // message like status not changed
}
});
setMessageData(messages);
};
Comment on lines +9 to +18

Choose a reason for hiding this comment

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

Great to see you using .map to map over your entries! Very functional! Since you are using a simple conditional block here, we could make this more succinct by using a ternary instead like so:

const toggleLike = (id) =>
  setEntries(entries.map(entry =>
    entry.id === id ? { ...entry, liked: !entry.liked } : entry
  ));

When you find yourself with simple checks like these, more often than not a ternary could be implemented instead for conciseness but still maintaining readability.

const totalLikes = messageData.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>
<h1>React Chatlog</h1>
<section id="heartWidget">{totalLikes} ❤️s</section>

Choose a reason for hiding this comment

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

⭐️

</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
entries={messageData}
onLikeToggle={toggleLiked}
></ChatLog>
Comment on lines +27 to +30

Choose a reason for hiding this comment

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

Formatting your component like this is one way to maintain readability! ⭐️🫡

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

const ChatEntry = () => {
const ChatEntry = ({id, sender, body, timeStamp, liked, onLikeToggle}) => {

Choose a reason for hiding this comment

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

Suggested change
const ChatEntry = ({id, sender, body, timeStamp, liked, onLikeToggle}) => {
const ChatEntry = ({ id, sender, body, timeStamp, liked, onLikeToggle }) => {

const likeButtonClicked = () => {
onLikeToggle(id);
};
const heartColor = liked ? '❤️' : '🤍';
Comment on lines +6 to +9

Choose a reason for hiding this comment

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

👍🏿

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>
Comment on lines +12 to +16

Choose a reason for hiding this comment

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

🫡

</p>
<button className="like" onClick={likeButtonClicked}>{heartColor}</button>

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>

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

ChatEntry.propTypes = {
// Fill with correct proptypes
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
id: PropTypes.number,
liked: PropTypes.bool,
onLikeToggle: PropTypes.func
};

export default ChatEntry;
export default ChatEntry;
2 changes: 1 addition & 1 deletion src/components/ChatLog.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.chat-log {
margin: auto;
max-width: 50rem;
max-width: 50rem
}
39 changes: 39 additions & 0 deletions src/components/ChatLog.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import './ChatLog.css';
import PropTypes from 'prop-types';
import ChatEntry from './ChatEntry.jsx';

const ChatLog = (props) => {

Choose a reason for hiding this comment

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

@ellenjin, is there a reason why you didn't de-structure your props here? Only asking out of curiosity!

const messageComponents = props.entries.map(message => {

Choose a reason for hiding this comment

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

I also prefer to use a variable to hold my mapped components! Just a heads, you might see the pattern where the mapping is done in the JSX object. Some developers prefer that since they feel like it helps them understand the overall structure of the component.

return (
<ChatEntry className="chat-log"
key={message.id}
id={message.id}
sender={message.sender}
body={message.body}
timeStamp={message.timeStamp}
liked={message.liked}
onLikeToggle={props.onLikeToggle}
></ChatEntry>
Comment on lines +8 to +16

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}
	/>
  );
});

Though, what you have is more verbose! The suggestion I have above can be used when we want to use pass every key/value pair to the child component. If not, then we wouldn't follow the suggested pattern since it would be passing impertinent information to a component, putting it at risk to accidentally change a piece and be more bug prone.

);
});

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

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

Choose a reason for hiding this comment

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

Nice work including prop types for this component! Just a quick reminder: in React v19, PropTypes are deprecated. Moving forward, it's best to use TypeScript for type checking, as it's the recommended and more robust solution for ensuring type safety across your application.

export default ChatLog;
2 changes: 1 addition & 1 deletion src/data/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,4 @@
"timeStamp":"2018-05-29T23:17:34+00:00",
"liked": false
}
]
]