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
24 changes: 20 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
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)

const likeCount = chatEntries.filter((entry) => entry.liked).length

Choose a reason for hiding this comment

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

Great use of filter & the message state to find the heart count!


const updateLike = (chatEntryId) => {
const updatedEntries = chatEntries.map((entry) => {
if (entry.id === chatEntryId) {
return { ...entry, liked: !entry.liked};

Choose a reason for hiding this comment

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

Nice! 🙌🏻

}
return entry;
})

setChatEntries(updatedEntries);
}

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat 🪵 {likeCount} ❤️s</h1>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={chatEntries} updateLike={updateLike} />
</main>
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion src/components/ChatEntry.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ button {

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

22 changes: 17 additions & 5 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import React from 'react';
// import TimeStamp from './TimeStamp';
import './ChatEntry.css';
import PropTypes from 'prop-types';

const ChatEntry = (props) => {
const heart = props.liked ? '❤️' : '🤍';

const handleLikeButton = () => {
props.updateLike(props.id);
};

return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<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">{props.timeStamp} years ago</p>
<button className="like" onClick={handleLikeButton} >{heart} </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,
updateLike: PropTypes.func.isRequired,
};

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

const ChatLog = (props) => {
return (
<ul>
{props.entries.map((entry) => (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
updateLike={props.updateLike}
/>
))}
</ul>
)
}

ChatLog.propTypes = {
entries: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number.isRequired,
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
liked: PropTypes.bool.isRequired
})
).isRequired,
// likeDecrease: PropTypes.func.isRequired,
updateLike: PropTypes.func.isRequired,
}

export default ChatLog;