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

const App = () => {
const [chatsData, setChat] = useState(messages);

const handleLikedMessage = (id) => {
setChat(chatsData => chatsData.map(message => {
if (message.id === id) {
return { ...message, liked: !message.liked };
} else {
return message;
}
}));
};

const calculateTotalLikes = (chatsData) => {
let total = 0;
for (const message of chatsData) {
if (message.liked === true) {
total += 1;
}
}
return total;
};

const totalLikes = calculateTotalLikes(chatsData);

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat between {messages[0].sender} and {messages[1].sender} </h1>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<h2>Total Number of Likes: {totalLikes} ❤️s</h2>
<ChatLog entries={chatsData} onLike={handleLikedMessage}/>
</main>
</div>
);
};

export default App;
export default App;
26 changes: 20 additions & 6 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import './ChatEntry.css';
import TimeStamp from './TimeStamp';
import PropTypes from 'prop-types';

const ChatEntry = ({id, sender, body, timeStamp, liked, onLike}) => {
const likedMessage = () =>{
onLike(id);
};

const messageWithHeart = liked ? '❤️': '🤍';


const ChatEntry = () => {
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={likedMessage}>{messageWithHeart}</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,
onLike: PropTypes.func.isRequired,
};

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


const ChatLog = ({entries, onLike}) => {
const ChatToEntry = (entries) => {
return entries.map((entry) =>{
return (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onLike={onLike} />
);
});
};
return (<ul className="chat-log">{ChatToEntry(entries)}</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,
onLike: PropTypes.func.isRequired,
};

export default ChatLog;