diff --git a/src/App.jsx b/src/App.jsx index 14a7f684d..6f3a8330c 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,41 @@ import './App.css'; +import ChatEntry from './components/ChatEntry'; +import ChatLog from './components/ChatLog';'./components/ChatLog'; +import jsonData from './data/messages.json'; +import { useState }from 'react'; const App = () => { + const [messages, setMessages] = useState(jsonData); + + const updateMsgLikes = (id) => { + const update = messages.map(message => { + if (message.id === id) { + return { ...message, liked: !message.liked}; + } + return message; + }); + return setMessages(update) + }; + + const numHearts = () => { + let count = 0; + for (const msg of messages) { + if (msg.liked === true) { + count++; + } + } + return count; + } + + return (

Application title

+

{numHearts()} ❤️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..1ddd5261b 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,20 +1,32 @@ import './ChatEntry.css'; +import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; +import { useState } from 'react'; + +const ChatEntry = (props) => { + // const [heart, setHeart] = useState(props.liked); + let heartShown = props.liked ? '❤️' : '🤍'; -const ChatEntry = () => { return (
-

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, + onHeart: PropTypes.func.isRequired, }; -export default ChatEntry; +export default ChatEntry; \ No newline at end of file diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 000000000..dff10be81 --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,24 @@ +import './ChatLog.css'; +import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; + +const ChatLog = (props) => { + return ( + props.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, + onHeart: PropTypes.func.isRequired, +}; + +export default ChatLog; \ No newline at end of file diff --git a/src/components/TimeStamp.jsx b/src/components/TimeStamp.jsx index e8892b4d4..60afe3bf8 100644 --- a/src/components/TimeStamp.jsx +++ b/src/components/TimeStamp.jsx @@ -6,7 +6,9 @@ const TimeStamp = (props) => { const absolute = time.toFormat('MMMM Do YYYY, h:mm:ss a'); const relative = time.toRelative(); - return {relative}; + return + {relative} + ; }; TimeStamp.propTypes = {