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

Application title

+

Chat between {messages[0].sender} and {messages[1].sender}

- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +

Total Number of Likes: {totalLikes} ❤️s

+
); }; -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 15c56f96b..9750c042d 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -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 (
-

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, + onLike: PropTypes.func.isRequired, }; export default ChatEntry; diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 000000000..2460b3574 --- /dev/null +++ b/src/components/ChatLog.jsx @@ -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 ( + + ); + }); + }; + return (); +}; + +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; \ No newline at end of file