diff --git a/src/App.js b/src/App.js index c10859093..083c73ccd 100644 --- a/src/App.js +++ b/src/App.js @@ -1,19 +1,43 @@ -import React from 'react'; +import React, { useState } from 'react'; import './App.css'; import chatMessages from './data/messages.json'; +import ChatLog from './components/ChatLog'; const App = () => { + const [totalLikes, setTotalLikes] = useState(0); + const [messages, setMessages] = useState(chatMessages); + + const handleLikeCount = (id) => { + const copyMessages = [...messages]; + let newLiked; + for (let i = 0; i < copyMessages.length; i++){ + if (copyMessages[i].id === id){ + const oneMessage = {...copyMessages[i]} + oneMessage.liked = ! oneMessage.liked; + copyMessages[i] = oneMessage; + newLiked = oneMessage.liked; + } + } + + + setMessages(copyMessages); + setTotalLikes((prevLikes) => (newLiked ? prevLikes + 1 : prevLikes - 1)); + }; + + + return (
-

Application title

+

Vladimir and Estragon

+

{totalLikes} {totalLikes === 1 ? '❤️' : '❤️s'}

- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +
); }; export default App; + diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index b92f0b7b2..460b0f5c2 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,39 @@ -import React from 'react'; +import React, { useState } from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; + +const ChatEntry = ({ id, sender, body, timeStamp, onLikeChange, liked }) => { + + const handleLike = () => { + + onLikeChange(id); + }; -const ChatEntry = (props) => { 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, + onLikeChange: PropTypes.func.isRequired, + liked: PropTypes.bool.isRequired }; export default ChatEntry; + diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..4a367bd26 --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,38 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; +import './ChatLog.css'; + +const ChatLog = ({ entries, onLikeChange }) => { + 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, + onLikeChange: PropTypes.func.isRequired, +}; + +export default ChatLog; +