Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 21 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import React from 'react';
import React, {useState} from 'react';
import './App.css';
import ChatLog from './components/ChatLog';
import chatMessages from './data/messages.json';


const App = () => {
const [chatEntries, setChatEntries] = useState(chatMessages)

// this works bc we are setting the state of the page in like 21 which will rerender the page(app component) everytime the buttone is pressed.
const likeCount = chatEntries.filter((entry) => entry.liked).length

Choose a reason for hiding this comment

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

Great use of filter & the message state to find the heart count!


const updateLike = (chatEntryId) => {
const updatedEntries = chatEntries.map((entry) => {
if (entry.id === chatEntryId) {
entry.liked = !entry.liked;
}
return entry;
})

setChatEntries(updatedEntries);
}

Choose a reason for hiding this comment

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

The way likeEntry updates the messages data causes one of the tests to fail depending on the order it runs in.

"In react-chatlog, inadvertently mutating the state object leads to inadvertently mutating the module-static messages variable, which has the side effect of persisting “state” across the two test runs.

Our useState data chatEntries is intended to be read-only, but we're editing the existing entries on line 16. What we want to do is make a copy of the message object we're updating, change the liked value on the copy, then return that new object. Essentially, we can make a shallow copy of objects that are not changing, but we need a deep copy of any objects we're updating. One way we could do that might look like:

  const updateLike = (chatEntryId) => {
    const updatedEntries = chatEntries.map((entry) => {
      if (entry.id === chatEntryId) {
        // Use spread operator to copy all of entry's values to a new object, 
        // then overwrite the attribute liked with the opposite of what entry's liked value was
        return { ...entry, liked: !entry.liked }
      } 

      // We didn't enter the if-statement, so this entry is not changing. 
      // Return the existing data
      return chat;
    });
    
    setChatEntries(updatedEntries);
  };


return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat 🪵 {likeCount}</h1>

Choose a reason for hiding this comment

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

The test for the heart count uses screen.getByText(/3 ❤️s/) to find the heart count on the screen. Right now the page displays the count by itself, how can we update the text we display to pass the tests?

</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={chatEntries} updateLike={updateLike} />
</main>
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion src/components/ChatEntry.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ button {

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

24 changes: 19 additions & 5 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import React from 'react';
// import TimeStamp from './TimeStamp';
import './ChatEntry.css';
import PropTypes from 'prop-types';

const ChatEntry = (props) => {
const heart = props.liked ? '🤍' : '❤️';
console.log(heart)

Choose a reason for hiding this comment

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

We want to delete logging statements and other diagnostic code before opening PRs to ensure we don't bring them into the final codebase.


const handleLikeButton = () => {
props.updateLike(props.id);
};

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">{props.timeStamp}</p>

Choose a reason for hiding this comment

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

There are a couple things to look at around the time:

  • This prop is empty so the time never shows, how is this data being passed from ChatLog to ChatEntry? What would we need to update for this data to flow through like we expect?
  • The TimeStamp component takes props.timeStamp and converts it to a text representation. There is a test that looks for expect(screen.getByText(/\d+ years ago/)).toBeInTheDocument() which finds the text generated by a TimeStamp instance. How could we create an instance of TimeStamp here and pass it a prop containing props.timeStamp?

<button className="like" onClick={handleLikeButton} >{heart} </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,
updateLike: PropTypes.func.isRequired,
// likeDecrease: PropTypes.func.isRequired,
};

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

const ChatLog = (props) => {
return (
<ul>
{props.entries.map((entry) => (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.time}

Choose a reason for hiding this comment

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

I see entry.time is being passed, does this key exist in the message data in the .json file?

liked={entry.liked}
updateLike={props.updateLike}
/>
))}
</ul>
)
}

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,
// likeDecrease: PropTypes.func.isRequired,
updateLike: PropTypes.func.isRequired,
}

export default ChatLog;