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

const App = () => {
const [messages, setMessages] = useState(MessageData);
const onClickLike = (id) => {
setMessages((prevMessages) =>
prevMessages.map((entry) =>
entry.id === id ? { ...entry, liked: !entry.liked } : entry
)
);
};
Comment on lines +8 to +14
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice work ensuring we're creating a new array of messages to trigger the re-render after updating the liked value!


const likeCount = messages.filter((e) => e.liked).length;
Copy link
Copy Markdown

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


return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat between Vladimir and Estragon</h1>
<section>
<span className="widget" id="heartWidget">{`${likeCount} ❤️s`}</span>
Copy link
Copy Markdown

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. It's great for when we need to dynamically lookup and affect a section of a larger text, but it should not be used to represent general text on a page.

  • How does React handle this text? Do we need to dynamically look up this element and change just a part of it or is it re-rendered with the complete text each time React triggers a re-render?
  • Since this is a chunk of text on the page, what HTML element or elements would better represent this subheading with the likes count?

</section>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={messages} onClickLike={onClickLike} />
</main>
</div>
);
};

export default App;
export default App;
10 changes: 10 additions & 0 deletions src/components/ChatEntry.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,14 @@ button {

.chat-entry.remote .entry-bubble:hover::before {
background-color: #a9f6f6;
}

.red {
color: #ff0000;
font-weight: bold;
}

.green {
color: green;
font-weight: bold;
}
39 changes: 29 additions & 10 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = () => {
const ChatEntry = ({
id,
sender,
body,
timeStamp,
liked = false,
onToggleLike = () => {},
}) => {
const sideClass = sender === 'Vladimir' ? 'local' : 'remote';
const colorClass = sender === 'Vladimir' ? 'red' : 'green';
return (
// Replace the outer tag name with a semantic element that fits our use case
<replace-with-relevant-semantic-element className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<article className={`chat-entry ${sideClass}`}>
<h2 className="entry-name">{sender}</h2>
Comment on lines +13 to +17
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great use of ternary operators and interpolated strings to set the relevant CSS!

<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 className={`${colorClass}`}>{body}</p>
<p className="entry-time">
<TimeStamp time={timeStamp} />
</p>
<button className="like" onClick={() => onToggleLike(id)}>
Copy link
Copy Markdown

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.

{liked ? '❤️' : '🤍'}
</button>
Comment on lines +23 to +25
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We can increase the accessibility of our page by setting further attributes on elements, especially interactive elements. Below is an example of updates we could make to the button, check out the MDN docs for more info!

const heart = liked ? '❤️' : '🤍';

...

<button
  onClick={() => onToggleLike(id)}
  className="like"
  aria-label={heart}
  role="img"
>
  {heart}
</button>

</section>
</replace-with-relevant-semantic-element>
</article>
);
};

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

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

const ChatLog = ({ entries, onClickLike = () => {} }) => {
return (
<section className="chat-log">
{entries.map((message) => (
<ChatEntry
key={message.id}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great use of the unique message ids for the key value!

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

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

Choose a reason for hiding this comment

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

Great use of Prop-Types and flagging those necessary for render with isRequired, I love the use of PropTypes.shape to ensure the passed array has the necessary contents as well!

id: PropTypes.number.isRequired,
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
liked: PropTypes.bool.isRequired,
})
),
onClickLike: PropTypes.func,
};

export default ChatLog;