From 7752c96a3e8d49771d217680a4d381b26529567b Mon Sep 17 00:00:00 2001 From: Aksana Date: Sun, 15 Dec 2024 14:52:59 -0800 Subject: [PATCH 1/3] upda ted ChatEntry and App components, implemented ChatLog comp onent --- src/App.jsx | 11 ++++++++--- src/components/ChatEntry.jsx | 17 ++++++++++++----- src/components/ChatLog.jsx | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 src/components/ChatLog.jsx diff --git a/src/App.jsx b/src/App.jsx index 14a7f684d..6e54d7b17 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,17 +1,22 @@ +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); return (

Application title

- {/* 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 15c56f96b..5a6c147ce 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,19 +1,26 @@ import './ChatEntry.css'; +import TimeStamp from './TimeStamp'; +import PropTypes from 'prop-types'; -const ChatEntry = () => { +const ChatEntry = ({id, sender, body, timeStamp, liked}) => { return (
-

Replace with name of sender

+

{sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{body}

+

+
); }; ChatEntry.propTypes = { + id: PropTypes.number.isRequired, + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.isRequired, // Fill with correct proptypes }; diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 000000000..0594487d2 --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,34 @@ +import PropTypes from 'prop-types'; +import './ChatLog.css'; +import ChatEntry from './ChatEntry.jsx'; + + +const ChatLog = ({entries}) => { + 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,}) + ) +}; + +export default ChatLog; \ No newline at end of file From be7f94a4eb8caa29d10f0b641fdd49f5a208b149 Mon Sep 17 00:00:00 2001 From: Aksana Date: Sun, 15 Dec 2024 22:37:13 -0800 Subject: [PATCH 2/3] added behaviour to heart button in ChatEntry, managed click event and state of chat entries, created a helper function to calculate the number of likes --- src/App.jsx | 30 ++++++++++++++++++++++++++---- src/components/ChatEntry.jsx | 13 ++++++++++--- src/components/ChatLog.jsx | 10 ++++++---- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 6e54d7b17..d9ef58856 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -5,15 +5,37 @@ 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}

- - - +

Total Number of Likes: {totalLikes}

+
); diff --git a/src/components/ChatEntry.jsx b/src/components/ChatEntry.jsx index 5a6c147ce..179b8b005 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -2,14 +2,21 @@ import './ChatEntry.css'; import TimeStamp from './TimeStamp'; import PropTypes from 'prop-types'; -const ChatEntry = ({id, sender, body, timeStamp, liked}) => { +const ChatEntry = ({id, sender, body, timeStamp, liked, onLike}) => { + const likedMessage = () =>{ + onLike(id); + }; + + const messageWithHeart = liked ? '❤️': '🤍'; + + return (

{sender}

{body}

- +
); @@ -21,7 +28,7 @@ ChatEntry.propTypes = { body: PropTypes.string.isRequired, timeStamp: PropTypes.string.isRequired, liked: PropTypes.bool.isRequired, - // Fill with correct proptypes + onLike: PropTypes.func.isRequired, }; export default ChatEntry; diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx index 0594487d2..2460b3574 100644 --- a/src/components/ChatLog.jsx +++ b/src/components/ChatLog.jsx @@ -3,7 +3,7 @@ import './ChatLog.css'; import ChatEntry from './ChatEntry.jsx'; -const ChatLog = ({entries}) => { +const ChatLog = ({entries, onLike}) => { const ChatToEntry = (entries) => { return entries.map((entry) =>{ return ( @@ -13,7 +13,8 @@ const ChatLog = ({entries}) => { sender={entry.sender} body={entry.body} timeStamp={entry.timeStamp} - liked={entry.liked} /> + liked={entry.liked} + onLike={onLike} /> ); }); }; @@ -27,8 +28,9 @@ ChatLog.propTypes = { sender: PropTypes.string.isRequired, body: PropTypes.string.isRequired, timeStamp: PropTypes.string.isRequired, - liked: PropTypes.bool.isRequired,}) - ) + liked: PropTypes.bool.isRequired, + })).isRequired, + onLike: PropTypes.func.isRequired, }; export default ChatLog; \ No newline at end of file From 0e51df5c8a1b01276c0ba445a019ca70fcfd5f82 Mon Sep 17 00:00:00 2001 From: Aksana Date: Sun, 15 Dec 2024 23:05:53 -0800 Subject: [PATCH 3/3] updated ChatEntry for passing tests --- src/App.jsx | 2 +- src/components/ChatEntry.jsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index d9ef58856..8f6a5a88d 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -34,7 +34,7 @@ const App = () => {

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

-

Total Number of Likes: {totalLikes}

+

Total Number of Likes: {totalLikes} ❤️s

diff --git a/src/components/ChatEntry.jsx b/src/components/ChatEntry.jsx index 179b8b005..9750c042d 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -16,7 +16,7 @@ const ChatEntry = ({id, sender, body, timeStamp, liked, onLike}) => {

{body}

- +
);