diff --git a/project-docs/wave-03.md b/project-docs/wave-03.md index 22e175a61..c37267179 100644 --- a/project-docs/wave-03.md +++ b/project-docs/wave-03.md @@ -20,6 +20,6 @@ In this wave we will update the components to manage a **like** feature. ## Tests The tests for this component are integration tests. They don't make assumptions about the implementation details of like feature. The tests verify the following functionality: -- When the user click on a 🤍 button it changes to a ❤️, and when the user clicks on a ❤️ it changes to a 🤍. This test also verifies that clicking on one `ChatEntry`'s like button (🤍) doesn't change other `ChatEntry`'s buttons. +- When the user clicks on a 🤍 button it changes to a ❤️, and when the user clicks on a ❤️ it changes to a 🤍. This test also verifies that clicking on one `ChatEntry`'s like button (🤍) doesn't change other `ChatEntry`'s buttons. - The correct number of filled hearts is displayed at the top of the screen. - If you make a design decision to use a different emoji, you will need to change the tests accordingly. diff --git a/src/App.js b/src/App.js index c10859093..8a105790d 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,53 @@ import React from 'react'; import './App.css'; import chatMessages from './data/messages.json'; +// import ChatEntry from './components/ChatEntry'; +import ChatLog from './components/ChatLog'; +import { useState } from 'react'; + const App = () => { + const [chatData, setChatData] = useState(chatMessages); + // { + // 'sender':'Vladimir', + // 'body':'why are you arguing with me', + // 'timeStamp':'2018-05-29T22:49:06+00:00', + // } + + const updateChatData = (updatedChatEntry) => { + const updatedChatEntries = chatData.map((chatEntry) => { + if (chatEntry.id === updatedChatEntry.id) { + return updatedChatEntry; + } else { + return chatEntry; + } + }); + + setChatData(updatedChatEntries); + }; + + const likeCounts = () => { + let likeCount = 0; + for (let entry of chatData) { + if (entry.liked === true) { + likeCount += 1; + } + } + return likeCount; + }; + return (
-

Application title

+

Chat between Vladimir and Estragon

+

Total Number of Likes: {likeCounts()} ❤️s

- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} + {/* */} +
); diff --git a/src/components/ChatEntry.css b/src/components/ChatEntry.css index 05c3baa44..ece5ad733 100644 --- a/src/components/ChatEntry.css +++ b/src/components/ChatEntry.css @@ -97,4 +97,4 @@ button { .chat-entry.remote .entry-bubble:hover::before { background-color: #a9f6f6; -} \ No newline at end of file +} diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index b92f0b7b2..7a9e04787 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,49 @@ import React from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; +// import { useState } from 'react'; const ChatEntry = (props) => { + // const [isLiked, setIsLiked] = useState(false); + + // const toggleLike = () => { + // setIsLiked(!isLiked); + // }; + + const onLikeButtonClick = () => { + const updatedChatEntry = { + id: props.id, + sender: props.sender, + body: props.body, + timeStamp: props.timeStamp, + liked: !props.liked, + }; + props.onUpdate(updatedChatEntry); + }; + + const like = props.liked ? '❤️' : '🤍'; + return (
-

Replace with name of sender

+

{props.sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{props.id}

+

{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, + onUpdate: PropTypes.func.isRequired, }; export default ChatEntry; diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..04978eb36 --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,42 @@ +import React from 'react'; +import './ChatLog.css'; +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; + +const ChatLog = (props) => { + const chatEntryComponents = props.entries.map((entry, index) => { + return ( +
  • + +
  • + ); + }); + + return ( +
    + +
    + ); +}; + +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, + }) + ), + onUpdateChatData: PropTypes.func.isRequired, +}; + +export default ChatLog; \ No newline at end of file