diff --git a/src/App.js b/src/App.js index c10859093..759f71605 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,44 @@ -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 [chatData, updateChatData] = useState(chatMessages); + const updateLikes = (id) => { + const chats = chatData.map((chat) => { + if (chat.id === id) { + const newChat = { ...chat }; + newChat.liked = !newChat.liked; + return newChat; + } + return chat; + }); + updateChatData(chats); + }; + const totalLikes = () => { + let total = 0; + for (let chat of chatData) { + if (chat.liked === true) { + total += 1; + } + } + return total; + }; return (
-
-

Application title

+
+

+ Chat between {chatData[0].sender} and {chatData[1].sender} +

+
+

+ {totalLikes()} ❤️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..80e84669e 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,42 @@ import React from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; + +const ChatEntry = ({ id, sender, body, timeStamp, liked, onUpdateLikes }) => { + const chatTime = ; + const localOrRemote = id % 2 === 0 ? 'remote' : 'local'; + const updateLikes = (id) => { + onUpdateLikes(id); + }; + const heartLiked = liked ? '❤️' : '🤍'; -const ChatEntry = (props) => { return ( -
-

Replace with name of sender

+
+

{sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{body}

+

{chatTime}

+
); }; ChatEntry.propTypes = { - //Fill with correct proptypes + id: PropTypes.number, + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool, + onUpdateLikes: PropTypes.func, }; export default ChatEntry; diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..7e029a2d8 --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,38 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; +import './ChatLog.css'; + +const ChatLog = ({ entries, onUpdateLikes }) => { + const getChatLogJSX = (entries, onUpdateLikes) => { + return entries.map((chat) => { + return ( + + ); + }); + }; + return
    {getChatLogJSX(entries, onUpdateLikes)}
; +}; + +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + id: PropTypes.number, + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool, + }) + ).isRequired, + onUpdateLikes: PropTypes.func, +}; + +export default ChatLog;