From 27b031ac2b72c9a8effc6966e8329525e08a1ba0 Mon Sep 17 00:00:00 2001 From: marisela Date: Sun, 15 Jun 2025 19:16:10 -0700 Subject: [PATCH 1/2] added import chatmessages into app.js and imported proptype and time stamp to chatentry, also added sender into the component and used timestamp --- src/App.jsx | 8 ++++++-- src/components/ChatEntry.jsx | 17 ++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 14a7f684d..0a5ff129b 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,18 @@ import './App.css'; +import ChatEntry from './components/ChatEntry'; +import chatMessages from './data/messages.json'; const App = () => { + const firstMessage = chatMessages[0]; return (

Application title

- {/* 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..abdfbc8a1 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,12 +1,17 @@ import './ChatEntry.css'; +import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; -const ChatEntry = () => { + +const ChatEntry = ({sender, body,timeStamp}) => { return (
-

Replace with name of sender

+

{sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

+

{body}

+

+ +

@@ -14,7 +19,9 @@ const ChatEntry = () => { }; ChatEntry.propTypes = { - // Fill with correct proptypes + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired }; export default ChatEntry; From 8cef1be151af5d0a1a9703f8421f7c20a20f4d16 Mon Sep 17 00:00:00 2001 From: marisela Date: Sun, 15 Jun 2025 20:58:29 -0700 Subject: [PATCH 2/2] added usestate for chat data, as ell as getting total likes in chatlog , passed entries chat data as well as the toglelike prop --- src/App.jsx | 29 ++++++++++++++++++++------- src/components/ChatEntry.jsx | 9 ++++++--- src/components/ChatLog.jsx | 35 +++++++++++++++++++++++++++++++++ src/components/ChatLog.test.jsx | 2 +- 4 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 src/components/ChatLog.jsx diff --git a/src/App.jsx b/src/App.jsx index 0a5ff129b..af00c6d9f 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,21 +1,36 @@ import './App.css'; -import ChatEntry from './components/ChatEntry'; +import ChatLog from './components/ChatLog'; import chatMessages from './data/messages.json'; +import { useState } from 'react'; const App = () => { - const firstMessage = chatMessages[0]; + const [chatData, setChatData] = useState(chatMessages); + + const toggleLike = (id) => { + const updatedMessages = chatData.map((entry) => { + if (entry.id === id) { + return { ...entry, liked: !entry.liked }; + } + return entry; + }); + setChatData(updatedMessages); + }; + + const getTotalLikes = () => { + return chatData.filter((entry) => entry.liked).length; + }; + return (
-

Application title

+

Chat App

+

{getTotalLikes()} ❤️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 abdfbc8a1..f4b5dbda2 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; import TimeStamp from './TimeStamp'; -const ChatEntry = ({sender, body,timeStamp}) => { +const ChatEntry = ({id,sender, body,timeStamp, liked,onLikeToggle}) => { return (

{sender}

@@ -12,16 +12,19 @@ const ChatEntry = ({sender, body,timeStamp}) => {

- +
); }; ChatEntry.propTypes = { + id: PropTypes.number.isRequired, sender: PropTypes.string.isRequired, body: PropTypes.string.isRequired, - timeStamp: PropTypes.string.isRequired + liked: PropTypes.bool.isRequired, + timeStamp: PropTypes.string.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..fa878d98c --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,35 @@ +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; + +const ChatLog = ({ entries, onLikeToggle }) => { + return ( +
+ {entries.map((entry) => ( + + ))} +
+ ); +}; + +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 diff --git a/src/components/ChatLog.test.jsx b/src/components/ChatLog.test.jsx index dfcfeda99..9e3637b0c 100644 --- a/src/components/ChatLog.test.jsx +++ b/src/components/ChatLog.test.jsx @@ -1,4 +1,4 @@ -import ChatLog from './ChatLog'; +import ChatLog from './ChatLog.jsx'; import { render, screen } from '@testing-library/react'; import '@testing-library/jest-dom';