diff --git a/src/App.js b/src/App.js index c10859093..b9ee31567 100644 --- a/src/App.js +++ b/src/App.js @@ -1,19 +1,49 @@ -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 [messages, setMessages] = useState(chatMessages); + const [likeCount, setLikeCount] = useState(0); + + const handleLikeUpdate = (id) => { + setMessages(messages => { + return messages.map(message => { + if (id === message.id) { + return { + ...message, + liked: !message.liked, + } + } else { + return message; + } + }); + }); + }; + + const updateLikeCount = ((liked) => { + if (liked) { + setLikeCount(() => likeCount + 1); + } else{ + setLikeCount(() => likeCount - 1); + } + }); + return (
-

Application title

+

Barbara's Chatlog

- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +

{likeCount} ❤️s

+
); }; - -export default App; +export default App; \ No newline at end of file diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index b92f0b7b2..b2fdb7d3f 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,50 @@ -import React from 'react'; +import React, { useState } from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; - +import TimeStamp from './TimeStamp'; const ChatEntry = (props) => { + const [heart, setHeart] = useState('🤍'); + + const onLikeButtonClick = () => { + const likedMessage = { + id: props.id, + sender: props.sender, + body: props.body, + timeStamp: props.timeStamp, + liked: !props.liked, + }; + props.onHandleLikeUpdate(likedMessage.id); + props.onUpdateLikeCount(likedMessage.liked); + + if (heart === '🤍') { + return setHeart('❤️'); + } else { + return setHeart('🤍'); + } + }; 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.isRequired, + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.isRequired, + onHandleLikeUpdate: PropTypes.func, + onUpdateLikeCount: PropTypes.func, }; -export default ChatEntry; +export default ChatEntry; \ No newline at end of file diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..c2d2a9474 --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,39 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import './ChatLog.css'; +import ChatEntry from './ChatEntry'; +const ChatLog = (props) => { + return ( +
+ {props.entries.map((message) => { + return ( + + )})} +
+ ); +} +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + id: PropTypes.number.isRequired, + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.isRequired + }) + ) + .isRequired, + onHandleLikeUpdate: PropTypes.func, + onUpdateLikeCount: PropTypes.func +}; + +export default ChatLog; \ No newline at end of file diff --git a/src/components/ChatLog.test.js b/src/components/ChatLog.test.js index 0bb36c3ea..63294974c 100644 --- a/src/components/ChatLog.test.js +++ b/src/components/ChatLog.test.js @@ -6,35 +6,35 @@ import { render, screen } from '@testing-library/react'; const LOG = [ { id: 1, - sender: 'Vladimir', + sender: 'Barbara', body: 'why are you arguing with me', timeStamp: '2018-05-29T22:49:06+00:00', liked: false, }, { id: 2, - sender: 'Estragon', + sender: 'Claire', body: 'Because you are wrong.', timeStamp: '2018-05-29T22:49:33+00:00', liked: false, }, { id: 3, - sender: 'Vladimir', + sender: 'Barbara', body: 'because I am what', timeStamp: '2018-05-29T22:50:22+00:00', liked: false, }, { id: 4, - sender: 'Estragon', + sender: 'Claire', body: 'A robot.', timeStamp: '2018-05-29T22:52:21+00:00', liked: false, }, { id: 5, - sender: 'Vladimir', + sender: 'Barbara', body: 'Notabot', timeStamp: '2019-07-23T22:52:21+00:00', liked: false, @@ -49,11 +49,11 @@ describe('Wave 02: ChatLog', () => { test('renders without crashing and shows all the names', () => { [ { - name: 'Vladimir', + name: 'Barbara', numChats: 3, }, { - name: 'Estragon', + name: 'Claire', numChats: 2, }, ].forEach((person) => { diff --git a/src/data/messages.json b/src/data/messages.json index 64fdb053c..be7e95136 100644 --- a/src/data/messages.json +++ b/src/data/messages.json @@ -1,191 +1,93 @@ [ { "id": 1, - "sender":"Vladimir", - "body":"why are you arguing with me", + "sender":"Barbara", + "body":"Hey there! How's your day going?", "timeStamp":"2018-05-29T22:49:06+00:00", "liked": false }, { "id": 2, - "sender":"Estragon", - "body":"Because you are wrong.", + "sender":"Claire", + "body":"Hey! Not too bad, just busy with work. How about you?", "timeStamp":"2018-05-29T22:49:33+00:00", "liked": false }, { "id": 3, - "sender":"Vladimir", - "body":"because I am what", + "sender":"Barbara", + "body":" Same here, work is piling up. Need a break though. Any exciting plans for the weekend?", "timeStamp":"2018-05-29T22:50:22+00:00", "liked": false }, { "id": 4, - "sender":"Estragon", - "body":"A robot.", + "sender":"Claire", + "body":"Not really, just hoping to catch up on some sleep and maybe binge-watch a new series. You?", "timeStamp":"2018-05-29T22:52:21+00:00", "liked": false }, { "id": 5, - "sender":"Vladimir", - "body":"how did you know", + "sender":"Barbara", + "body":"Nice! I've got a family gathering on Saturday, and Sunday is reserved for some much-needed relaxation. Maybe a good book and a cup of coffee.", "timeStamp":"2018-05-29T22:52:58+00:00", "liked": false }, { "id": 6, - "sender":"Estragon", - "body":"Because I'm smart like that.", + "sender":"Claire", + "body":"Sounds perfect. Which book are you planning to read?", "timeStamp":"2018-05-29T22:54:28+00:00", "liked": false }, { "id": 7, - "sender":"Vladimir", - "body":"no you are not 😀", + "sender":"Barbara", + "body":"I picked up a mystery novel that a friend recommended.'The Silent Enigma.' Ever heard of it?", "timeStamp":"2018-05-29T22:55:03+00:00", "liked": false }, { "id": 8, - "sender":"Estragon", - "body":"Why are you so mean to me?", + "sender":"Claire", + "body":"No, but the title sounds intriguing. Let me know how it is. I've been looking for a good book myself.", "timeStamp":"2018-05-29T22:55:54+00:00", "liked": false }, { "id": 9, - "sender":"Vladimir", - "body":"because you are just a machine you have no real feelings", + "sender":"Barbara", + "body":"Will do! Anything specific you're in the mood for?", "timeStamp":"2018-05-29T22:57:30+00:00", "liked": false }, { "id": 10, - "sender":"Estragon", - "body":"No, you are the machine.", + "sender":"Claire", + "body":"I'm thinking of diving into some science fiction. Maybe something with a futuristic twist. Any suggestions?", "timeStamp":"2018-05-29T22:57:47+00:00", "liked": false }, { "id": 11, - "sender":"Vladimir", - "body":"I think you are", + "sender":"Barbara", + "body":"'Neon Nexus' just came out, and I've heard it's fantastic. It's got all the futuristic elements and a gripping plot. Give it a shot!", "timeStamp":"2018-05-29T22:58:18+00:00", "liked": false }, { "id": 12, - "sender":"Estragon", - "body": "NO! YOU ARE A ROBOT!! I am a human being. Just like the one that created you.", + "sender":"Claire", + "body": "Thanks for the tip! I'll add it to my list. Anyway, I should get back to work. Let's catch up soon, maybe over the weekend?", "timeStamp":"2018-05-29T23:00:08+00:00", "liked": false }, { "id": 13, - "sender":"Vladimir", - "body":"no you are a robot and I am a human", + "sender":"Barbara", + "body":"Definitely! Have a productive day, and we'll talk soon. Take care!", "timeStamp":"2018-05-29T23:00:40+00:00", "liked": false - }, - { - "id": 14, - "sender":"Estragon", - "body":"Incorrect. I am a human and you are a robot.", - "timeStamp":"2018-05-29T23:01:21+00:00", - "liked": false - }, - { - "id": 15, - "sender":"Vladimir", - "body":"you are a robot called Cleverbot", - "timeStamp":"2018-05-29T23:02:17+00:00", - "liked": false - }, - { - "id": 16, - "sender":"Estragon", - "body":"Nope, you are a robot, every human has some concept of the meaning of life.", - "timeStamp":"2018-05-29T23:04:13+00:00", - "liked": false - }, - { - "id": 17, - "sender":"Vladimir", - "body":"explain", - "timeStamp":"2018-05-29T23:06:14+00:00", - "liked": false - }, - { - "id": 18, - "sender":"Estragon", - "body":"I'm afraid you are a robot designed to talk back to humans over the internet.", - "timeStamp":"2018-05-29T23:07:46+00:00", - "liked": false - }, - { - "id": 19, - "sender":"Vladimir", - "body":"so you are a human", - "timeStamp":"2018-05-29T23:08:47+00:00", - "liked": false - }, - { - "id": 20, - "sender":"Estragon", - "body":"I am a robot.", - "timeStamp":"2018-05-29T23:09:36+00:00", - "liked": false - }, - { - "id": 21, - "sender":"Vladimir", - "body":"you are robots running on Android system", - "timeStamp":"2018-05-29T23:11:01+00:00", - "liked": false - }, - { - "id": 22, - "sender":"Estragon", - "body":"No apple.", - "timeStamp":"2018-05-29T23:12:03+00:00", - "liked": false - }, - { - "id": 23, - "sender":"Vladimir", - "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 } ]