diff --git a/src/App.css b/src/App.css index d97beb4e6..f56752a0a 100644 --- a/src/App.css +++ b/src/App.css @@ -28,6 +28,7 @@ #App header section { background-color: #e0ffff; + color: black; } #App .widget { diff --git a/src/App.jsx b/src/App.jsx index 14a7f684d..898aea08a 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,15 +1,60 @@ +import { useState } from 'react'; import './App.css'; +import ChatLog from './components/ChatLog'; +import messages from './data/messages.json'; -const App = () => { + +const calculateTotalLikeCount = (chat) => { + return chat.reduce((total, chat) => { + return total + (chat.liked ? 1 : 0); + }, 0); +}; + +const localSender = messages[0].sender; +let remoteSender = 'Unknown'; + +for (const message of messages) { + if (message.sender !== localSender) { + remoteSender = message.sender; + break; // Stop once other sender is found + } +} + + +const App =() => { + const [chat, setChat] = useState(messages); + + const handleLikeButtom = (id) => { + setChat(chat => { + return chat.map(chat => { + if (chat.id === id) { + return { ...chat, liked: !chat.liked }; + } else { + return chat; + } + }); + }); + }; + const totalLikes = calculateTotalLikeCount(chat); return (
-

Application title

+

Chat Between {localSender} and {remoteSender}

+
+

{`${totalLikes} ❤️s`}

+
- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +
{ + } + +
+
); }; diff --git a/src/components/ChatEntry.css b/src/components/ChatEntry.css index 05c3baa44..d0e8d6539 100644 --- a/src/components/ChatEntry.css +++ b/src/components/ChatEntry.css @@ -97,4 +97,5 @@ button { .chat-entry.remote .entry-bubble:hover::before { background-color: #a9f6f6; -} \ No newline at end of file +} + diff --git a/src/components/ChatEntry.jsx b/src/components/ChatEntry.jsx index 15c56f96b..483b5c26e 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,20 +1,36 @@ import './ChatEntry.css'; +import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; + + +const ChatEntry = (props) => { + const likeButtonCliked = () => { + props.onLiked(props.id); + }; + + const heartColor = props.liked ? '❤️' : '🤍'; + const messageClass = props.sender === props.localSender ? 'local' : 'remote'; -const ChatEntry = () => { return ( -
-

Replace with name of sender

+
+

{props.sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{props.body}

+

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