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: 4 additions & 2 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@
font-size: 1.5em;
text-align: center;
display: inline-block;

}

#App header section {
background-color: #e0ffff;
background-color: #222;
}

#App .widget {
display: inline-block;
line-height: 0.5em;
border-radius: 10px;
color: black;
color:rgb(rgb(21, 138, 233), green, blue);
font-size:0.8em;
padding-left: 1em;
padding-right: 1em;
Expand All @@ -43,6 +44,7 @@
#App #heartWidget {
font-size: 1.5em;
margin: 1em

}

#App span {
Expand Down
41 changes: 38 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
import React from 'react';
import './App.css';
import ChatLog from './components/ChatLog';
import chatMessages from './data/messages.json';
import { useState } from 'react';

Choose a reason for hiding this comment

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

We could combine the react imports into a single line:

import React, { useState } from 'react';


let numberOfLikes = 0;

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 MessageData data we should avoid holding an extra 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 (our like count).

// 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 = 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

const App = () => {
const [MessageData, setMessageData] = useState(chatMessages)

Choose a reason for hiding this comment

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

Javascript naming best practices uses camel case for local variables, I recommend messageData for the variable name here.


const updateMessageData = updatedChatEntry =>{
const entries = MessageData.map((chat) => {
if(chat.id === updatedChatEntry.id){
numberOfLikes = updatedChatEntry.liked ? numberOfLikes+1 : numberOfLikes-1;
return updatedChatEntry;
} else {
return chat;
}
});
setMessageData(entries);
};

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat between VLadimir and Estragon
<section>
<p className="widget" id="heartWidget">
{numberOfLikes} ❤️s
</p>
</section>
</h1>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}

{/* Wave 01 */}
{/* <ChatEntry
sender = {chatMessages[0].sender}
body = {chatMessages[0].body}
timeStamp = {chatMessages[0].timeStamp}
>
</ChatEntry> */}

<ChatLog
entries={MessageData}
onUpdatechat={updateMessageData}>
</ChatLog>

</main>
</div>
);
Expand Down
8 changes: 8 additions & 0 deletions src/components/ChatEntry.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,19 @@ button {
/* "local" messages are shown on the left side */
.chat-entry.local {
text-align: left;
color:green;
}


.chat-entry.local .entry-time {
text-align: right;
color:green
}

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

}

.chat-entry.local .entry-bubble:hover::before {
Expand All @@ -74,20 +78,24 @@ button {
/* "remote" messages are shown on the right side, in blue */
.chat-entry.remote {
text-align: right;
color: blue
}

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

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

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

.chat-entry.remote .entry-bubble::before {
Expand Down
34 changes: 28 additions & 6 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = (props) => {
const onFlipHeartClick = () => {

const updatedChatEntry = {
id : props.id,
sender : props.sender,
body : props.body,
timeStamp : props.timeStamp,
liked : !props.liked
};
props.onUpdate(updatedChatEntry);
};
Comment on lines +7 to +17

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.onUpdate 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 a 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 HeartColor = props.liked ? '❤️':'🤍';
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div
className={'chat-entry ' + (props.sender === 'Vladimir' ? 'local' : 'remote')}>

Choose a reason for hiding this comment

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

Having the decision logic in the JSX may make it harder to read quickly. Another option could be to have an interpolated string here that always holds chat-entry and use a placeholder where we pass only the remote or local class name:

const entryClassName = (props.sender === 'Vladimir') ? 'local' : 'remote';
...
<div className={`chat-entry ${entryClassName}`}>

<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 onClick={onFlipHeartClick}className="like" >
{HeartColor}
</button>

</section>
</div>
);
};

ChatEntry.propTypes = {
//Fill with correct proptypes
id: PropTypes.number,
sender: PropTypes.string,
body: PropTypes.string,
timeStamp: PropTypes.string,
liked: PropTypes.bool,
onUpdate: PropTypes.func,
};

export default ChatEntry;
2 changes: 1 addition & 1 deletion src/components/ChatEntry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("Wave 01: ChatEntry", () => {
test("that it will display the body", () => {
expect(screen.getByText(/Get out by 8am/)).toBeInTheDocument();
});

test("that it will display the time", () => {
expect(screen.getByText(/\d+ years ago/)).toBeInTheDocument();
});
Expand Down
44 changes: 44 additions & 0 deletions src/components/ChatLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import './ChatLog.css';
import PropTypes from 'prop-types';
import ChatEntry from './ChatEntry';

const ChatLog = (props) => {
const chatentryComponents =props.entries.map((chat,index)=>{
return (
<div key={index} className="chat-log">

Choose a reason for hiding this comment

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

If the messages all have unique ids, then we could use those values for the key over generating and using indices.

<ChatEntry
id = {chat.id}
sender ={chat.sender}
body = {chat.body}
timeStamp = {chat.timeStamp}
liked = {chat.liked}
onUpdate = {props.onUpdatechat}
></ChatEntry>
</div>
);
});
return (
<section>
<h2>Chat</h2>
<ul>
{chatentryComponents};
</ul>
</section>
);
};

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,
sender: PropTypes.string,
body: PropTypes.string,
timeStamp: PropTypes.string,
liked: PropTypes.bool,
})
).isRequired,

onUpdatechat: PropTypes.func
};

export default ChatLog;