Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tigers - Melley Gebretatios #123

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
import React from 'react';
import React, { useState } from 'react';
import './App.css';
// import ChatEntry from './components/ChatEntry';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// import ChatEntry from './components/ChatEntry';

import chatMessages from './data/messages.json';
import ChatLog from './components/ChatLog';

const App = () => {
// brain
const [ChatData, setChatData] = useState(chatMessages);
const [likeCount, setLikeCount] = useState(0);
const updateLike = (id, like) => {
// console.log(id, ChatData);
// console.log(like);
// console.log(id.liked);
Comment on lines +12 to +14

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to delete any commented out code or print statements used for debugging before turning in your final submission

Suggested change
// console.log(id, ChatData);
// console.log(like);
// console.log(id.liked);


const newMessage = ChatData.find((entry) => entry.id === id);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can refactor this function so we aren't iterating twice. By separating the message out here, we now lose track of where it is in the data structure. So we have to iterate through a second time to put it back in.

Let's just do everything at once inside the bottom loop.

// console.log('newMessage');
// console.log(newMessage);
// console.log(like);
Comment on lines +17 to +19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// console.log('newMessage');
// console.log(newMessage);
// console.log(like);

newMessage.liked = like;
setChatData([...ChatData]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The newMessage is never put back into chatData, so the hearts in the ChatEntry components never change color, which is why one of the Wave 3 tests doesn't pass.

if (like) {
setLikeCount(likeCount + 1);
console.log('setLikeCount');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log('setLikeCount');

}
if (!like) {
setLikeCount(likeCount - 1);
console.log('setLikeCount2');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log('setLikeCount2');

}
};
// beauty
return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat between Vladmir and Estrogen</h1>
<section>
<h2>{likeCount} ❤️s</h2>
</section>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
// Wave 02: Render ChatLog component */}
<ChatLog entries={ChatData} updateMessage={updateLike}></ChatLog>
</main>
</div>
);
Expand Down
41 changes: 35 additions & 6 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,51 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = (props) => {
const ChatEntry = ({ id, body, sender, timeStamp, liked, updateMessage }) => {
// console.log('ChatEntry');
const onLikeButtonClick = () => {
// console.log('onLikeButtonClick');
// console.log(liked);
Comment on lines +7 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// console.log('ChatEntry');
const onLikeButtonClick = () => {
// console.log('onLikeButtonClick');
// console.log(liked);
const onLikeButtonClick = () => {


const updatedMessage = {
id: id,
body: body,
sender: sender,
timeStamp: timeStamp,
liked: !liked,
};
updateMessage(updatedMessage.id, updatedMessage.liked);
};
Comment on lines +12 to +20

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works just fine, and it's an example we show in Learn.

But consider that updateMessage has been given the responsibility to update the likes, right? Let updateMessage take care of it by reassigning liked. All onLikeButtonClick should do is grab the id that updateMessage needs and pass it through.


const likeColor = liked ? '❤️' : '🤍';
// console.log(likeColor);
// console.log(onLikeButtonClick);
Comment on lines +23 to +24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// console.log(likeColor);
// console.log(onLikeButtonClick);

return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<h2 className="entry-name">{sender}</h2>
<section className="entry-bubble">
<p>Replace with body of ChatEntry</p>
<p className="entry-time">Replace with TimeStamp component</p>
<button className="like">🤍</button>
<p>{body}</p>
<p className="entry-time">
<TimeStamp time={timeStamp}></TimeStamp>
</p>
<button className="like" onClick={onLikeButtonClick}>
{likeColor}
</button>
</section>
</div>
);
};

ChatEntry.propTypes = {
//Fill with correct proptypes
// //Fill with correct proptypes
id: PropTypes.number,
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
liked: PropTypes.bool.isRequired,
updateMessage: PropTypes.func.isRequired,
};

export default ChatEntry;
32 changes: 32 additions & 0 deletions src/components/ChatLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import ChatEntry from './ChatEntry';
import PropTypes from 'prop-types';

const ChatLog = (props) => {
const entries = props.entries;
return (
<div>
{entries.map((message) => (
<ChatEntry
id={message.id}
key={message.id}
body={message.body}
sender={message.sender}
timeStamp={message.timeStamp}
liked={message.liked}
updateMessage={props.updateMessage}
></ChatEntry>
))}
;
</div>
);
};
// return requires an argument
// return <div>{entries}</div>;

ChatLog.propTypes = {
entries: PropTypes.arrayOf(PropTypes.object),
updateMessage: PropTypes.func.isRequired,
};

export default ChatLog;
48 changes: 24 additions & 24 deletions src/components/ChatLog.test.js
Original file line number Diff line number Diff line change
@@ -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(<ChatLog entries={LOG} />);
});

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) => {
Expand All @@ -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(<ChatLog entries={[]} />);
expect(element).not.toBeNull();
});
Expand Down