diff --git a/src/App.jsx b/src/App.jsx index 14a7f684d..a5b5f1f37 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,17 +1,40 @@ +import { useState } from 'react'; import './App.css'; +import messages from './data/messages.json'; +import ChatLog from './components/ChatLog'; const App = () => { + const [chatMessages, setChatMessages] = useState(messages); + + const handleLikeToggle = (id) => { + setChatMessages((prevMessages) => + prevMessages.map((message) => + message.id === id + ? { ...message, liked: !message.liked } + : message + ) + ); + }; + + const totalLikes = chatMessages.filter((message) => message.liked).length; + const person1 = messages[0].sender; + const person2 = messages[1].sender; + return (
-

Application title

+

Chatlog

+

{`Chat between ${person1} and ${person2}`}

+

{totalLikes} ❤️s

- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +
); }; -export default App; +export default App; \ No newline at end of file diff --git a/src/components/ChatEntry.jsx b/src/components/ChatEntry.jsx index 15c56f96b..b3d557149 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,20 +1,31 @@ +import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; import './ChatEntry.css'; -const ChatEntry = () => { +const ChatEntry = ({id, sender, body, timeStamp, liked, onToggleLike}) => { 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.isRequired, + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.isRequired, + onToggleLike: PropTypes.func.isRequired, }; export default ChatEntry; + + diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 000000000..25e97d0e2 --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,40 @@ +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry.jsx'; +import './ChatLog.css'; + +const ChatLog = ({ entries, onToggleLike }) => { + const chatEntryComponents = entries.map((entry) => { + return ( + + ); + }); + + return ( +
+ {chatEntryComponents} +
+ ); +}; + +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + id: PropTypes.number.isRequired, + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.isRequired, + }) + ).isRequired, + onToggleLike: PropTypes.func.isRequired, +}; + +export default ChatLog;