diff --git a/src/App.jsx b/src/App.jsx index 14a7f684d..af00c6d9f 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,17 +1,36 @@ import './App.css'; +import ChatLog from './components/ChatLog'; +import chatMessages from './data/messages.json'; +import { useState } from 'react'; const App = () => { + const [chatData, setChatData] = useState(chatMessages); + + const toggleLike = (id) => { + const updatedMessages = chatData.map((entry) => { + if (entry.id === id) { + return { ...entry, liked: !entry.liked }; + } + return entry; + }); + setChatData(updatedMessages); + }; + + const getTotalLikes = () => { + return chatData.filter((entry) => entry.liked).length; + }; + return (
-

Application title

+

Chat App

+

{getTotalLikes()} ❤️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..f4b5dbda2 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,20 +1,30 @@ import './ChatEntry.css'; +import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; -const ChatEntry = () => { + +const ChatEntry = ({id,sender, body,timeStamp, liked,onLikeToggle}) => { 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, + liked: PropTypes.bool.isRequired, + timeStamp: PropTypes.string.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..fa878d98c --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,35 @@ +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; + +const ChatLog = ({ entries, onLikeToggle }) => { + return ( +
+ {entries.map((entry) => ( + + ))} +
+ ); +}; + +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, + onLikeToggle: PropTypes.func.isRequired, +}; + +export default ChatLog; \ No newline at end of file diff --git a/src/components/ChatLog.test.jsx b/src/components/ChatLog.test.jsx index dfcfeda99..9e3637b0c 100644 --- a/src/components/ChatLog.test.jsx +++ b/src/components/ChatLog.test.jsx @@ -1,4 +1,4 @@ -import ChatLog from './ChatLog'; +import ChatLog from './ChatLog.jsx'; import { render, screen } from '@testing-library/react'; import '@testing-library/jest-dom';