From 7064b980efb6165a2c13c7946a617e0eb37b3c11 Mon Sep 17 00:00:00 2001 From: Sno Date: Wed, 25 Jun 2025 17:40:44 -0700 Subject: [PATCH 1/2] waves completed and tests passed --- src/App.jsx | 32 ++++++++++++++++++++++++++++---- src/components/ChatEntry.jsx | 31 ++++++++++++++++++++++++------- src/components/ChatLog.jsx | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 src/components/ChatLog.jsx diff --git a/src/App.jsx b/src/App.jsx index 14a7f684d..dd616f1a0 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,38 @@ import './App.css'; +import ChatLog from './components/ChatLog'; +import chatMessages from './data/messages.json'; +import { useState } from 'react'; + +const calculateTotalLikes = (messages) => { + return messages.reduce((total, msg) => { + return total + (msg.liked ? 1 : 0); + }, 0); +}; const App = () => { + const [messages, setMessages] = useState(chatMessages); + + const toggleLike = (id) => { + const updatedMessages = messages.map((msg) => { + if (msg.id === id) { + return { ...msg, liked: !msg.liked }; + } else { + return msg; + } + }); + setMessages(updatedMessages); + }; + + const totalLikes = calculateTotalLikes(messages); + return ( -
+
-

Application title

+

React Chatlog

+

{totalLikes} ❤️s

- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +
); diff --git a/src/components/ChatEntry.jsx b/src/components/ChatEntry.jsx index 15c56f96b..50aa99dac 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,13 +1,24 @@ import './ChatEntry.css'; +import TimeStamp from './TimeStamp'; +import PropTypes from 'prop-types'; + +// The ChatEntry component should use an instance of the TimeStamp component. +// The TimeStamp component expects one prop: time. + +const ChatEntry = (props) => { + const heart = props.liked ? '❤️' : '🤍'; -const ChatEntry = () => { return ( -
-

Replace with name of sender

-
-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +
+

{props.sender}

+
+

{props.body}

+

+ +

+
); @@ -15,6 +26,12 @@ const ChatEntry = () => { 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; diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 000000000..240663c58 --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,35 @@ +import './ChatLog.css'; +import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; + +const ChatLog = (props) => { + return ( +
+ {props.entries.map((msg) => ( + + ))} +
+ ); +}; + +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.isRequired, + }) + ).isRequired, + onLikeToggle: PropTypes.func.isRequired, +}; + +export default ChatLog; From 9278ab434771f13de4526dffba32397846d6afc3 Mon Sep 17 00:00:00 2001 From: Sno Date: Wed, 25 Jun 2025 17:46:19 -0700 Subject: [PATCH 2/2] removed unecessary comments --- src/components/ChatEntry.jsx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/components/ChatEntry.jsx b/src/components/ChatEntry.jsx index 50aa99dac..ba474c7ad 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -2,9 +2,6 @@ import './ChatEntry.css'; import TimeStamp from './TimeStamp'; import PropTypes from 'prop-types'; -// The ChatEntry component should use an instance of the TimeStamp component. -// The TimeStamp component expects one prop: time. - const ChatEntry = (props) => { const heart = props.liked ? '❤️' : '🤍'; @@ -25,7 +22,6 @@ const ChatEntry = (props) => { }; ChatEntry.propTypes = { - // Fill with correct proptypes id: PropTypes.number.isRequired, sender: PropTypes.string.isRequired, body: PropTypes.string.isRequired,