diff --git a/src/App.js b/src/App.js index c10859093..af53c27d1 100644 --- a/src/App.js +++ b/src/App.js @@ -1,14 +1,41 @@ import React from 'react'; import './App.css'; import chatMessages from './data/messages.json'; +import ChatLog from './components/ChatLog'; +import { useState } from 'react'; const App = () => { + + const [chatDATA, setchatDATA] = useState(chatMessages); + + const onLikeUpdate = (updatedEntry) => { + const entries = chatDATA.map(entry => { + if (entry.id === updatedEntry.id) { + return updatedEntry; + } else { + return entry; + } + }); + setchatDATA(entries); + }; + + const calTotalLikes = (chatDATA) => { + return chatDATA.reduce( + (likesCount, entry) => likesCount + entry.liked, 0); + }; + + const likesCount = calTotalLikes(chatDATA); + return (
-

Application title

+

Chat between Vladimir and Estragon

+
+

{likesCount} ❤️s

+
+ {/* Wave 01: Render one ChatEntry component Wave 02: Render ChatLog component */}
@@ -16,4 +43,4 @@ const App = () => { ); }; -export default App; +export default App; \ No newline at end of file diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index b92f0b7b2..4c226de70 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,15 +1,31 @@ import React from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp.js'; + +const ChatEntry = ({id, sender, body, timeStamp, liked, onLikeUpdate}) => { + + const onLikeBtnClick = () => { + const updatedEntry = { + id: id, + sender: sender, + body: body, + timeStamp: timeStamp, + liked: !liked + } + onLikeUpdate(updatedEntry); + } + + const heartStatus = liked ? '❤️' : '🤍'; + const remoteStatus = sender === 'Estragon' ? 'remote' : 'local'; -const ChatEntry = (props) => { return ( -
-

Replace with name of sender

+
+

{sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{body}

+

+
); @@ -17,6 +33,12 @@ const ChatEntry = (props) => { 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, + onLikeUpdate: PropTypes.func }; -export default ChatEntry; +export default ChatEntry; \ No newline at end of file diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..05998c757 --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,35 @@ +import React from 'react'; +import ChatEntry from './ChatEntry.js'; +import PropTypes from 'prop-types'; +import './ChatLog.css'; + +const ChatLog = ({entries, onLikeUpdate}) => { + return entries.map((entry) => { + return ( + + ); + }); +}; + +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, + }) + ), + onLikeUpdate: PropTypes.func +}; + +export default ChatLog; \ No newline at end of file