diff --git a/src/App.jsx b/src/App.jsx index 14a7f684..5f195920 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,31 @@ import './App.css'; +import ChatLog from './components/ChatLog'; +import messages from './data/messages.json'; +import { useState} from 'react'; const App = () => { + const [entries, setEntries] = useState(messages); + + const handleLike = (id) => { + setEntries((prevEntries) => + prevEntries.map((entry) => + entry.id === id ? { ...entry, liked: !entry.liked } : entry + ) + ); + }; + + const totalLikes = entries.reduce((acc, entry) => acc + Number(entry.liked), 0); + return (

Application title

+
+

{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 fe05efa0..37e93724 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,21 +1,33 @@ +import PropTypes from 'prop-types'; import './ChatEntry.css'; +import TimeStamp from './TimeStamp'; -const ChatEntry = () => { + +const ChatEntry = ({ sender, body, timeStamp, liked, onHandleLike }) => { + const heart = liked ? '❤️' : '🤍'; 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 + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.isRequired, + onHandleLike: PropTypes.func, }; export default ChatEntry; diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 00000000..51f6a7a6 --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,48 @@ +import PropTypes from 'prop-types'; +import './ChatLog.css'; +import ChatEntry from './ChatEntry'; + + +const ChatLog = ({ entries, onHandleLike }) => { + const messageList = entries.map((message) => { + const handleLikeClick = + typeof onHandleLike === 'function' + ? () => onHandleLike(message.id) + : undefined; + + return ( +
  • + +
  • + ); + }); + + return ( + + ); +}; + + +ChatLog.propTypes = { + entries: PropTypes.arrayOf(PropTypes.shape({ + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.isRequired, + })).isRequired, + onHandleLike: PropTypes.func, +}; + +export default ChatLog; + + + +