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

const App = () => {
const [messages, setMessages] = useState(messagesData);

const toggleLike = (messageId) => {
setMessages(prevMessages =>
prevMessages.map(message =>
message.id === messageId
? { ...message, liked: !message.liked }
: message
Comment on lines +11 to +14

Choose a reason for hiding this comment

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

Great use of map, the ternary operator, and the spread operator to simplify copying data and updating the liked value where necessary!

)
);
};

const getTotalLikes = () => {
return messages.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.

Great work calculating the likes count from the chatsData! Since we don't need the contents of the array we create with 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
// totalLikes is a variable that accumulates a value as we loop over each entry in chatEntries
const likesCount = chatEntries.reduce((totalLikes, currentMessage) => {
    // If currentMessage.liked is true add 1 to totalLikes, else add 0
    return (totalLikes += currentMessage.liked ? 1 : 0);
}, 0); // The 0 here sets the initial value of totalLikes to 0

};

const totalLikes = getTotalLikes();

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Ada Chat Log</h1>
<section>
<span className="widget">
{totalLikes} ❤️s
</span>
Comment on lines +30 to +32

Choose a reason for hiding this comment

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

span is not a semantic element, so we should avoid it unless necessary for styling. Since this is a chunk of user facing text, we should use <p> and style it as we'd like with CSS.

</section>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
entries={messages}
onToggleLike={toggleLike}
/>
</main>
</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 = ({ id, sender, body, timeStamp, liked, onToggleLike }) => {
const handleLikeClick = () => {
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.

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


const ChatEntry = () => {
return (
<div className="chat-entry local">

Choose a reason for hiding this comment

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

I know this was in the scaffold, but since div is not a semantic element, I would consider something like article as the outermost tag on a ChatEntry.

<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={handleLikeClick}>
{liked ? '❤️' : '🤍'}
</button>
</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,
};

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

const ChatLog = ({ entries, onToggleLike }) => {
return (
<section className="chat-log">
{entries.map(entry => (
<ChatEntry
key={entry.id}

Choose a reason for hiding this comment

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

Nice use of the entry.id for the key values since our messages have unique ids!

id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onToggleLike={onToggleLike}
/>
))}
</section>
);
};

ChatLog.propTypes = {
entries: PropTypes.array.isRequired,
onToggleLike: PropTypes.func.isRequired,
};

export default ChatLog;