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
29 changes: 26 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
import './App.css';
import ChatLog from './components/ChatLog';
import messages from './data/messages.json';
import { useState } from 'react';

const App = () => {
const [messageLogs, setMessageLogs] = useState(messages);

// handles what happens when 'like' button is clicked:
const toggleLike = (id) => {
const update = messageLogs.map((message) => {
if (message.id === id) {
return {...message, liked: !message.liked};
} else {
return message;
}
});

setMessageLogs(update);
};

// helper f(x) to easily retrieve/display like count:
const getLikeCount = () => {
return messageLogs.filter((message) => message.liked).length;
};

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Messages</h1>
<p><sub>{getLikeCount()} ❤️s</sub></p>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={messageLogs} onLikeClick={toggleLike} />
</main>
</div>
);
Expand Down
19 changes: 13 additions & 6 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = () => {
const ChatEntry = (props) => {
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"><TimeStamp time={props.timeStamp} /></p>
<button className="like" onClick={() => {props.onLikeClick(props.id);}}>{props.liked ? '❤️' : '🤍'}</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,
onLikeClick: PropTypes.func.isRequired
};

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

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

ChatLog.propTypes = {
entries: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number.isRequired,
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
})
).isRequired,
onLikeClick: PropTypes.func.isRequired,
};

export default ChatLog;