diff --git a/src/App.jsx b/src/App.jsx index 14a7f684d..a6013bb6f 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,46 @@ +import { useState } from 'react'; import './App.css'; +import ChatLog from './components/ChatLog'; +import messages from './data/messages.json' + + +// import data from messages.json +const LOG = messages; + const App = () => { + const [entries, setEntries] = useState(LOG); + + // update chat entry => like change + const toggleLike = (id) => { + const updatedEntries = entries.map(entry => { + if (entry.id === id) { + // toggle like + return {... entry, liked: !entry.liked}; + } else { + // no change + return entry; + } + }); + // update entries + setEntries(updatedEntries); + } + + const totalLikes = entries.filter(entry => entry.liked).length; + return (
-

Application title

+

Chat Between {entries[0].sender} and {entries[1].sender}

+

{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..7461cceaf 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,20 +1,44 @@ import './ChatEntry.css'; +import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; + +const ChatEntry = ({id, sender, body, timeStamp,liked, onLikeToggle}) => { + + const handleHeartClick = () => { + onLikeToggle(id); + }; + + let isLocalUser = false + + if (sender === 'Vladimir') { + isLocalUser = true; + } -const ChatEntry = () => { 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, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.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..48c951b1f --- /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 = ({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