diff --git a/src/App.jsx b/src/App.jsx index 14a7f684..30d76baa 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,26 @@ import './App.css'; +import { useState } from 'react'; +import messages from './data/messages.json'; +import ChatLog from './components/ChatLog'; + const App = () => { + const [entries, setEntries] = useState(messages); + + const toggleLiked = (id) => { + setEntries((prev) => prev.map((m) => (m.id === id ? { ...m, liked: !m.liked } : m))); + }; + + const likeCount = entries.filter((m) => m.liked).length; + return (

Application title

+
{likeCount} ❤️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 fe05efa0..5b44455e 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,21 +1,34 @@ import './ChatEntry.css'; +import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; -const ChatEntry = () => { +const ChatEntry = ({ id, sender, body, timeStamp, liked, onToggleLiked }) => { return ( - // Replace the outer tag name with a semantic element that fits our use case - -

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, + onToggleLiked: PropTypes.func, }; export default ChatEntry; diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 00000000..1e667ffb --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,41 @@ +import ChatEntry from './ChatEntry'; +import './ChatLog.css'; +import PropTypes from 'prop-types'; + +const ChatLog = ({ entries, onToggleLiked }) => { + const chatEntries = entries.map((message) => { + return ( + + ); + }); + + return ( +
+ {chatEntries} +
+ ); +}; + +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, + onToggleLiked: PropTypes.func.isRequired, +}; + +export default ChatLog; +