From 73a2ee3a6a606ee107a6b431dfb1ff2d0ccc48f3 Mon Sep 17 00:00:00 2001 From: larissa Date: Mon, 6 Feb 2023 17:29:22 -0800 Subject: [PATCH 1/3] Added state to track chatEntries, function to update state, and number of likes tracker --- src/App.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/App.js b/src/App.js index c10859093..202f7ecc7 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,40 @@ import React from 'react'; import './App.css'; import chatMessages from './data/messages.json'; +import ChatLog from './components/ChatLog'; +import { useState } from 'react'; const App = () => { + // create chatEntries state with imported chatMessages data + const [chatEntries, setChatEntries] = useState(chatMessages); + + // Function to update chatEntries + const updateEntries = (updatedEntry) => { + const entries = chatEntries.map((entry) => { + if (entry.id === updatedEntry.id) { + return updatedEntry; + } else { + return entry; + } + }); + setChatEntries(entries); + }; + + // Track total number of likes in conversation + let numLikes = 0; + for (const entry of chatEntries) { + if (entry.liked) { + numLikes++; + } + } + return (
-

Application title

+

{numLikes} ❤️s

- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +
); From 745818b10b00acff81286b9e4882539f6d1a1ff8 Mon Sep 17 00:00:00 2001 From: larissa Date: Mon, 6 Feb 2023 17:30:37 -0800 Subject: [PATCH 2/3] Added onclick liked event handler and passed in chat entry props for presentation --- src/components/ChatEntry.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index b92f0b7b2..47e10a4b4 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,44 @@ import React from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; const ChatEntry = (props) => { + // onclick liked event handler + const onLikedButtonClick = () => { + const updatedEntry = { + id: props.id, + sender: props.sender, + body: props.body, + timeStamp: props.timeStamp, + liked: !props.liked, + }; + props.updateEntries(updatedEntry); + }; + + const heartColor = props.liked ? '❤️' : '🤍'; + 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, + updateEntries: PropTypes.func, }; export default ChatEntry; From 39b2f2af3f9ad9475317e585d09eda920536a6fc Mon Sep 17 00:00:00 2001 From: larissa Date: Mon, 6 Feb 2023 17:31:28 -0800 Subject: [PATCH 3/3] Added map function to iterate entries and pass in props to ChatEntry component --- src/components/ChatLog.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/components/ChatLog.js diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..e9fe71882 --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,32 @@ +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; + +const ChatLog = (props) => { + const messages = props.entries.map((entry) => { + return ( + + ); + }); + return messages; +}; + +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, + }) + ), + updateEntries: PropTypes.func, +}; +export default ChatLog;