diff --git a/project-docs/wave-02.md b/project-docs/wave-02.md index 5a55fe505..ee9ceb791 100644 --- a/project-docs/wave-02.md +++ b/project-docs/wave-02.md @@ -2,7 +2,9 @@ **Learn Topics: React Components and Props required for this wave** -Implement a `ChatLog` component and update the `App` component to display an entire chat log. `ChatLog` should display a sequence of individual `ChatEntry` components. +--Implement a `ChatLog` component and +--update the `App` component to display an entire chat log. +--`ChatLog` should display a sequence of individual `ChatEntry` components. `ChatLog` takes one prop named `entries` (which is an array). diff --git a/project-docs/wave-03.md b/project-docs/wave-03.md index 22e175a61..625512768 100644 --- a/project-docs/wave-03.md +++ b/project-docs/wave-03.md @@ -5,6 +5,7 @@ In this wave we will update the components to manage a **like** feature. - Add behavior to heart button in `ChatEntry` so that when it is clicked it toggles from an empty heart (🤍) to a filled heart (❤️) and from a filled heart (❤️) to an empty heart (🤍). + - Manage the click event and state of the chat entries such that when the like status of a chat message changes by clicking the heart button, it is tracked by the `App` and the `App` reports the number of total messages that have been liked. - Example: If the user has liked three messages, `3 ❤️s` will appear at the top of the screen. diff --git a/src/App.js b/src/App.js index c10859093..16fbd1d9d 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,33 @@ -import React from 'react'; +import React, {useState} from 'react'; import './App.css'; import chatMessages from './data/messages.json'; +import ChatLog from './components/ChatLog'; const App = () => { + const [messageData, setMessageData] = useState(chatMessages) + + const updateMessageData = (updatedMessage) => { + const messages = messageData.map((message) => { + if (message.id === updatedMessage.id) { + return updatedMessage; + } else { + return message; + } + }); + + setMessageData(messages) + } + return ( -
+
-

Application title

+

Chatlog

-
- {/* 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..e59861605 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,88 @@ import React from 'react'; import './ChatEntry.css'; -import PropTypes from 'prop-types'; +// import PropTypes from 'prop-types'; + + +const ChatEntry = ({ + id, + sender, + body, + timeStamp, + liked, + onUpdate +}) => { + + + // console.log({ + // id, + // sender, + // body, + // timeStamp, + // liked, + // onUpdate + // }) + // const firstMessage = props.messages[0] + const onLikeButtonClick = () => { + const updatedMessage = { + id: id, + sender: sender, + body: body, + timeStamp: timeStamp, + liked: !liked, + } + onUpdate(updatedMessage) + + // const button = document.getElementById('button'); + + // if (!props.liked === true){ + // button.textContent = '🌻' + // } else { + // button.textContent = '🤍' + // } + + // const emojiChange = () => { + // if (!props.liked === true){ + // button.textContent = '🌻' + // } else { + // button.textContent = '🤍' + // } + // } + + } + + // not right + // const button = document.getElementById('button'); + // this is not right..... + // const emojiChange = props.liked ? button.textContent = '🌻' : button.textContent = '🤍'; + // experimenting + // const emojiChange = props.liked ? 'green' : 'red'; + + + let icon = '' + + if (liked === true){ + icon = '🌻' + } else { + icon = '🤍' + } -const ChatEntry = (props) => { return (
-

Replace with name of sender

+

{sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{body}

+

{timeStamp}

+
); }; -ChatEntry.propTypes = { - //Fill with correct proptypes -}; +// ChatEntry.propTypes = { +// //Fill with correct proptypes +// sender: PropTypes.string.isRequired, +// body: PropTypes.string.isRequired, +// timeStamp: PropTypes.string.isRequired, +// }; export default ChatEntry; diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..c55031cd6 --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,31 @@ +import React from 'react'; +import './ChatLog.css'; +import './ChatEntry.css'; +// import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; + +const ChatLog = ({messages, onUpdateMessage}) => { + // console.log(messages) + + + const messageComponents = messages.map((message,index) => { + return( +
+ +
+ ) + }); + + return( +
{messageComponents}
+ ); +}; + +export default ChatLog; \ No newline at end of file