diff --git a/src/App.jsx b/src/App.jsx index 14a7f684..ee81c1ac 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,39 @@ import './App.css'; +import ChatLog from './components/ChatLog'; +import entriesData from './data/messages.json'; +import { useState } from 'react'; + +const countTotalLikes = entriesData => { + return entriesData.reduce((acc, entry) => { + if (!entry.liked) { + return acc; + } + return acc + 1; + }, 0); +}; const App = () => { + const [entryData, setEntriesData] = useState(entriesData); + + function handleLikes(id) { + setEntriesData(entryData => { + return entryData.map(entry => { + if (entry.id === id) { + return { ...entry, liked: !entry.liked}; + } + return entry; + }); + }); + }; + return (

Application title

+

{countTotalLikes(entryData)} ❤️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..f71c1870 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,21 +1,30 @@ import './ChatEntry.css'; +import TimeStamp from './TimeStamp'; +import PropTypes from 'prop-types'; + +const ChatEntry = (props) => { + + const chatDisplay = props.sender === 'Vladimir' ? 'local' : 'remote'; -const ChatEntry = () => { return ( - // Replace the outer tag name with a semantic element that fits our use case - -

Replace with name of sender

+
+

{props.sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

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