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

const App = () => {
const [chatData, setChatData] = useState(messagesData);
const [likedCount, setLikedCount] = useState(0);
const toggleLike = (chatEntryId) => {
setChatData((prevChatData) => {
const updatedChatData = prevChatData.map((chatEntry) => {
if (chatEntry.id === chatEntryId) {
return { ...chatEntry, liked: !chatEntry.liked };
} else {
return chatEntry;
}
});
let totalLikes = 0;
updatedChatData.forEach((chatEntry) => {
if (chatEntry.liked) {
totalLikes++;
}
});
setLikedCount(totalLikes);
return updatedChatData;
});
};

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat Between {messagesData[0].sender} and {messagesData[1].sender}</h1>
<section>
<span className="widget" id="heartWidget">{likedCount} ❤️s</span>
</section>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
entries={chatData}
onLikeToggle={toggleLike}
></ChatLog>
</main>
</div>
);
};

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

const ChatEntry = ({ id, sender, body, timeStamp, liked, onLikeToggle }) => {
const likeButtonClick = () => {
onLikeToggle(id);
};
const senderType = (sender === 'Vladimir') ? 'local' : 'remote';

const ChatEntry = () => {
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className={`chat-entry ${senderType}`}>
<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}></TimeStamp>
</p>
<button className="like" onClick={likeButtonClick}>{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,
onLikeToggle: PropTypes.func.isRequired,
};

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

const ChatLog = ({ entries, onLikeToggle }) => {
const chatEntryComponents = entries.map((chatEntry) => {
return (
<ChatEntry
key={chatEntry.id}
id={chatEntry.id}
sender={chatEntry.sender}
body={chatEntry.body}
timeStamp={chatEntry.timeStamp}
liked={chatEntry.liked}
onLikeToggle={onLikeToggle}
></ChatEntry>
);
});

return (
<section className="chat-log">
{chatEntryComponents}
</section>
);
};

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

export default ChatLog;