diff --git a/src/App.css b/src/App.css index d97beb4e6..cdcdbb761 100644 --- a/src/App.css +++ b/src/App.css @@ -49,6 +49,10 @@ display: inline-block } +li { + list-style-type: none; +} + .red { color: #b22222 } diff --git a/src/App.js b/src/App.js index c10859093..1ea89c248 100644 --- a/src/App.js +++ b/src/App.js @@ -1,19 +1,62 @@ -import React from 'react'; -import './App.css'; +import React, { useEffect, useState } from 'react'; +import ChatLog from './components/ChatLog'; import chatMessages from './data/messages.json'; +import './App.css'; const App = () => { + + const [chatEntryData, setChatEntryData] = useState([ + { + id: 0, + sender: '', + body: '', + timeStamp: '', + liked: false + } + ]); + + useEffect(() => { + setChatEntryData(chatMessages); + }, []); + + const [likesCount, setLikesCount] = useState(0); + + const updateChatEntryData = updatedEntry => { + const updatedEntries = chatEntryData.map(entry => { + if (entry.id === updatedEntry.id) { + if (updatedEntry.liked === true) { + setLikesCount((likesCount) => likesCount + 1); + return updatedEntry; + } else { + setLikesCount((likesCount) => likesCount - 1); + return updatedEntry; + } + } else { + return entry; + }}); + setChatEntryData(updatedEntries); + }; + return (
-

Application title

+

Rockin' React Chat Log!

+

{likesCount} ❤️

- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +
+
+
    + +
+
+
); }; -export default App; +export default App; \ No newline at end of file diff --git a/src/App.test.js b/src/App.test.js index ca75c71dd..5296a141c 100644 --- a/src/App.test.js +++ b/src/App.test.js @@ -14,7 +14,7 @@ describe('Wave 03: clicking like button and rendering App', () => { fireEvent.click(buttons[10]) // Assert - const countScreen = screen.getByText(/3 ❤️s/) + const countScreen = screen.getByText(/3 ❤️/) expect(countScreen).not.toBeNull() }) diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index b92f0b7b2..56f5b6be1 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,46 @@ -import React from 'react'; +import React, { useState } from 'react'; +import TimeStamp from './TimeStamp'; import './ChatEntry.css'; import PropTypes from 'prop-types'; const ChatEntry = (props) => { + + const [isMessageLiked, setIsMessageLiked] = useState(false) + + + const toggleLikeButton = (event) => { + const updatedChatEntry = { + id: props.id, + sender: props.sender, + body: props.body, + timeStamp: props.timeStamp, + liked: !props.liked + } + setIsMessageLiked(!isMessageLiked); + props.onHandleLikes(updatedChatEntry); + }; + 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, + sender: PropTypes.string, + body: PropTypes.string, + timeStamp: PropTypes.string, + liked: PropTypes.bool, + onHandleLikes: PropTypes.func }; -export default ChatEntry; +export default ChatEntry; \ No newline at end of file diff --git a/src/components/ChatLog.css b/src/components/ChatLog.css index 378848d1f..6fbac3452 100644 --- a/src/components/ChatLog.css +++ b/src/components/ChatLog.css @@ -2,3 +2,7 @@ margin: auto; max-width: 50rem; } + + + + diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..bfa26a82f --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,39 @@ +import ChatEntry from '../components/ChatEntry'; +import PropTypes from 'prop-types'; + +const ChatLog = (props) => { + + const fullChatLog = props.entries.map((entry, index) => { + return ( +
  • + +
  • + ) + }); + + return ( +
    + {fullChatLog} +
    + ) +}; + +ChatLog.propTypes = { + entries: PropTypes.arrayOf(PropTypes.shape({ + id: PropTypes.number, + sender: PropTypes.string, + body: PropTypes.string, + timeStamp: PropTypes.string, + liked: PropTypes.bool + })), + onUpdateLikes: PropTypes.func +}; + +export default ChatLog; \ No newline at end of file diff --git a/src/components/TimeStamp.js b/src/components/TimeStamp.js index 51232778b..d059703da 100644 --- a/src/components/TimeStamp.js +++ b/src/components/TimeStamp.js @@ -1,4 +1,5 @@ import { DateTime } from 'luxon'; +import PropTypes from 'prop-types'; const TimeStamp = (props) => { const time = DateTime.fromISO(props.time); @@ -8,4 +9,8 @@ const TimeStamp = (props) => { return {relative}; }; -export default TimeStamp; +TimeStamp.propTypes = { + time: PropTypes.string +} + +export default TimeStamp; \ No newline at end of file diff --git a/src/data/messages.json b/src/data/messages.json index 64fdb053c..554b908ad 100644 --- a/src/data/messages.json +++ b/src/data/messages.json @@ -159,33 +159,33 @@ "body":"so you are a robot", "timeStamp":"2018-05-29T23:13:31+00:00", "liked": false - }, - { +}, +{ "id": 24, "sender":"Estragon", "body":"NO, I am a human, you are a robot.", "timeStamp":"2018-05-29T23:14:28+00:00", "liked": false - }, - { +}, +{ "id": 25, "sender":"Vladimir", "body":"but you just said that you are robots", "timeStamp":"2018-05-29T23:15:47+00:00", "liked": false - }, - { +}, +{ "id": 26, "sender":"Estragon", "body":"No, I said you are a person, I am a robot.", "timeStamp":"2018-05-29T23:16:53+00:00", "liked": false - }, - { +}, +{ "id": 27, "sender":"Vladimir", "body":"then you are lying", "timeStamp":"2018-05-29T23:17:34+00:00", "liked": false - } +} ]