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
36 changes: 34 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
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);

let initalNumLiked = 0;
for (const message of chatEntries) {
if (message.liked) {
initalNumLiked++;
}
}

const [numLiked, setNumLiked] = useState(initalNumLiked);

Choose a reason for hiding this comment

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

Since the liked status of a message lives in the chatEntries data we should avoid holding an extra variable or piece of state that we need to manually keep in sync. We can 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


const updateHeartChatEntries = (updatedMessage) => {
const updatedChat = chatEntries.map((message) => {
if (message.id === updatedMessage.id) {
if (updatedMessage.liked) {
setNumLiked(numLiked + 1);
} else {
setNumLiked(numLiked - 1);
}
return updatedMessage;
} else {
return message;
}
});
setChatEntries(updatedChat);
};

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>{numLiked} ❤️s</h1>
</header>
<main>
<ChatLog
entries={chatEntries}
onUpdateChatHeart={updateHeartChatEntries}
/>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
</main>
Expand Down
44 changes: 36 additions & 8 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = ({
id,
sender,
body,
timeStamp,
liked,
onUpdateChatHeart,
}) => {
const likeOrUnlikeMessage = () => {
const updatedMessage = {
id: id,
sender: sender,
body: body,
timeStamp: timeStamp,
liked: !liked,
};
onUpdateChatHeart(updatedMessage);
};
Comment on lines +13 to +22

Choose a reason for hiding this comment

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

I would consider passing the id of the message clicked to props.likeOrUnlikeMessage and having the App code handle the new object creation. When ChatEntry creates the new object for the App state, it takes some responsibility for managing those contents. If we want the responsibility of managing the state to live solely with App, we would want it to handle defining the new message object.

This made me think of a related concept in secure design for APIs. Imagine we had an API for creating and updating messages, and it has an endpoint /<msg_id>/like meant to update the true/false liked value. We could have that endpoint accept a body in the request and let the user send an object with data for the message's record (similar to passing a message object from ChatEntry to App), but the user could choose to send any data for those values. If the endpoint only takes in an id and handles updating the liked status for the message itself, there is less opportunity for user error or malicious action.

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

const ChatEntry = (props) => {
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className="chat-entry local" id={id}>
<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} />
</p>
<button className="like" onClick={likeOrUnlikeMessage}>
{heartShape}
</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,
onUpdateChatHeart: PropTypes.func.isRequired,
};

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

const chatLog = ({ entries, onUpdateChatHeart }) => {
const chatEntries = entries.map((entry) => {
return (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onUpdateChatHeart={onUpdateChatHeart}
/>
);
});

return <section>{chatEntries}</section>;
};

export default chatLog;

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.

Really nice use of PropTypes.

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