diff --git a/src/App.jsx b/src/App.jsx index 14a7f684d..e053dbb5c 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,31 @@ import './App.css'; +import ChatLog from './components/ChatLog'; +import messagesData from './data/messages.json'; +import { useState } from 'react'; const App = () => { + const [entries, setEntries] = useState(messagesData); + + const calcTotalLikes = () => { + return entries.filter(entry => entry.liked).length; + }; + + const toggleLike = (id) => { + setEntries(entries.map(entry => + entry.id === id ? { ...entry, liked: !entry.liked } : entry + )); + }; + + const totalLikes = calcTotalLikes(); + return (
-

Application title

+

Chat between Vladimir and Estragon

+

{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..e269bd979 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,20 +1,30 @@ import './ChatEntry.css'; +import TimeStamp from './TimeStamp'; +import PropTypes from 'prop-types'; -const ChatEntry = () => { +const ChatEntry = ({ sender, body, timeStamp, liked, onLike }) => { + const isLocal = sender === 'Estragon'; + return ( -
-

Replace with name of sender

+
+

{sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{body}

+

+
); }; ChatEntry.propTypes = { - // Fill with correct proptypes + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.isRequired, + onLike: PropTypes.func.isRequired, }; export default ChatEntry; diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 000000000..5c6607e0e --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,27 @@ +import './ChatLog.css'; +import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; + +const ChatLog = ({ entries, onLike }) => { + return ( +
+ {entries.map((entry) => ( + onLike(entry.id)} + /> + ))} +
+ ); +}; + +ChatLog.propTypes = { + entries: PropTypes.array.isRequired, + onLike: PropTypes.func.isRequired, +}; + +export default ChatLog; \ No newline at end of file