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

Elsje S - Snow Leopards #112

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
7 changes: 5 additions & 2 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
}

#App h1 {
font-size: 1.5em;
font-size: 2em;
text-align: center;
display: inline-block;
}

#App header section {
background-color: #e0ffff;
background-color: rgb(73, 18, 18);
text-align: left;
padding-left: 20px;
font-size: 1em;
}

#App .widget {
Expand Down
61 changes: 56 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,67 @@
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 [chatData, setChatData] = useState(chatMessages);

const toggleLikeButton = (id) => {
setChatData(chatData => chatData.map(chat => {
if (chat.id === id) {
return {...chat, liked: !chat.liked}
} else {
return chat;
}
}))
};

const calcTotalLikes = (chatData) => {
let everyHeart = '❤️'
let numberOfHearts = 0
Copy link

Choose a reason for hiding this comment

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

Nice job with this calculation! Here's another way to write this that doesn't necessitate a variable outside the scope of the reduce:

return chatData.reduce((count, chat) => {
  if (chat.liked) {
    count += 1;
  }
  return count;
}, 0);

Then you also don't have to do the formatting in the template string and you can do it in the JSX instead.

return chatData.reduce((likeTotal, chat) => {
if (chat.liked) {
numberOfHearts += 1
}
return `${numberOfHearts}${everyHeart}`;
}, 1);
};

const displayTotalLikes = calcTotalLikes(chatData);

return (
<div id="App">
<header>
<h1>Application title</h1>
<header className="App-header">
<h1>Ghibli Jabber</h1>
<section>
<h2 className="likes-counter" >Likes: {displayTotalLikes}</h2>
Copy link

Choose a reason for hiding this comment

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

Following up on the earlier comment, I mean you can do this if you follow the reduce pattern I laid out:

<h2 className="likes-counter" >Likes: {displayTotalLikes} ❤️s</h2>

Which will help your tests to pass as well.

<h2 className="movie-selector">Movie:
<select>
<option>Default Studio Ghibli</option>
<option>Howl's Moving Castle</option>
<option>My Neighbor Totoro</option>
<option>Secret World of Arietty</option>
</select>
</h2>
<h2 className="textbox-selector">
Textbox (or heart?) color:
<select>
<option>Red</option>
<option>Orange</option>
<option>Yellow</option>
<option>Green</option>
<option>Blue</option>
</select>
</h2>
</section>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<div>
<ChatLog
chats={chatData}
onLiked={toggleLikeButton}
/>
</div>
</main>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Wave 03: clicking like button and rendering App', () => {
let buttons = container.querySelectorAll('button.like')

// Act
fireEvent.click(buttons[0])
fireEvent.click(buttons[5])
Copy link

Choose a reason for hiding this comment

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

There's no need to modify the tests here and doing so could cause false positives/negatives unexpectedly.

fireEvent.click(buttons[1])
fireEvent.click(buttons[10])

Expand Down
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 PropTypes from 'prop-types';
// import TimeStamp from './TimeStamp.js';

const ChatEntry = (props) => {
// const convertedTimeStamp = TimeStamp(props);
const heart = props.liked ? '❤️' : '🤍';

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>
<p className="entry-time">{props.timeStamp}</p>
Copy link

Choose a reason for hiding this comment

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

The usage you're looking for is (first uncomment the Timestamp component import on line 4. Then this line:

        <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
id: PropTypes.number.isRequired,
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
liked: PropTypes.bool.isRequired,
};


export default ChatEntry;
2 changes: 2 additions & 0 deletions src/components/ChatEntry.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// "avoidEscape": true;

import React from "react";
import "@testing-library/jest-dom/extend-expect";
import ChatEntry from "./ChatEntry";
Expand Down
37 changes: 37 additions & 0 deletions src/components/ChatLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import PropTypes from 'prop-types';
import ChatEntry from './ChatEntry';
import './ChatLog.css';

const ChatLog = (entries) => {
Copy link

Choose a reason for hiding this comment

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

The expected name for the argument passed to a React component is props

const getChatLogJSX = (chats) => {
return chats.map((chat) => {
return (
<ChatEntry
id={chat.id}
sender={chat.sender}
body={chat.body}
timeStamp={chat.timeStamp}
liked={chat.liked}
key={chat.id}
onLiked={entries.onLiked}
/>
);
});
};
return <div className="chat-log">{getChatLogJSX(entries.chats)}</div>
Copy link

Choose a reason for hiding this comment

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

I think I see where the confusion about entries is. Chatlog is supposed to be passed a prop called entries and those entries are the chat objects. So you'd pass props.entities to the getChatLogJSX function. This is another reason why your tests weren't passing for this component.

};

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

export default ChatLog;
2 changes: 2 additions & 0 deletions src/components/ChatLog.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// "avoidEscape": true;
Copy link

Choose a reason for hiding this comment

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

You can just delete this commented out code.


import React from "react";
import "@testing-library/jest-dom/extend-expect";
import ChatLog from "./ChatLog";
Expand Down