diff --git a/src/App.js b/src/App.js index c10859093..1be62ca7a 100644 --- a/src/App.js +++ b/src/App.js @@ -1,14 +1,46 @@ -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 [chatEntries, setChatEntries] = useState(chatMessages); + + let initalNumLiked = 0; + for (const message of chatEntries) { + if (message.liked) { + initalNumLiked++; + } + } + + const [numLiked, setNumLiked] = useState(initalNumLiked); + + const updateHeartChatEntries = (updatedMessage) => { + const updatedChat = chatEntries.map((message) => { + if (message.id === updatedMessage.id) { + if (updatedMessage.liked) { + setNumLiked(numLiked + 1); + } else { + setNumLiked(numLiked - 1); + } + return updatedMessage; + } else { + return message; + } + }); + setChatEntries(updatedChat); + }; + return (
-

Application title

+

{numLiked} ❤️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..f9173f504 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,50 @@ -import React from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; + +const ChatEntry = ({ + id, + sender, + body, + timeStamp, + liked, + onUpdateChatHeart, +}) => { + const likeOrUnlikeMessage = () => { + const updatedMessage = { + id: id, + sender: sender, + body: body, + timeStamp: timeStamp, + liked: !liked, + }; + onUpdateChatHeart(updatedMessage); + }; + const heartShape = liked ? '❤️' : '🤍'; -const ChatEntry = (props) => { return ( -
-

Replace with name of sender

+
+

{sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{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, + onUpdateChatHeart: PropTypes.func.isRequired, }; export default ChatEntry; diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..3a195e83e --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,37 @@ +import React from 'react'; +import './ChatLog.css'; +import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; + +const chatLog = ({ entries, onUpdateChatHeart }) => { + const chatEntries = entries.map((entry) => { + return ( + + ); + }); + + return
{chatEntries}
; +}; + +export default chatLog; + +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, + }) + ), + onUpdateChatHeart: PropTypes.func.isRequired, +};