diff --git a/src/App.js b/src/App.js index c10859093..26a1b857a 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,44 @@ import React from 'react'; import './App.css'; import chatMessages from './data/messages.json'; +import ChatLog from './components/ChatLog'; +import { useState } from 'react'; const App = () => { + + const [chatData, setChatData] = useState(chatMessages); + const [likeCount, setLikeCount] = useState(0); + + + const updateChatData = (updatedChat) => { + const entries = chatData.map((entry) => { + if (entry.id === updatedChat.id) { + if (entry.liked !== updatedChat.liked) { + const likeCountChange = updatedChat.liked ? 1 : -1; + setLikeCount((prevCount) => prevCount + likeCountChange); + } + return updatedChat; + } else { + return entry; + } + }); + setChatData(entries); + }; + return (
-

Application title

+

Chat between Vladimir and Estragon

+
+

{likeCount} ❤️s

+
- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +
); diff --git a/src/App.test.js b/src/App.test.js index 878148902..a619498d6 100644 --- a/src/App.test.js +++ b/src/App.test.js @@ -7,6 +7,7 @@ describe('Wave 03: clicking like button and rendering App', () => { // Arrange const { container } = render(); let buttons = container.querySelectorAll('button.like'); + console.log(buttons); // Act fireEvent.click(buttons[0]); diff --git a/src/components/ChatEntry.css b/src/components/ChatEntry.css index 05c3baa44..e894bad62 100644 --- a/src/components/ChatEntry.css +++ b/src/components/ChatEntry.css @@ -1,4 +1,4 @@ -button { + button { background: none; color: inherit; border: none; @@ -8,6 +8,7 @@ button { outline: inherit; } + .chat-entry { margin: 1rem; } diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index b92f0b7b2..8946c9030 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,47 @@ import React from 'react'; import './ChatEntry.css'; +import TimeStamp from './TimeStamp'; import PropTypes from 'prop-types'; -const ChatEntry = (props) => { + +const ChatEntry = ({sender, body, timeStamp, liked, id, updateChatData}) => { + + const location = sender === 'Vladimir' ? 'local' : 'remote'; + const heartColor = liked ? '❤️' : '🤍'; + + const onHeartClick = () => { + const updatedEntry = { + id: id, + sender: sender, + body: body, + timeStamp: timeStamp, + liked: !liked, + }; + updateChatData(updatedEntry); + }; + 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, + liked: PropTypes.bool.isRequired, + updateChatData: PropTypes.func, }; export default ChatEntry; + \ No newline at end of file diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..30fd945ac --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,39 @@ +import React from 'react'; +import './ChatLog.css'; +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; + +const ChatLog = (props) => { + const chatComponents = props.entries.map((entry) => { + return ( + + ); + }); + + return ( +
+ {chatComponents} +
+ ) +}; + +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, + })), + updateChatData: PropTypes.func +}; + +export default ChatLog;