From 101b2a687fc34d88e045e42b36a1198e2f0b0979 Mon Sep 17 00:00:00 2001 From: Cindy Truong Date: Tue, 31 Jan 2023 21:52:59 -0800 Subject: [PATCH 1/2] Updated App state and event handlers, created ChatLog Component, ChatEntry update likes' --- src/App.js | 36 +++++++++++++++++++++++++++++---- src/components/ChatEntry.css | 4 ++++ src/components/ChatEntry.js | 30 ++++++++++++++++++++++----- src/components/ChatLog.js | 39 ++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 9 deletions(-) create mode 100644 src/components/ChatLog.js diff --git a/src/App.js b/src/App.js index c10859093..a6a1d2df3 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,44 @@ -import React from 'react'; +import React, { useState } from 'react'; import './App.css'; +import ChatLog from './components/ChatLog'; import chatMessages from './data/messages.json'; const App = () => { + const [entries, setEntries] = useState(chatMessages); + + const heartCount = () => { + let count = 0; + for (let entry of entries) { + if (entry.liked) { + count++; + } + } + return count; + }; + + const updateMessageData = (updateMessage) => { + const messages = entries.map((message) => { + if (message.id === updateMessage.id) { + return updateMessage; + } else { + return message; + } + }); + setEntries(messages); + }; + return (
-

Application title

+

Chat between Vladimir and Estragon

+
+

+ {heartCount()} ❤️s +

+
- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} + {}
); diff --git a/src/components/ChatEntry.css b/src/components/ChatEntry.css index 05c3baa44..1d187fd7e 100644 --- a/src/components/ChatEntry.css +++ b/src/components/ChatEntry.css @@ -8,6 +8,10 @@ button { outline: inherit; } +.red { + color: red; +} + .chat-entry { margin: 1rem; } diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index b92f0b7b2..6d98d5e60 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,42 @@ import React from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; const ChatEntry = (props) => { + const onClickUpdatedMessage = () => { + const updatedMessage = { + id: props.id, + sender: props.sender, + body: props.body, + timeStamp: props.timeStamp, + liked: !props.liked, + }; + props.onUpdate(updatedMessage); + }; 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, + onUpdate: PropTypes.func.isRequired, + liked: PropTypes.bool.isRequired, }; export default ChatEntry; diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..a1c508458 --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,39 @@ +import React from 'react'; +import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; + +const ChatLog = (props) => { + const getChats = props.entries.map((entry) => { + return ( + + ); + }); + return ( +
+
{getChats}
+
+ ); +}; + +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + id: PropTypes.number.isRequired, + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string, + liked: PropTypes.bool.isRequired, + }).isRequired + ), + onUpdateLikes: PropTypes.func.isRequired, +}; + +export default ChatLog; From db2c17597e8436be857bf78e35f49865f78e1df6 Mon Sep 17 00:00:00 2001 From: Cindy Truong Date: Wed, 1 Feb 2023 12:55:20 -0800 Subject: [PATCH 2/2] added display styles to chat bubble --- src/components/ChatEntry.js | 6 ++++-- src/components/ChatLog.js | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index 6d98d5e60..f04a998bf 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -14,15 +14,17 @@ const ChatEntry = (props) => { }; props.onUpdate(updatedMessage); }; + const senderOrReciever = + props.messageDisplay === 0 ? 'chat-entry local' : 'chat-entry remote'; return ( -
+

{props.sender}

{props.body}

-
diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js index a1c508458..fcaf434a4 100644 --- a/src/components/ChatLog.js +++ b/src/components/ChatLog.js @@ -3,10 +3,11 @@ import ChatEntry from './ChatEntry'; import PropTypes from 'prop-types'; const ChatLog = (props) => { - const getChats = props.entries.map((entry) => { + const getChats = props.entries.map((entry, index) => { return (