diff --git a/src/App.js b/src/App.js index c10859093..a6a1d2df3 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,44 @@ -import React from 'react'; +import React, { useState } from 'react'; import './App.css'; +import ChatLog from './components/ChatLog'; import chatMessages from './data/messages.json'; const App = () => { + const [entries, setEntries] = useState(chatMessages); + + const heartCount = () => { + let count = 0; + for (let entry of entries) { + if (entry.liked) { + count++; + } + } + return count; + }; + + const updateMessageData = (updateMessage) => { + const messages = entries.map((message) => { + if (message.id === updateMessage.id) { + return updateMessage; + } else { + return message; + } + }); + setEntries(messages); + }; + return (
-

Application title

+

Chat between Vladimir and Estragon

+
+

+ {heartCount()} ❤️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..1d187fd7e 100644 --- a/src/components/ChatEntry.css +++ b/src/components/ChatEntry.css @@ -8,6 +8,10 @@ button { outline: inherit; } +.red { + color: red; +} + .chat-entry { margin: 1rem; } diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index b92f0b7b2..f04a998bf 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,44 @@ import React from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; const ChatEntry = (props) => { + const onClickUpdatedMessage = () => { + const updatedMessage = { + id: props.id, + sender: props.sender, + body: props.body, + timeStamp: props.timeStamp, + liked: !props.liked, + }; + props.onUpdate(updatedMessage); + }; + const senderOrReciever = + props.messageDisplay === 0 ? 'chat-entry local' : 'chat-entry remote'; 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, + onUpdate: 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..fcaf434a4 --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,40 @@ +import React from 'react'; +import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; + +const ChatLog = (props) => { + const getChats = props.entries.map((entry, index) => { + return ( + + ); + }); + return ( +
+
{getChats}
+
+ ); +}; + +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + id: PropTypes.number.isRequired, + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string, + liked: PropTypes.bool.isRequired, + }).isRequired + ), + onUpdateLikes: PropTypes.func.isRequired, +}; + +export default ChatLog;