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

const App = () => {
const [entries, setEntries] = useState(messagesData);

const calcTotalLikes = () => {
return entries.filter(entry => entry.liked).length;
};

const toggleLike = (id) => {
setEntries(entries.map(entry =>
entry.id === id ? { ...entry, liked: !entry.liked } : entry
));
};

const totalLikes = calcTotalLikes();

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat between Vladimir and Estragon</h1>
<p>{totalLikes} ❤️s</p>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={entries} onLike={toggleLike} />
</main>
</div>
);
Expand Down
24 changes: 17 additions & 7 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import './ChatEntry.css';
import TimeStamp from './TimeStamp';
import PropTypes from 'prop-types';

const ChatEntry = () => {
const ChatEntry = ({ sender, body, timeStamp, liked, onLike }) => {
const isLocal = sender === 'Estragon';

return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className={`chat-entry ${isLocal ? 'local' : 'remote'}`}>
<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={onLike}>
{liked ? '❤️' : '🤍'}
</button>
</section>
</div>
);
};

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

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

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

ChatLog.propTypes = {
entries: PropTypes.array.isRequired,
onLike: PropTypes.func.isRequired,
};

export default ChatLog;