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

snow leopards - Aretta B. #129

Open
wants to merge 4 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
2 changes: 1 addition & 1 deletion project-docs/wave-03.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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.
- Example: If the user has liked three messages, `3 ❤️s` will appear at the top of the screen.

<details>
<summary>Expand to see hints for implementing this feature</summary>
Expand Down
37 changes: 34 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
import React from 'react';
import './App.css';
import { useState } from 'react';
import ChatLog from './components/ChatLog';
import chatMessages from './data/messages.json';

const App = () => {
const [likedData, setLikedData] = useState(chatMessages);

const switchLikeButton = (id) => {
setLikedData(likedData => likedData.map(entry => {
if (entry.id === id) {
return {...entry, liked: !entry.liked}
} else {
return entry;
}
}))
};

const countTotalLikes = (likedData) => {
let likedHeart = '❤️'
let numberOfHearts = 0
return likedData.reduce((totalLikes, chat) => {
Copy link

Choose a reason for hiding this comment

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

Nice use of .reduce here! A simpler version that just counts likes would be preferable since it's easier to see what's going on in the function and adhering to the single-purpose function paradigm is a good goal. Then you could avoid formatting the heart emoji string in the reduce and just do it in the JSX itself. You could also avoid declaring variables outside the reduce function...something like:

return likedData.reduce((totalLikes, chat) => chat.liked ? totalLikes + 1 : totalLikes, 0);

so that function would return a count like how it's named. Then you can assign it to a variable like you already have on line 31 use it in the JSX like

 <p> {`${countLikes} ❤️s`}</p>

if (chat.liked) {
numberOfHearts += 1
}
return `${numberOfHearts} ${likedHeart}s`;
}, 1);
};

const countLikes = countTotalLikes(likedData);


return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat App</h1>
<p> {countLikes}</p>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={likedData} onLiked={switchLikeButton}
/>
</main>
</div>
);
};



export default App;
21 changes: 15 additions & 6 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';


const ChatEntry = (props) => {
const heart = props.liked ? '❤️' : '🤍';
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className={props.sender==='Vladimir'? 'chat-entry local': 'chat-entry remote' } >
<h2 className="entry-name">{props.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>{props.body}</p>
<p className="entry-time"><TimeStamp time={props.timeStamp}/></p>
<button className="like" onClick={() => props.onLiked(props.id)}>{heart}</button>
</section>
</div>
);
};

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

};

export default ChatEntry;
16 changes: 8 additions & 8 deletions src/components/ChatEntry.test.js
Original file line number Diff line number Diff line change
@@ -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(
<ChatEntry
Expand All @@ -14,15 +14,15 @@ describe("Wave 01: ChatEntry", () => {
);
});

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();
});
});
35 changes: 35 additions & 0 deletions src/components/ChatLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';
import PropTypes from 'prop-types';

import ChatEntry from './ChatEntry';

const ChatLog = (props) => {
const getEntries =
props.entries.map((entry)=> {
return (
<ChatEntry
sender={entry.sender}
id={entry.id}
body={entry.body}
liked={entry.liked}
timeStamp={entry.timeStamp}
key={entry.id}
onLiked={props.onLiked}
/>
);
});
return <div>{getEntries}</div>;
};

ChatLog.propTypes = {
entries: PropTypes.arrayOf(PropTypes.shape({
sender: PropTypes.string.isRequired,
id: PropTypes.number.isRequired,
body: PropTypes.string.isRequired,
liked: PropTypes.bool.isRequired,
timeStamp: PropTypes.string.isRequired
})),
onLiked: PropTypes.func.isRequired,
};

export default ChatLog;
53 changes: 29 additions & 24 deletions src/components/ChatLog.test.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,54 @@
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',
id: 1
},
{
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',
id: 2
},
{
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',
id: 3
},
{
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',
id: 4
},
{
sender: "Vladimir",
body: "Notabot",
timeStamp: "2019-07-23T22:52:21+00:00",
sender: 'Vladimir',
body: 'Notabot',
timeStamp: '2019-07-23T22:52:21+00:00',
id: 5
},
];

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 +61,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
Loading