diff --git a/src/App.jsx b/src/App.jsx index 14a7f684d..a4a4b74cc 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,46 @@ import './App.css'; +import ChatLog from './components/ChatLog'; +import DATA from './data/messages.json'; +import { useState } from 'react'; + +const calculateLikedCount = (entries) => { + let likedCount = 0; + for (const entry of entries) { + if (entry.liked) { + likedCount++; + } + } + return likedCount; +}; const App = () => { + const [entries, setEntries] = useState(DATA); + const likedCount = calculateLikedCount(entries); + + const likeEntry = (id) => { + setEntries((entries) => { + return entries.map((entry) => { + if (entry.id === id) { + return {...entry, liked: !entry.liked}; + } else { + return entry; + } + }); + }); + }; + return (
-

Application title

+

Chat Between Vladimir and Estragon

+
+

{likedCount} ❤️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..c1eeacb1b 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,13 +1,18 @@ import './ChatEntry.css'; +import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; + +const ChatEntry = ({id, sender, body, timeStamp, liked, onLikeEntry}) => { + const emoji = liked ? '❤️' : '🤍'; + const entrySender = sender === 'Vladimir' ? 'local' : 'remote'; -const ChatEntry = () => { return ( -
-

Replace with name of sender

+
+

{sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{body}

+

+
); @@ -15,6 +20,12 @@ const ChatEntry = () => { 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, + onLikeEntry: PropTypes.func.isRequired, }; export default ChatEntry; diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 000000000..8c385f868 --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,37 @@ +import './ChatLog.css'; +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; + +const ChatLog = (props) => { + const chatEntryComponents = props.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, + onLikeEntry: PropTypes.func.isRequired, +}; + +export default ChatLog;