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

const App = () => {

const [entries, setEntries] = useState(chatMessages);

const toggleLike = (id) => {
const entriesCopy = entries.map((entry) => {
if (entry.id === id) {
return {
id: entry.id,
sender: entry.sender,
body: entry.body,
timeStamp: entry.timeStamp,
liked: !entry.liked
};
Comment on lines +13 to +19

Choose a reason for hiding this comment

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

Nice managing of data & making sure we make a new object when we need to alter a message in our list!

A best practice for copying over an object's values is to use the spread operator, then overwrite any key/value pairs we want to change:

// Use the spread operator to copy all of entry's values to a new object, 
// then overwrite the attribute liked with the opposite of entry's liked value
return {
    ...entry, 
    liked: !entry.liked
}

} else return entry;
});
setEntries(entriesCopy);
};

const countLikes = () => {
let counter = 0;
for (let entry of entries) {
if (entry.liked) {
counter += 1;
};
};
return counter;
};
Comment on lines +25 to +33

Choose a reason for hiding this comment

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

Great use of the existing data to calculate the total likes! Another option would be to use the array function reduce:

    return messageData.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 Log</h1>
<h2>{countLikes()} ❤️s</h2>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
entries={entries}
setLiked={toggleLike}
/>
</main>
</div>
);
Expand Down
19 changes: 13 additions & 6 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 heartIcon = (props.liked) ? '❤️' : '🤍';
const messageAlign = (props.sender === 'Vladimir') ? 'remote' : 'local';
Comment on lines +7 to +8

Choose a reason for hiding this comment

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

Ternary operators! 🎉

return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className={`chat-entry ${messageAlign}`}>
<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"><TimeStamp time={props.timeStamp}/></p>
<button className="like" onClick={() => {props.setLiked(props.id)}}>{heartIcon}</button>

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 props.setLiked since it keeps all the state management and message object creation confined to App.

</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,
Comment on lines +22 to +26

Choose a reason for hiding this comment

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

Nice use of Prop-Types and flagging those necessary for render with isRequired!. To help our future selves (or other folks we're working with) easily see what is allowed to be passed when creating an instance of a component, I suggest including all props that can be passed in, including functions such as setLiked.

};

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

const ChatLog = (props) => {

const chatComponents = props.entries.map((entry) => {
return (
<ChatEntry
id = {entry.id}
key = {entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
setLiked={props.setLiked}
/>
);
});
return (
<section className="chat-log">{chatComponents}</section>
);
};

ChatLog.propTypes = {
entries: PropTypes.arrayOf(PropTypes.object).isRequired,
Comment on lines +22 to +26

Choose a reason for hiding this comment

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

We can be even more specific with our PropTypes for validation. If we know that we need an array, and that array's objects need to hold certain data, we can check the objects as well! Inside arrayOf we can pass PropTypes.shape() with information about the object's shape. A modified example from stack overflow is below:

list: PropTypes.arrayOf(
    PropTypes.shape({
        id: PropTypes.number.isRequired,
        customTitle: PropTypes.string.isRequired,
        btnStyle:PropTypes.object,
    })
).isRequired,

Source: https://stackoverflow.com/questions/59038307/reactjs-proptypes-validation-for-array-of-objects

Similar to some of the feedback for ChatEntry's PropTypes, it would be good to list the function ChatLog accepts here as well.


};

export default ChatLog;