diff --git a/src/App.js b/src/App.js index c1085909..bff25fe4 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,52 @@ import React from 'react'; +import { useState } from 'react'; import './App.css'; +import ChatLog from './components/ChatLog'; import chatMessages from './data/messages.json'; const App = () => { + const chatCopy =[] + for (const message of chatMessages){ + chatCopy.push(message) + } + const [chatData, setChatData]= useState(chatCopy) + + const updateLikes = (id, updatedLike) => { + console.log('updatelikes is being called'); + const newChatEntries = []; + for (const chat of chatData) { + if (chat.id !== id) { + + newChatEntries.push(chat); + } else { + const newChat = { + ...chat, + liked: updatedLike + }; + + newChatEntries.push(newChat); + + } + + setChatData(newChatEntries); + + } + }; + const countLikes = () => { + return chatData.reduce((accumulator, count) => { + return count.liked ? accumulator + 1 : accumulator; + }, 0); + }; + + return (
-

Application title

+

Instant Messenger

+

{countLikes()} ❤️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 b92f0b7b..c7062137 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,30 @@ import React from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; -const ChatEntry = (props) => { +const ChatEntry = ({id, sender,body, timeStamp,liked, updateLikes}) => { + const myHeart = liked ? '❤️': '🤍' ; 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, + sender:PropTypes.string.isRequired, + body:PropTypes.string.isRequired, + timeStamp:PropTypes.string.isRequired, + updateLikes:PropTypes.func }; export default ChatEntry; diff --git a/src/components/ChatLog.css b/src/components/ChatLog.css index 378848d1..a7043e1d 100644 --- a/src/components/ChatLog.css +++ b/src/components/ChatLog.css @@ -2,3 +2,4 @@ margin: auto; max-width: 50rem; } + diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 00000000..d90ad3de --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,38 @@ +import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; + +const ChatLog = ({entries,updateLikes}) => { + + const chatEntrycomponents = entries.map((chats) =>{ + + return ( +
  • + < ChatEntry id = {chats.id} sender={chats.sender} body={chats.body} timeStamp={chats.timeStamp} liked={chats.liked} updateLikes={updateLikes}/> +
  • + ) +}); + + return ( + + ); + + +}; + +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + id:PropTypes.number, + sender:PropTypes.string.isRequired, + body:PropTypes.string.isRequired, + timeStamp:PropTypes.string.isRequired, + liked:PropTypes.bool.isRequired, + +}) + ), + updateLikes:PropTypes.func.isRequired, +}; + +export default ChatLog; \ No newline at end of file