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 */}
+
);
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;
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;