diff --git a/src/App.jsx b/src/App.jsx index 14a7f684..d4aa5888 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -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 (
-

Application title

+

Chat Between {messagesData[0].sender} and {messagesData[1].sender}

+
+ {likedCount} ❤️s +
- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +
); }; -export default App; +export default App; \ No newline at end of file diff --git a/src/components/ChatEntry.jsx b/src/components/ChatEntry.jsx index 15c56f96..b6d2c57c 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -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 ( -
-

Replace with name of sender

+
+

{sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{body}

+

+ +

+
); }; 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; \ No newline at end of file diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 00000000..18bbe522 --- /dev/null +++ b/src/components/ChatLog.jsx @@ -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 ( + + ); + }); + + return ( +
+ {chatEntryComponents} +
+ ); +}; + +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; \ No newline at end of file