From ac65c2ff8462c25400861942128f49cc889beed8 Mon Sep 17 00:00:00 2001 From: Brian Martinez <100006298+bamart-dev@users.noreply.github.com> Date: Mon, 16 Jun 2025 08:31:16 -0700 Subject: [PATCH 1/4] implement ChatLog component --- src/components/ChatLog.jsx | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/components/ChatLog.jsx diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 000000000..0a9da0a5f --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,33 @@ +import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; + +const ChatLog = (props) => { + return ( +
+ {props.messages.map((message) => ( + + ))} +
); +}; + +ChatLog.propTypes = { + messages: PropTypes.arrayOf( + PropTypes.shape({ + id: PropTypes.number.isRequired, + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + }) + ).isRequired, + onLikeClick: PropTypes.func.isRequired, +}; + +export default ChatLog; From c797d9e532201cd0d313aa3245b36ae276c1e307 Mon Sep 17 00:00:00 2001 From: Brian Martinez <100006298+bamart-dev@users.noreply.github.com> Date: Mon, 16 Jun 2025 08:33:02 -0700 Subject: [PATCH 2/4] refactor App.jsx & ChatEntry.jsx to integrate ChatLog component; set up useState in App.jsx w/ functions --- src/App.jsx | 27 ++++++++++++++++++++++++--- src/components/ChatEntry.jsx | 19 +++++++++++++------ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 14a7f684d..d25f459f2 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,35 @@ import './App.css'; +import ChatLog from './components/ChatLog'; +import messages from './data/messages.json'; +import { useState } from 'react'; const App = () => { + const [messageLogs, setMessageLogs] = useState(messages); + + const toggleLike = (id) => { + const update = messageLogs.map((message) => { + if (message.id === id) { + return {...message, liked: !message.liked}; + } else { + return message; + } + }); + + setMessageLogs(update); + }; + + const getLikeCount = () => { + return messageLogs.filter((message) => message.liked).length; + }; + return (
-

Application title

+

Messages

+

{getLikeCount()} Likes

- {/* 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..93d030c56 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,20 +1,27 @@ import './ChatEntry.css'; +import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; -const ChatEntry = () => { +const ChatEntry = (props) => { return (
-

Replace with name of sender

+

{props.sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{props.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, + onLikeClick: PropTypes.func.isRequired }; export default ChatEntry; From 876814dd8a73ee20ad615d01e156507333cf28ac Mon Sep 17 00:00:00 2001 From: Brian Martinez <100006298+bamart-dev@users.noreply.github.com> Date: Mon, 16 Jun 2025 08:37:16 -0700 Subject: [PATCH 3/4] slight refactor in App.jsx --- src/App.jsx | 2 ++ src/components/ChatEntry.jsx | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/App.jsx b/src/App.jsx index d25f459f2..26365ad2f 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -6,6 +6,7 @@ import { useState } from 'react'; const App = () => { const [messageLogs, setMessageLogs] = useState(messages); + // handles what happens when 'like' button is clicked: const toggleLike = (id) => { const update = messageLogs.map((message) => { if (message.id === id) { @@ -18,6 +19,7 @@ const App = () => { setMessageLogs(update); }; + // helper f(x) to easily retrieve/display like count: const getLikeCount = () => { return messageLogs.filter((message) => message.liked).length; }; diff --git a/src/components/ChatEntry.jsx b/src/components/ChatEntry.jsx index 93d030c56..63597571e 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -9,7 +9,7 @@ const ChatEntry = (props) => {

{props.body}

- +
); From 2d6b65f58916e22b80d2a09df9ac396be0f37d26 Mon Sep 17 00:00:00 2001 From: Brian Martinez <100006298+bamart-dev@users.noreply.github.com> Date: Mon, 16 Jun 2025 08:58:07 -0700 Subject: [PATCH 4/4] refactor App.jsx & ChatLog.jsx (changed messages to entries to pass tests) --- src/App.jsx | 4 ++-- src/components/ChatLog.jsx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 26365ad2f..ea60bb7c1 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -28,10 +28,10 @@ const App = () => {

Messages

-

{getLikeCount()} Likes

+

{getLikeCount()} ❤️s

- +
); diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx index 0a9da0a5f..f04d92dd2 100644 --- a/src/components/ChatLog.jsx +++ b/src/components/ChatLog.jsx @@ -4,7 +4,7 @@ import PropTypes from 'prop-types'; const ChatLog = (props) => { return (
- {props.messages.map((message) => ( + {props.entries.map((message) => ( { }; ChatLog.propTypes = { - messages: PropTypes.arrayOf( + entries: PropTypes.arrayOf( PropTypes.shape({ id: PropTypes.number.isRequired, sender: PropTypes.string.isRequired,