diff --git a/src/App.js b/src/App.js index c10859093..49b3eabae 100644 --- a/src/App.js +++ b/src/App.js @@ -1,19 +1,44 @@ import React from 'react'; +import { useState } from 'react'; import './App.css'; import chatMessages from './data/messages.json'; +import messageDataJson from './data/messages.json'; +import ChatLog from './components/ChatLog'; -const App = () => { - return ( -
-
-

Application title

-
-
- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} -
-
- ); +function App() { + const[messageData,setMessageData] = useState(chatMessages); + const getCountLikes = () => { + let likes = 0; + for (const message of messageData){ + if (message.liked){ + likes += 1; + } + }; + return likes + }; + const updateLikes = (updatedData) => { + const messages = messageData.map((message) => { + if (message.id === updatedData.id) { + return updatedData + } + else return message + }); + setMessageData(messages); + } + return ( +
+
+

Application title

+

{getCountLikes()}

+
+
+ {/* Wave 01: Render one ChatEntry component + Wave 02: Render ChatLog component */} + {/* +
+
+ ); }; export default App; diff --git a/src/components/ChatEntry.css b/src/components/ChatEntry.css index 05c3baa44..ece5ad733 100644 --- a/src/components/ChatEntry.css +++ b/src/components/ChatEntry.css @@ -97,4 +97,4 @@ button { .chat-entry.remote .entry-bubble:hover::before { background-color: #a9f6f6; -} \ No newline at end of file +} diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index b92f0b7b2..adf813b5d 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,15 +1,40 @@ import React from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; +import { useState} from 'react'; const ChatEntry = (props) => { + const[isLiked, setLiked] = useState(false); + + //Function to toggle Liked Heart + const toggleLiked = () => { + setLiked(!isLiked); + } + // Tenary Operator for Liked Heart change color + const like = isLiked ? '❤️': ' 🤍 '; + + const likedMessage = () => { + const updatedMessage = { + id: props.id, + sender: props.sender, + body: props.body, + timeStamp: props.timeStamp, + liked: !props.liked, + }; + props.updateLikes(updatedMessage); + setLiked((prevLike) => ! prevLike); + } + return (
-

Replace with name of sender

+

{props.sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{props.body}

+

+

+ {like} +

); @@ -17,6 +42,11 @@ const ChatEntry = (props) => { ChatEntry.propTypes = { //Fill with correct proptypes + id: PropTypes.number.isRequired, + sender: PropTypes.string.isRequired, + body: PropTypes.string.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..803ea04eb --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,27 @@ +import React from 'react'; +import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; + +const ChatLog = (props) => { + const entryComponents = props.messageData.map((entries) => { + // console.log(entries) + return ( + + ); + }); + return entryComponents +}; + +ChatLog.propTypes = { + //Fill with correct proptypes + messageData: PropTypes.arrayOf(PropTypes.object), +}; + +export default ChatLog; \ No newline at end of file