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

C17 Otters - Ada Barries #104

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
29 changes: 26 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import React from 'react';
import './App.css';
import chatMessages from './data/messages.json';
import ChatLog from './components/ChatLog';
import { useState } from 'react';


const App = () => {
const [entries, setEntries] = useState(chatMessages);
let [likeCount, setLikes] = useState(0);

const changeHeartColor = (id) => {
const newEntries = entries.map((entry) => ({...entry}));

Choose a reason for hiding this comment

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

Nice copy! As a heads up, you can create a copy with Destructuring alone like this:

const newEntries = [...entries];

for (const entry of newEntries) {
if (entry.id === id) {
entry.liked = !entry.liked;
if (entry.liked) {
likeCount += 1;

Choose a reason for hiding this comment

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

Careful not to update the state variable directly! This could have unintended side effects. You should always use the setState() (or in this case setLikes()) function.

Here's a good discussion on this: https://stackoverflow.com/questions/37755997/why-cant-i-directly-modify-a-components-state-really

Alsoooo your app currently only increases likeCount and it doesn't handle situations where someone unlikes a message. I don't think this was explicitly stated as a requirement for the project, but I'm mentioning it since it is still good to pay attention to stuff like this.

}
}
}
setEntries(newEntries);
setLikes(likeCount);
};

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat Log</h1>
<h2>{likeCount} ❤️s</h2>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
entries={entries}
heartCallback={changeHeartColor}
/>
</main>
</div>
);
Expand Down
22 changes: 15 additions & 7 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = (props) => {
const heartToggle = () => {
props.heartCallback(props.id);
};

return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<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>
<TimeStamp className="entry-time" time={props.timeStamp}></TimeStamp>
<button className="like" onClick={heartToggle}>{props.liked ? '❤️' : '🤍' }</button>

Choose a reason for hiding this comment

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

Nice ternary operator 🎉

</section>
</div>
);
};
}

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

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


const ChatLog = (props) => {
const chatComponents = props.entries.map((entry) => {

Choose a reason for hiding this comment

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

Nice ✨

return (
<div key={entry.id}>
<ChatEntry
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
heartCallback={props.heartCallback}
/>
</div>
);
});
return (
<div>
{chatComponents}
</div>
)
}

ChatLog.propTypes = {
entries: PropTypes.array.isRequired,
};

export default ChatLog;

5 changes: 5 additions & 0 deletions src/components/ChatLog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@ import { render, screen } from "@testing-library/react";

const LOG = [
{
id: 0,
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",
},
{
id: 2,
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",
},
{
id: 4,
sender: "Vladimir",
body: "Notabot",
timeStamp: "2019-07-23T22:52:21+00:00",
Expand Down