From 7d303783e9c3deae3c611b7a24190242b85868a5 Mon Sep 17 00:00:00 2001 From: Xuan Hien Pham Date: Sun, 29 Jan 2023 18:17:17 -0500 Subject: [PATCH 1/4] completed wave 01 --- src/App.js | 6 ++++++ src/components/ChatEntry.js | 13 ++++++++----- src/components/ChatEntry.test.js | 16 ++++++++-------- src/components/ChatLog.js | 3 +++ 4 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 src/components/ChatLog.js diff --git a/src/App.js b/src/App.js index c10859093..aa314c363 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,6 @@ import React from 'react'; import './App.css'; +import ChatEntry from './components/ChatEntry'; import chatMessages from './data/messages.json'; const App = () => { @@ -11,6 +12,11 @@ const App = () => {
{/* 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..5920ccbd1 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,14 +1,15 @@ import React from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; -const ChatEntry = (props) => { +const ChatEntry = ({ sender, body, timeStamp }) => { return (
-

Replace with name of sender

+

{sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

+

{body}

+

{}

@@ -16,7 +17,9 @@ const ChatEntry = (props) => { }; 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/ChatEntry.test.js b/src/components/ChatEntry.test.js index b69270c03..4a9a2b09f 100644 --- a/src/components/ChatEntry.test.js +++ b/src/components/ChatEntry.test.js @@ -1,9 +1,9 @@ -import React from "react"; -import "@testing-library/jest-dom/extend-expect"; -import ChatEntry from "./ChatEntry"; -import { render, screen, fireEvent, waitFor } from "@testing-library/react"; +import React from 'react'; +import '@testing-library/jest-dom/extend-expect'; +import ChatEntry from './ChatEntry'; +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; -describe("Wave 01: ChatEntry", () => { +describe('Wave 01: ChatEntry', () => { beforeEach(() => { render( { ); }); - test("renders without crashing and shows the sender", () => { + test('renders without crashing and shows the sender', () => { expect(screen.getByText(/Joe Biden/)).toBeInTheDocument(); }); - test("that it will display the body", () => { + test('that it will display the body', () => { expect(screen.getByText(/Get out by 8am/)).toBeInTheDocument(); }); - test("that it will display the time", () => { + test('that it will display the time', () => { expect(screen.getByText(/\d+ years ago/)).toBeInTheDocument(); }); }); diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..70bb0cfff --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,3 @@ +import React from 'react'; + +const ChatLog = () => {}; From d7e64b9b3a0a4c8de76210f0c0f571c56529c88b Mon Sep 17 00:00:00 2001 From: Xuan Hien Pham Date: Sun, 29 Jan 2023 19:36:45 -0500 Subject: [PATCH 2/4] completed wave02 and modify the chatleft and right --- src/App.js | 7 +++-- src/components/ChatEntry.js | 6 +++-- src/components/ChatLog.js | 17 +++++++++++- src/components/ChatLog.test.js | 48 +++++++++++++++++----------------- 4 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/App.js b/src/App.js index aa314c363..d8ebf9283 100644 --- a/src/App.js +++ b/src/App.js @@ -2,6 +2,7 @@ import React from 'react'; import './App.css'; import ChatEntry from './components/ChatEntry'; import chatMessages from './data/messages.json'; +import ChatLog from './components/ChatLog'; const App = () => { return ( @@ -12,11 +13,13 @@ const App = () => {
{/* Wave 01: Render one ChatEntry component Wave 02: Render ChatLog component */} - + > */} + +
); diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index 5920ccbd1..d412e60e5 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -3,9 +3,11 @@ import './ChatEntry.css'; import PropTypes from 'prop-types'; import TimeStamp from './TimeStamp'; -const ChatEntry = ({ sender, body, timeStamp }) => { +const ChatEntry = ({ id, sender, body, timeStamp }) => { return ( -
+

{sender}

{body}

diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js index 70bb0cfff..54d50489c 100644 --- a/src/components/ChatLog.js +++ b/src/components/ChatLog.js @@ -1,3 +1,18 @@ import React from 'react'; +import ChatEntry from './ChatEntry'; -const ChatLog = () => {}; +const ChatLog = (props) => { + const chatEntry = props.entries.map((chat) => { + return ( + + ); + }); + return
{chatEntry}
; +}; + +export default ChatLog; diff --git a/src/components/ChatLog.test.js b/src/components/ChatLog.test.js index 96f89ebc3..5bafee291 100644 --- a/src/components/ChatLog.test.js +++ b/src/components/ChatLog.test.js @@ -1,49 +1,49 @@ -import React from "react"; -import "@testing-library/jest-dom/extend-expect"; -import ChatLog from "./ChatLog"; -import { render, screen } from "@testing-library/react"; +import React from 'react'; +import '@testing-library/jest-dom/extend-expect'; +import ChatLog from './ChatLog'; +import { render, screen } from '@testing-library/react'; const LOG = [ { - sender: "Vladimir", - body: "why are you arguing with me", - timeStamp: "2018-05-29T22:49:06+00:00", + sender: 'Vladimir', + body: 'why are you arguing with me', + timeStamp: '2018-05-29T22:49:06+00:00', }, { - sender: "Estragon", - body: "Because you are wrong.", - timeStamp: "2018-05-29T22:49:33+00:00", + sender: 'Estragon', + body: 'Because you are wrong.', + timeStamp: '2018-05-29T22:49:33+00:00', }, { - sender: "Vladimir", - body: "because I am what", - timeStamp: "2018-05-29T22:50:22+00:00", + sender: 'Vladimir', + body: 'because I am what', + timeStamp: '2018-05-29T22:50:22+00:00', }, { - sender: "Estragon", - body: "A robot.", - timeStamp: "2018-05-29T22:52:21+00:00", + sender: 'Estragon', + body: 'A robot.', + timeStamp: '2018-05-29T22:52:21+00:00', }, { - sender: "Vladimir", - body: "Notabot", - timeStamp: "2019-07-23T22:52:21+00:00", + sender: 'Vladimir', + body: 'Notabot', + timeStamp: '2019-07-23T22:52:21+00:00', }, ]; -describe("Wave 02: ChatLog", () => { +describe('Wave 02: ChatLog', () => { beforeEach(() => { render(); }); - test("renders without crashing and shows all the names", () => { + test('renders without crashing and shows all the names', () => { [ { - name: "Vladimir", + name: 'Vladimir', numChats: 3, }, { - name: "Estragon", + name: 'Estragon', numChats: 2, }, ].forEach((person) => { @@ -56,7 +56,7 @@ describe("Wave 02: ChatLog", () => { }); }); - test("renders an empty list without crashing", () => { + test('renders an empty list without crashing', () => { const element = render(); expect(element).not.toBeNull(); }); From 583e37ad532726db4831b737cfb48fafc1201889 Mon Sep 17 00:00:00 2001 From: Xuan Hien Pham Date: Wed, 1 Feb 2023 21:12:29 -0500 Subject: [PATCH 3/4] added useState in ChatEntry --- src/App.js | 2 +- src/components/ChatEntry.js | 9 ++++++++- src/components/ChatLog.js | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/App.js b/src/App.js index d8ebf9283..849cbb14b 100644 --- a/src/App.js +++ b/src/App.js @@ -8,7 +8,7 @@ const App = () => { return (
-

Application title

+

Chat between Vladimir and Estragon

{/* Wave 01: Render one ChatEntry component diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index d412e60e5..e062cd131 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,9 +1,14 @@ import React from 'react'; +import { useState } from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; import TimeStamp from './TimeStamp'; const ChatEntry = ({ id, sender, body, timeStamp }) => { + const [like, setLike] = useState(false); + const updateLike = () => { + setLike(!like); + }; return (
{

{body}

{}

- +
); diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js index 54d50489c..64c07af4f 100644 --- a/src/components/ChatLog.js +++ b/src/components/ChatLog.js @@ -5,7 +5,7 @@ const ChatLog = (props) => { const chatEntry = props.entries.map((chat) => { return ( Date: Thu, 2 Feb 2023 01:02:10 -0500 Subject: [PATCH 4/4] completed wave 03 --- src/App.js | 35 +++++++++++++++++++++++++++++++++-- src/components/ChatEntry.js | 35 ++++++++++++++++++++++++----------- src/components/ChatLog.js | 17 +++++++++++++++++ 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/src/App.js b/src/App.js index 849cbb14b..d3c2e598d 100644 --- a/src/App.js +++ b/src/App.js @@ -1,14 +1,42 @@ import React from 'react'; +import { useState } from 'react'; import './App.css'; -import ChatEntry from './components/ChatEntry'; + import chatMessages from './data/messages.json'; import ChatLog from './components/ChatLog'; const App = () => { + const [chatMessagesData, setChatMessagesData] = useState(chatMessages); + + const updateChatMessagesData = (updatedChatMessages) => { + const messages = chatMessagesData.map((chatMessage) => { + if (chatMessage.id === updatedChatMessages.id) { + return updatedChatMessages; + } else { + return chatMessage; + } + }); + setChatMessagesData(messages); + }; + let count = 0; + const countLike = () => { + for (let i = 0; i < chatMessagesData.length; ++i) { + if (chatMessagesData[i]['liked'] === true) { + count += 1; + } + } + return count; + }; + return (

Chat between Vladimir and Estragon

+
+

+ {countLike()} ❤️s +

+
{/* Wave 01: Render one ChatEntry component @@ -19,7 +47,10 @@ const App = () => { timeStamp={chatMessages[0].timeStamp} > */} - +
); diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index e062cd131..38680e16e 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,24 +1,34 @@ import React from 'react'; -import { useState } from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; import TimeStamp from './TimeStamp'; -const ChatEntry = ({ id, sender, body, timeStamp }) => { - const [like, setLike] = useState(false); - const updateLike = () => { - setLike(!like); +const ChatEntry = (props) => { + const onHeartButtonClick = () => { + const updatedChatMessage = { + id: props.id, + sender: props.sender, + body: props.body, + timeStamp: props.timeStamp, + liked: !props.liked, + }; + props.onUpdate(updatedChatMessage); }; + const heart = !props.liked ? '🤍' : '❤️'; return (
-

{sender}

+

{props.sender}

-

{body}

-

{}

-
@@ -26,9 +36,12 @@ const ChatEntry = ({ id, sender, body, timeStamp }) => { }; ChatEntry.propTypes = { + id: PropTypes.number.isRequired, sender: PropTypes.string.isRequired, body: PropTypes.string.isRequired, timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.isRequired, + onUpdate: PropTypes.func.isRequired, }; export default ChatEntry; diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js index 64c07af4f..53258b63b 100644 --- a/src/components/ChatLog.js +++ b/src/components/ChatLog.js @@ -1,18 +1,35 @@ import React from 'react'; import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; const ChatLog = (props) => { const chatEntry = props.entries.map((chat) => { return ( ); }); return
{chatEntry}
; }; +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + id: PropTypes.number.isRequired, + sender: PropTypes.string.isRequired, + body: PropTypes.string, + timeStamp: PropTypes.string, + liked: PropTypes.bool.isRequired, + onUpdate: PropTypes.func.isRequired, + }).isRequired + ), +}; + export default ChatLog;