From 82a44a2b97393151c2f37edc3525e1d4230b2639 Mon Sep 17 00:00:00 2001
From: Jacqueline Hopkins <318jlh@gmail.com>
Date: Fri, 23 Dec 2022 13:56:26 -0800
Subject: [PATCH 01/10] update the ChatEntry and App components
---
src/App.js | 2 ++
src/components/ChatEntry.js | 20 ++++++++++++++++----
2 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/src/App.js b/src/App.js
index c10859093..37e0e5898 100644
--- a/src/App.js
+++ b/src/App.js
@@ -1,5 +1,6 @@
import React from 'react';
import './App.css';
+import ChatEntry from './components/ChatEntry';
import chatMessages from './data/messages.json';
const App = () => {
@@ -9,6 +10,7 @@ const App = () => {
-
+
-
+
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js
index 7873e104a..08b1520c4 100644
--- a/src/components/ChatEntry.js
+++ b/src/components/ChatEntry.js
@@ -5,18 +5,28 @@ import TimeStamp from './TimeStamp';
import { useState } from 'react';
const ChatEntry = (props) => {
+ const onClickHeartButton = () => {
+ const updatedChat = {
+ id: props.id,
+ sender: props.sender,
+ body: props.body,
+ isLiked: !props.isLiked,
+ timeStamp: props.timeStamp,
+ };
+ props.onUpdate(updatedChat);
+ };
// const [likesCount, setLikesCount] = useState(0);
// const increaseLikes = () => {
// setLikesCount(likesCount + 1);
// };
- const [isLiked, setLiked] = useState(false);
+ // const [isLiked, setLiked] = useState(false);
- const heartMessage = () => {
- setLiked(!isLiked);
- };
+ // const heartMessage = () => {
+ // setLiked(!isLiked);
+ // };
- const heartType = isLiked ? '❤️' : '🤍';
+ const heartType = props.isLiked ? '❤️' : '🤍';
// const chatData = [
// {
@@ -33,7 +43,7 @@ const ChatEntry = (props) => {
{props.body}
{props.timeStamp}
-
@@ -42,10 +52,12 @@ const ChatEntry = (props) => {
};
ChatEntry.propTypes = {
+ id: PropTypes.number.isRequired,
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
- likes: PropTypes.number.isRequired,
+ isLiked: PropTypes.bool,
+ onUpdate: PropTypes.func.isRequired,
};
export default ChatEntry;
diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js
index b6794e049..b965ab91f 100644
--- a/src/components/ChatLog.js
+++ b/src/components/ChatLog.js
@@ -9,9 +9,12 @@ const ChatLog = (props) => {
{props.entries.map((chat) => (
))}
@@ -19,4 +22,17 @@ const ChatLog = (props) => {
);
};
+ChatLog.propTypes = {
+ chats: PropTypes.arrayOf(
+ PropTypes.shape({
+ sender: PropTypes.string.isRequired,
+ id: PropTypes.number.isRequired,
+ body: PropTypes.string.isRequired,
+ timeStamp: PropTypes.string.isRequired,
+ isLiked: PropTypes.bool,
+ })
+ ),
+ onUpdateChat: PropTypes.func.isRequired,
+};
+
export default ChatLog;
From 37c9ba570d150aa3ad59852819ee029e835f0904 Mon Sep 17 00:00:00 2001
From: Jacqueline Hopkins <318jlh@gmail.com>
Date: Fri, 23 Dec 2022 20:22:33 -0800
Subject: [PATCH 07/10] refactor getChatData in app component
---
src/App.js | 22 +++++++++++-----------
src/components/ChatEntry.js | 28 ++++++++++++++--------------
src/components/ChatLog.js | 7 ++++---
3 files changed, 29 insertions(+), 28 deletions(-)
diff --git a/src/App.js b/src/App.js
index 22e9374bf..284228e74 100644
--- a/src/App.js
+++ b/src/App.js
@@ -8,17 +8,17 @@ import { useState } from 'react';
const App = () => {
const [chatData, setChatData] = useState(chatMessages);
- const updateChatData = (updatedChat) => {
- const chats = chatData.map((chat) => {
- if (chat.id === updatedChat.id) {
- return updatedChat;
- } else {
- return chat;
- }
- });
- setChatData(chats);
+ const updateChatData = (id) => {
+ setChatData((chatData) =>
+ chatData.map((chat) => {
+ if (chat.id === id) {
+ return { ...chat, liked: !chat.liked };
+ } else {
+ return chat;
+ }
+ })
+ );
};
- // const [isLiked, setLiked] = useState(false);
// const heartMessage = () => {
// setLiked(!isLiked);
@@ -28,7 +28,7 @@ const App = () => {
return (
-
+
{/* Wave 01: Render one ChatEntry component
diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js
index 08b1520c4..9edacff8b 100644
--- a/src/components/ChatEntry.js
+++ b/src/components/ChatEntry.js
@@ -5,16 +5,16 @@ import TimeStamp from './TimeStamp';
import { useState } from 'react';
const ChatEntry = (props) => {
- const onClickHeartButton = () => {
- const updatedChat = {
- id: props.id,
- sender: props.sender,
- body: props.body,
- isLiked: !props.isLiked,
- timeStamp: props.timeStamp,
- };
- props.onUpdate(updatedChat);
- };
+ // const onClickHeartButton = () => {
+ // const updatedChat = {
+ // id: props.id,
+ // sender: props.sender,
+ // body: props.body,
+ // isLiked: !props.isLiked,
+ // timeStamp: props.timeStamp,
+ // };
+ // props.onUpdateChat(updatedChat);
+ // };
// const [likesCount, setLikesCount] = useState(0);
// const increaseLikes = () => {
// setLikesCount(likesCount + 1);
@@ -26,7 +26,7 @@ const ChatEntry = (props) => {
// setLiked(!isLiked);
// };
- const heartType = props.isLiked ? '❤️' : '🤍';
+ const heartType = props.liked ? '❤️' : '🤍';
// const chatData = [
// {
@@ -43,7 +43,7 @@ const ChatEntry = (props) => {
{props.body}
{props.timeStamp}
-
+ props.onUpdateChat(props.id)} className="like">
{heartType}
@@ -56,8 +56,8 @@ ChatEntry.propTypes = {
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
- isLiked: PropTypes.bool,
- onUpdate: PropTypes.func.isRequired,
+ liked: PropTypes.bool,
+ onUpdateChat: PropTypes.func,
};
export default ChatEntry;
diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js
index b965ab91f..c5c583f98 100644
--- a/src/components/ChatLog.js
+++ b/src/components/ChatLog.js
@@ -13,8 +13,9 @@ const ChatLog = (props) => {
sender={chat.sender}
body={chat.body}
timeStamp={chat.timeStamp}
- isLiked={chat.isLiked}
- onUpdate={props.onUpdateChat}
+ liked={chat.liked}
+ onUpdateChat={props.onUpdateChat}
+ key={chat.id}
/>
))}
@@ -29,7 +30,7 @@ ChatLog.propTypes = {
id: PropTypes.number.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
- isLiked: PropTypes.bool,
+ liked: PropTypes.bool,
})
),
onUpdateChat: PropTypes.func.isRequired,
From 4a1420ddb8c50272714471807c54bc6a4793b598 Mon Sep 17 00:00:00 2001
From: Jacqueline Hopkins <318jlh@gmail.com>
Date: Fri, 23 Dec 2022 21:05:19 -0800
Subject: [PATCH 08/10] implemented function to find the total number of
likes/hearts
---
src/App.js | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/src/App.js b/src/App.js
index 284228e74..739311c59 100644
--- a/src/App.js
+++ b/src/App.js
@@ -20,6 +20,22 @@ const App = () => {
);
};
+ const findTotalHearts = (chatData) => {
+ let total = 0;
+ for (const element of chatData) {
+ if (element.liked === true) {
+ total += 1;
+ }
+ }
+ return total;
+ };
+
+ const allHearts = findTotalHearts(chatData);
+
+ // let totalHearts = 0;
+ // chatData.forEach(chat => {
+ // if {chat["liked"] === 'true"'}
+
// const heartMessage = () => {
// setLiked(!isLiked);
// };
@@ -28,7 +44,7 @@ const App = () => {
return (
-
+
Number of Likes:{allHearts}
{/* Wave 01: Render one ChatEntry component
From 850fd044c5f915a000075e6c58cb46e5af22e668 Mon Sep 17 00:00:00 2001
From: Jacqueline Hopkins <318jlh@gmail.com>
Date: Fri, 23 Dec 2022 21:12:16 -0800
Subject: [PATCH 09/10] update app component header in order to pass test
---
src/App.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/App.js b/src/App.js
index 739311c59..1b27e7a71 100644
--- a/src/App.js
+++ b/src/App.js
@@ -44,7 +44,7 @@ const App = () => {
return (
-
Number of Likes:{allHearts}
+
{/* Wave 01: Render one ChatEntry component
From 14cd5dc09d93abd154e838513c561abd226c3aca Mon Sep 17 00:00:00 2001
From: Jacqueline Hopkins <318jlh@gmail.com>
Date: Fri, 23 Dec 2022 21:16:12 -0800
Subject: [PATCH 10/10] clean up code
---
src/App.js | 13 -------------
src/components/ChatEntry.js | 32 --------------------------------
2 files changed, 45 deletions(-)
diff --git a/src/App.js b/src/App.js
index 1b27e7a71..1b00563f5 100644
--- a/src/App.js
+++ b/src/App.js
@@ -1,6 +1,5 @@
import React from 'react';
import './App.css';
-import ChatEntry from './components/ChatEntry';
import ChatLog from './components/ChatLog';
import chatMessages from './data/messages.json';
import { useState } from 'react';
@@ -32,23 +31,11 @@ const App = () => {
const allHearts = findTotalHearts(chatData);
- // let totalHearts = 0;
- // chatData.forEach(chat => {
- // if {chat["liked"] === 'true"'}
-
- // const heartMessage = () => {
- // setLiked(!isLiked);
- // };
-
- // const heartType = isLiked ? '❤️' : '🤍';
-
return (
- {/* Wave 01: Render one ChatEntry component
- Wave 02: Render ChatLog component */}
);
diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js
index 9edacff8b..34d94eeb5 100644
--- a/src/components/ChatEntry.js
+++ b/src/components/ChatEntry.js
@@ -1,42 +1,10 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
-import TimeStamp from './TimeStamp';
-import { useState } from 'react';
const ChatEntry = (props) => {
- // const onClickHeartButton = () => {
- // const updatedChat = {
- // id: props.id,
- // sender: props.sender,
- // body: props.body,
- // isLiked: !props.isLiked,
- // timeStamp: props.timeStamp,
- // };
- // props.onUpdateChat(updatedChat);
- // };
- // const [likesCount, setLikesCount] = useState(0);
- // const increaseLikes = () => {
- // setLikesCount(likesCount + 1);
- // };
-
- // const [isLiked, setLiked] = useState(false);
-
- // const heartMessage = () => {
- // setLiked(!isLiked);
- // };
-
const heartType = props.liked ? '❤️' : '🤍';
- // const chatData = [
- // {
- // id: 1,
- // sender: 'Vladimir',
- // body: 'why are you arguing with me',
- // timeStamp: '2018-05-29T22:49:06+00:00',
- // liked: false,
- // },
- // ];
return (
{props.sender}