diff --git a/src/App.jsx b/src/App.jsx index 14a7f684..630782a1 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,36 @@ import './App.css'; +import ChatLog from './components/ChatLog'; +import chatMessages from './data/messages.json'; +import {useState} from 'react'; + const App = () => { + const [chatData, setChatData] = useState(chatMessages); + const toggleLiked = (id) => { + const data = chatData.map(chat => { + if (chat.id === id) { + return {...chat, liked: !chat.liked}; + }else { + return chat; + }; + }); + setChatData(data); + }; + let likeCount = 0; + for (const chat of chatData) { + if (chat.liked){likeCount += 1}; + }; return (
-

Application title

+

Chat between {chatData[0].sender} and {chatData[1].sender}

+

{likeCount} ❤️s

- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} + { + + /* Wave 01: Render one ChatEntry component + Wave 02: Render ChatLog component */}
); diff --git a/src/components/ChatEntry.jsx b/src/components/ChatEntry.jsx index 15c56f96..f1eef3c0 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,13 +1,21 @@ import './ChatEntry.css'; +import TimeStamp from './TimeStamp'; +import PropTypes from 'prop-types'; + +const ChatEntry = (props) => { + const toggleLikeButton = () => { + props.toggleLiked(props.id); + }; + + const heartButton= props.liked ? '❤️' : '🤍'; -const ChatEntry = () => { return (
-

Replace with name of sender

+

{props.sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{props.body}

+

+
); @@ -15,6 +23,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, + toggleLiked: PropTypes.func.isRequired, }; export default ChatEntry; diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 00000000..228ba568 --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,38 @@ +import './ChatLog.css'; +import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; + +const ChatLog = ({entries, toggleLiked}) => { + const entryComponents = entries.map((entry) => { + return ( + ); + }); + return ( + <> +

Chat Log

+ + + ); +}; + +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, + toggleLiked: PropTypes.func.isRequired, +}; +export default ChatLog; \ No newline at end of file