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
3 changes: 2 additions & 1 deletion src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@

#App #heartWidget {
font-size: 1.5em;
margin: 1em
margin: 1em;
color: black
}

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

const App = () => {
// console.log(chatMessages);

// const [likedMap, setLikedMap] = useState({})
Comment on lines +8 to +10

Choose a reason for hiding this comment

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

Remove unused, commented out code

const [likedCount, setLikedCount] = useState(0);

const addLike = () => setLikedCount(likedCount + 1);
const removeLike = () => setLikedCount(likedCount - 1);
Comment on lines +13 to +14

Choose a reason for hiding this comment

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

Can you think of how you could combine the logic from addLike() and removeLike() into one method like updateLike()? The bodies of these 2 methods look very similar, the difference being + or - so maybe we can add some logic by passing in a boolean value

updateLike = isLiked => { // your logic in here }

Choose a reason for hiding this comment

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

Another comment about updating likedCount state with setLikedCount. When state represents something like a counter, we need to depend on a state's previous value and increment the previous value.

setLikedCount(prevState => prevState + 1)

This video here does a great job explaining why


return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat between Vladimir and Estragon</h1>
<section id="heartWidget">{likedCount} ❤️s</section>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
addLike={addLike}
removeLike={removeLike}
entries={chatMessages}
></ChatLog>
</main>
</div>
);
Expand Down
36 changes: 29 additions & 7 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
import React from 'react';
import { React, useState } from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = ({ sender, body, timeStamp, liked, addLike, removeLike }) => {
const [like, setLike] = useState(liked);

const handleOnClick = () => {
if (like) {
removeLike();
} else {
addLike();
}

setLike(!like);
};

const ChatEntry = (props) => {
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} />
</p>
<button className="like" onClick={handleOnClick}>
{like ? '❤️' : '🤍'}
</button>
</section>
</div>
);
};

ChatEntry.propTypes = {
//Fill with correct proptypes
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
liked: PropTypes.bool,
addLike: PropTypes.func,
removeLike: PropTypes.func,
Comment on lines +39 to +41

Choose a reason for hiding this comment

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

are these 3 required?

};

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 './ChatLog.css';
import PropTypes from 'prop-types';
import ChatEntry from './ChatEntry';

const ChatLog = ({ entries, addLike, removeLike }) => {
return (
<section className="chat-log">
{entries.map((message) => {
return (
<ChatEntry
key={message.id}
sender={message.sender}
body={message.body}
timeStamp={message.timeStamp}
liked={message.liked}
addLike={addLike}
removeLike={removeLike}
/>
);
})}
</section>
);
};

ChatLog.propTypes = {
entries: PropTypes.arrayOf(
PropTypes.shape({
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
liked: PropTypes.bool,
})
).isRequired,
addLike: PropTypes.func,
removeLike: PropTypes.func,
Comment on lines +35 to +36

Choose a reason for hiding this comment

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

Are these two functions required?

};

export default ChatLog;