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
6 changes: 3 additions & 3 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#App {
background-color: #87cefa;
background-color: #fafafa;
}

#App header {
background-color: #222;
color: #fff;
background-color: #ffe4ec;
color: black;
padding-bottom: 0.5rem;
position: fixed;
width: 100%;
Expand Down
42 changes: 39 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
// Wave 1: Import ChatEntry to show one message
// import ChatEntry from './components/ChatEntry';
import './App.css';
import messages from './data/messages.json';
import ChatLog from './components/ChatLog';
import { useState } from 'react';

const calculateTotalLikes = (chatData) => {
return chatData.reduce((total, message) => {
return total + (message.liked ? 1 : 0);
}, 0);
Comment on lines +9 to +11
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 job using reduce! This is how most JS devs would go about calculating a total amount too.

};

const App = () => {
// Wave 1: Use first message data to render one ChatEntry
// const chatMessages = messages[0];

const [chatData, setChatData] = useState(messages);
const totalLikes = calculateTotalLikes(chatData);

const toggleLike = (id) => {
setChatData((chatData) =>
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 use of the callback setter style. In this application, it doesn't really matter whether we use the callback style or the value style, but it's good practice to get in the habit of using the callback style.

chatData.map((entry) => {
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 use of map here to both handle making a new list so that React sees the message data has changed, and make new data for the clicked message with its like status toggled.

if (entry.id === id) {
return { ...entry, liked: !entry.liked };
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 showed this approach in class, but technically, we're mixing a few responsibilities here. rather than this function needing to know how to change the liked status itself, we could move this update logic to a helper function. This would better mirror how we eventually update records when there's an API call involved.

In this project, our messages are very simple objects, but if we had more involved operations, it could be worthwhile to create an actual class with methods to work with them, or at least have a set of dedicated helper functions to centralize any such mutation logic.

} else {
return entry;
}
})
);
};

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>iYap</h1>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🤗

<h2>{totalLikes} 💖s</h2>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
{/* Wave 1: Rendered one ChatEntry
<ChatEntry
sender={chatMessages.sender}
body={chatMessages.body}
timeStamp={chatMessages.timeStamp}
/> */}
Comment on lines +40 to +45
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This comment might be confusing to other devs because it might make someone wonder if they should uncomment lines 41-44 in order for the ChatEntry component to render.

If you want to keep track of what pieces of data a component should have and how the data should flow between components, creating a diagram or documenting it in the project README could be good places for that and would also leave your code uncluttered with comments.

Suggested change
{/* Wave 1: Rendered one ChatEntry
<ChatEntry
sender={chatMessages.sender}
body={chatMessages.body}
timeStamp={chatMessages.timeStamp}
/> */}

{/* Wave 2: Render all messages using ChatLog */}
<ChatLog entries={chatData} onLike={toggleLike} />
</main>
</div>
);
Expand Down
8 changes: 4 additions & 4 deletions src/App.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Wave 03: clicking like button and rendering App', () => {
fireEvent.click(buttons[10]);

// Assert
const countScreen = screen.getByText(/3 ❤️s/);
const countScreen = screen.getByText(/3 💖s/);
expect(countScreen).not.toBeNull();
});

Expand All @@ -28,7 +28,7 @@ describe('Wave 03: clicking like button and rendering App', () => {

// click the first button
fireEvent.click(firstButton);
expect(firstButton.innerHTML).toEqual('❤️');
expect(firstButton.innerHTML).toEqual('💖');

// check that all other buttons haven't changed
for (let i = 1; i < buttons.length; i++) {
Expand All @@ -39,13 +39,13 @@ describe('Wave 03: clicking like button and rendering App', () => {
fireEvent.click(firstButton);
expect(firstButton.innerHTML).toEqual('🤍');
fireEvent.click(firstButton);
expect(firstButton.innerHTML).toEqual('❤️');
expect(firstButton.innerHTML).toEqual('💖');
fireEvent.click(firstButton);
expect(firstButton.innerHTML).toEqual('🤍');

// click the last button a couple times
fireEvent.click(lastButton);
expect(lastButton.innerHTML).toEqual('❤️');
expect(lastButton.innerHTML).toEqual('💖');
fireEvent.click(lastButton);
expect(lastButton.innerHTML).toEqual('🤍');
});
Expand Down
16 changes: 8 additions & 8 deletions src/components/ChatEntry.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ button {
}

.chat-entry .entry-bubble {
background-color: #ffffe0;
background-color: #f0f0f5;
border-radius: 30px;
max-width: 50rem;
min-width: 10rem;
Expand All @@ -27,7 +27,7 @@ button {
}

.chat-entry .entry-bubble:hover {
background-color: #fefea2;
background-color: #d6d6e0;
}

.chat-entry .entry-name {
Expand Down Expand Up @@ -63,12 +63,12 @@ button {
}

.chat-entry.local .entry-bubble::before {
background-color: #ffffe0;
background-color: #f0f0f5;
left: -18px;
}

.chat-entry.local .entry-bubble:hover::before {
background-color: #fefea2;
background-color: #d6d6e0;
}

/* "remote" messages are shown on the right side, in blue */
Expand All @@ -77,24 +77,24 @@ button {
}

.chat-entry.remote .entry-bubble {
background-color: #e0ffff;
background-color: #339aff;
margin-left: auto;
margin-right: 0;
}

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

.chat-entry.remote .entry-time {
text-align: left;
}

.chat-entry.remote .entry-bubble::before {
background-color: #e0ffff;
background-color: #339aff;
right: -18px;
}

.chat-entry.remote .entry-bubble:hover::before {
background-color: #a9f6f6;
background-color: #80bdff;
}
28 changes: 21 additions & 7 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import './ChatEntry.css';
import TimeStamp from './TimeStamp';
import PropTypes from 'prop-types';

const ChatEntry = ({ id, sender, body, timeStamp, liked, onLike }) => {
const heartIcon = liked ? '💖' : '🤍';
const isLocal = sender === 'Chip';

const ChatEntry = () => {
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className={`chat-entry ${isLocal ? 'local' : 'remote'}`}>
<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} />
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 job making use of the provided TimeStamp component.

</p>
<button className="like" onClick={() => onLike(id)}>
{heartIcon}
</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,
onLike: PropTypes.func.isRequired,
};

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 ChatEntry from './ChatEntry';
import PropTypes from 'prop-types';

const ChatLog = ({ entries, onLike }) => {
return (
<div className="chat-log">
{entries.map((entry) => (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onLike={onLike}
/>
))}
</div>
);
};

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,
onLike: PropTypes.func.isRequired,
};

export default ChatLog;
Loading