From b9dbd3e3e62659e280d2384c463e61de99806e80 Mon Sep 17 00:00:00 2001 From: San Date: Thu, 23 Jun 2022 20:37:46 +0900 Subject: [PATCH] wave 3 --- src/App.js | 43 ++++++++++++++++++++++++++++++---- src/components/ChatEntry.js | 36 +++++++++++++++++++++++------ src/components/ChatLog.js | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 11 deletions(-) create mode 100644 src/components/ChatLog.js diff --git a/src/App.js b/src/App.js index c10859093..e427c5fce 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,51 @@ -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 [chatMessagesData, setChatData] = useState(chatMessages); + + const updateChatData = (updatedMessage) => { + const messages = chatMessagesData.map((message) => { + if (message.id === updatedMessage.id) { + return updatedMessage; + } else { + return message; + } + + }); + setChatData(messages); + }; + + + const countLikes = (messages) => { + let likes = 0; + messages.forEach((message) => { + if (message.liked) { + likes++; + } + }); + return likes; + }; + + const likes = countLikes(chatMessagesData); + return (
+
-

Application title

+

A tale of two cities

+
{likes} 💚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..a4b76c377 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 onLikeButtonClick = () => { + const updatedMessage = { + id: props.id, + sender: props.sender, + body: props.body, + timeStamp: props.timeStamp, + liked: !props.liked, + }; + props.onUpdate(updatedMessage); + } + + const likeButton = props.liked ? '💚' : '🤍'; + return ( -
-

Replace with name of sender

-
-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +
+

{ props.sender }

+
+

{ props.body }

+

+

+
); }; ChatEntry.propTypes = { - //Fill with correct proptypes + id: PropTypes.number, + sender: PropTypes.string, + body: PropTypes.string, + timeStamp: PropTypes.string, + liked: PropTypes.bool, + onUpdate: PropTypes.func, }; export default ChatEntry; diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..4276f3dc7 --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,46 @@ + +import React from 'react'; +import './ChatLog.css'; +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; + +const ChatLog = ({ entries, onUpdateMessage }) => { + const getMessageLog = (entries) => { + + return entries.map((entry, index) => { + return ( +
+ +
+ ); + }); + }; + + + return getMessageLog(entries); + +}; + +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + id: PropTypes.number, + sender: PropTypes.string, + body: PropTypes.string, + timeStamp: PropTypes.string, + liked: PropTypes.bool, + }) + ).isRequired, + onUpdateMessage: PropTypes.func, +}; + + + +export default ChatLog; \ No newline at end of file