diff --git a/src/App.js b/src/App.js index c1085909..18994290 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,38 @@ import React from 'react'; +import { useState } from 'react'; import './App.css'; import chatMessages from './data/messages.json'; +import ChatLog from './components/ChatLog'; + +let countLikes =0; const App = () => { + const [chatEntry, setChatEntry ] = useState(chatMessages); + const updateChatEntry = (toUpdate) => { + const chats = chatEntry.map((chat) => { + if (chat.id === toUpdate.id){ + countLikes = toUpdate.liked ? countLikes +1 : countLikes -1 + return toUpdate; + } + return chat; + }); + setChatEntry(chats); + }; + + + return (
-

Application title

+

Chat between Vladmir and Estragon

+
+

{countLikes} ❤️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 b92f0b7b..e4c9476f 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,47 @@ -import React from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; + +const ChatEntry = ({id,sender,body,timeStamp,liked,onUpdateChat}) => { + const updateLike = () => { + onUpdateChat({ + id, + sender, + body, + timeStamp, + liked : !liked + }) + } + const heartLiked = liked ? '❤️' : '🤍'; + let entryClass = '' + + if(sender === 'Vladimir'){ + entryClass = 'local'; + }else{ + entryClass = 'remote'; + } -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, + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool, + onUpdateChat: PropTypes.func }; export default ChatEntry; diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 00000000..d0475e67 --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,35 @@ +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; +import './ChatLog.css'; + +const ChatLog = ({entries, onUpdateChat}) => { + const messages = entries.map((message) => { + return( +
+ +
+ ); + }); + return(messages); +}; + +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + id: PropTypes.number, + sender: PropTypes.string, + body: PropTypes.string, + timeStamp: PropTypes.string, + liked: PropTypes.bool, + onUpdateChat:PropTypes.func + })).isRequired +} + +export default ChatLog \ No newline at end of file