diff --git a/src/App.jsx b/src/App.jsx index 14a7f684..c18f4d40 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,16 +1,48 @@ import './App.css'; +import Data from './data/messages.json'; +import ChatLog from './components/ChatLog'; +import { useState } from 'react'; + + +function App () { + const [messageData, setMessageData] = useState(Data); + + const senders = [...new Set(Data.map(message => message.sender))]; + + const handleLiked = (id) => { + setMessageData(messageData => messageData.map(message => { + if (message.id === id) { + return {...message, liked: !message.liked}; + } else { + return message; + }; + })); + }; + + const calcTotalLiked = (messageData) => { + let total = 0; + for (const message of messageData) { + if (message.liked) { + total += 1; + } + }; + return total; + }; + + const totalLiked = calcTotalLiked(messageData); -const App = () => { return ( -
-
-

Application title

-
-
- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} -
-
+ <> +
+
+

Chat between {senders[0]} and {senders[1]}

+

{totalLiked} ❤️s

+
+
+ +
+
+ ); }; diff --git a/src/components/ChatEntry.jsx b/src/components/ChatEntry.jsx index 15c56f96..ce54f8c0 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,20 +1,43 @@ import './ChatEntry.css'; +import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; + +const ChatEntry = ({sender, body, timeStamp, id, liked, onLiked}) => { + const heart = liked ? '❤️' :'🤍' ; + + const localOrRemote = sender === 'Vladimir' ? 'chat-entry local' : 'chat-entry remote'; + + const onLikeClicked = () => { + onLiked(id); + }; -const ChatEntry = () => { return ( -
-

Replace with name of sender

-
-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- -
-
+ <> +
+

{sender}

+
+

{body}

+

+ +

+ +
+
+ ); }; ChatEntry.propTypes = { - // Fill with correct proptypes + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + id: PropTypes.number.isRequired, + liked: PropTypes.bool.isRequired, + onLiked: PropTypes.func, }; export default ChatEntry; diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 00000000..9ee62ba3 --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,39 @@ +import './ChatLog.css'; +import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; + +const ChatLog = ({entries, onLiked}) => { + const chatEntryComponents = entries.map((chatEntry) => { + return ( + + + ); + }); + + return ( +
+ {chatEntryComponents} +
+ ); +}; + +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + id: PropTypes.number.isRequired, + liked: PropTypes.bool.isRequired, + })).isRequired, + onLiked: PropTypes.func, +}; + +export default ChatLog; \ No newline at end of file