diff --git a/src/App.jsx b/src/App.jsx index 14a7f684d..dd616f1a0 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,38 @@ import './App.css'; +import ChatLog from './components/ChatLog'; +import chatMessages from './data/messages.json'; +import { useState } from 'react'; + +const calculateTotalLikes = (messages) => { + return messages.reduce((total, msg) => { + return total + (msg.liked ? 1 : 0); + }, 0); +}; const App = () => { + const [messages, setMessages] = useState(chatMessages); + + const toggleLike = (id) => { + const updatedMessages = messages.map((msg) => { + if (msg.id === id) { + return { ...msg, liked: !msg.liked }; + } else { + return msg; + } + }); + setMessages(updatedMessages); + }; + + const totalLikes = calculateTotalLikes(messages); + return ( -
+
-

Application title

+

React Chatlog

+

{totalLikes} ❤️s

- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +
); diff --git a/src/components/ChatEntry.jsx b/src/components/ChatEntry.jsx index 15c56f96b..ba474c7ad 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,20 +1,33 @@ import './ChatEntry.css'; +import TimeStamp from './TimeStamp'; +import PropTypes from 'prop-types'; + +const ChatEntry = (props) => { + const heart = props.liked ? '❤️' : '🤍'; -const ChatEntry = () => { return ( -
-

Replace with name of sender

-
-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +
+

{props.sender}

+
+

{props.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, + onLikeToggle: PropTypes.func.isRequired, }; export default ChatEntry; diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 000000000..240663c58 --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,35 @@ +import './ChatLog.css'; +import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; + +const ChatLog = (props) => { + return ( +
+ {props.entries.map((msg) => ( + + ))} +
+ ); +}; + +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.isRequired, + }) + ).isRequired, + onLikeToggle: PropTypes.func.isRequired, +}; + +export default ChatLog;