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
35 changes: 31 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import React from 'react';
import React, { useState } from 'react';
import './App.css';
import chatMessages from './data/messages.json';
import ChatEntry from './components/ChatEntry';
import ChatLog from './components/ChatLog';


const App = () => {

const [chatData, setChatData] = useState(chatMessages);
let [likeCount, setLikeCount] = useState(0);
// let likeCount = 0;

Choose a reason for hiding this comment

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

Gentle reminder we want to clean up commented out code so it doesn't get into our final codebase.


const onLikeClick = (id) => {
setChatData(chatData.map(chat => {
if (chat.id === id){
chat.liked = !chat.liked
if (chat.liked === true){
setLikeCount(likeCount += 1)
};
if (chat.liked === false){
setLikeCount(likeCount -= 1)
};
Comment on lines +18 to +23

Choose a reason for hiding this comment

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

Our state chatData contains info on whether a message is liked or not, which means we could derive our heart count from the data by looping over chatData and counting up how many entries have a liked value of True. This would let us remove the likeCount state that we need to manually keep in sync with the changes to chatData. What changes would we need to make for that to work?


return chat;
}
return chat;
}));
};
Comment on lines +14 to +29

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 is what's causing 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 chatData is intended to be read-only, but we're editing the existing entries on line 15. 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 onLikeClick = (id) => {
    const newEntries = chatData.map((chat) => {
      if (chat.id === id) {
        // 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 {...chat, liked: !chat.liked}
      } 

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




return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>ChatBug</h1>
<h2>{likeCount} ❤️s</h2>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<div><ChatLog entries={chatData} onClick={onLikeClick}></ChatLog></div>
</main>
</div>
);
Expand Down
21 changes: 14 additions & 7 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = (props) => {
const ChatEntry = ({id, sender, body, timeStamp, liked, onClick}) => {
const heartColor = liked === false ? '🤍' : '❤️';
const likeButton = () => {onClick(id)};
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></p>
<button className="like" onClick={likeButton}>{heartColor}</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,
onClick: PropTypes.func.isRequired,
};

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

const ChatLog = (props) => {
const getChat = props.entries.map((chat) => {
return (
<ChatEntry id={chat.id}
sender={chat.sender}
body={chat.body}
timeStamp={chat.timeStamp}
liked={chat.liked}
onClick={props.onClick}
key={chat.id}
/>
)
})
return <div className='chat-log'>{getChat}</div>

Choose a reason for hiding this comment

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

I'm seeing some divs for component wrappers that we could replace with more semantic HTML. I'd consider swapping out the enclosing div here for something like a ul to better represent that this is a list of items being displayed. We could also swap out the div that wraps the ChatEntry components to be a li; together these changes could help increase accessibility of the page overall.

};

ChatLog.propTypes = {
entries: PropTypes.arrayOf(
PropTypes.shape({

Choose a reason for hiding this comment

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

Great use of PropTypes & PropTypes.shape!

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


export default ChatLog;