diff --git a/src/App.js b/src/App.js index c10859093..1b00563f5 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,41 @@ import React from 'react'; 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 updateChatData = (id) => { + setChatData((chatData) => + chatData.map((chat) => { + if (chat.id === id) { + return { ...chat, liked: !chat.liked }; + } else { + return chat; + } + }) + ); + }; + + const findTotalHearts = (chatData) => { + let total = 0; + for (const element of chatData) { + if (element.liked === true) { + total += 1; + } + } + return total; + }; + + const allHearts = findTotalHearts(chatData); + return (
-
-

Application title

-
+
{allHearts} ❤️s
- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +
); diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index b92f0b7b2..34d94eeb5 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -3,20 +3,29 @@ import './ChatEntry.css'; import PropTypes from 'prop-types'; const ChatEntry = (props) => { + const heartType = props.liked ? '❤️' : '🤍'; + return (
-

Replace with name of sender

+

{props.sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{props.body}

+

{props.timeStamp}

+
); }; 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, + onUpdateChat: PropTypes.func, }; export default ChatEntry; diff --git a/src/components/ChatLog.css b/src/components/ChatLog.css index 378848d1f..dd8e96386 100644 --- a/src/components/ChatLog.css +++ b/src/components/ChatLog.css @@ -1,4 +1,5 @@ .chat-log { margin: auto; max-width: 50rem; + /* background-color: [orchid]; */ } diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..c5c583f98 --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,39 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; +import './ChatLog.css'; + +const ChatLog = (props) => { + return ( +
+ +
+ ); +}; + +ChatLog.propTypes = { + chats: PropTypes.arrayOf( + PropTypes.shape({ + sender: PropTypes.string.isRequired, + id: PropTypes.number.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool, + }) + ), + onUpdateChat: PropTypes.func.isRequired, +}; + +export default ChatLog;