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
4 changes: 4 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
align-items: center;
}

header section p {
color: black;
}

#App main {
padding-left: 2em;
padding-right: 2em;
Expand Down
25 changes: 22 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import './App.css';
import ChatLog from './components/ChatLog';
import { useState } from 'react';
import messagesData from './data/messages.json';


const App = () => {
const [messages, setMessages] = useState(messagesData);
const totalLikes = messages.filter(message => message.liked).length;

// Function to toggle like status of a message
const toggleLike = (id) => {
setMessages(messages.map(message => {
if (message.id === id) {
return { ...message, liked: !message.liked };
}
return message;
}));
};

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

const ChatEntry = ({id, sender, body, timeStamp, liked, onToggleLike }) => {

const handleLikeClick = () => {
onToggleLike(id);
};


const ChatEntry = () => {
return (
// Replace the outer tag name with a semantic element that fits our use case
<replace-with-relevant-semantic-element className="chat-entry local">
<h2 className="entry-name">Replace with name of 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>
</section>
</replace-with-relevant-semantic-element>
<section className="chat-entry local">
<h2 className="entry-name">{sender}</h2>
<div className="entry-bubble">
<p>{body}</p>
<p className="entry-time">
<TimeStamp time={timeStamp} />
</p>
<button className="like" onClick={handleLikeClick}>
{liked ? '❤️' : '🤍'}

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

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,
onToggleLike: PropTypes.func.isRequired,
};

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

// ChatLog component to render a list of ChatEntry components
const ChatLog = ({ entries, onToggleLike }) => {
return (
<ul className="chat-log">
{entries.map((entry) => (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onToggleLike={onToggleLike}
/>
))}
</ul>
);
};

// Prop type validation for ChatLog component
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,
onToggleLike: PropTypes.func.isRequired,
};

export default ChatLog;