Skip to content
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
12 changes: 10 additions & 2 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#App {
background-color: #87cefa;
background-color: #a3d5f4;
}

#App header {
background-color: #222;
background-color: rgb(143, 142, 142);
color: #fff;
padding-bottom: 0.5rem;
position: fixed;
Expand All @@ -13,6 +13,14 @@
align-items: center;
}

#App h1{
font-size: 60px;
}

/* #App h4{
padding:10px;
} */

#App main {
padding-left: 2em;
padding-right: 2em;
Expand Down
51 changes: 43 additions & 8 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,54 @@
import React from 'react';
import './App.css';
import ChatLog from './components/ChatLog'
import chatMessages from './data/messages.json';
import {useState} from 'react';
// import ChatEntry from './components/ChatEntry'

const App = () => {
return (
<div id="App">

console.log(chatMessages);
Copy link

Choose a reason for hiding this comment

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

remove this console log after debugging


function App () {

const [chatData, setChatData] = useState(chatMessages)
const updateChatData = updatedChat => {
Copy link

Choose a reason for hiding this comment

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

this function looks good. You can remove the console log on line 15

console.log({updatedChat})
const chats = chatData.map(chat => {
if(chat.id === updatedChat.id){

return updatedChat;
} else{
return chat;
}
});
setChatData(chats);
};

const calcTotalHearts = ()=>{
Copy link

Choose a reason for hiding this comment

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

great way to use existing data to calculate total likes. You could also use reduce like

    return messageData.reduce((totalLikes, message) => {
      // If messages.liked is true add 1 to totalLikes, else add 0
      return (totalLikes += message.liked ? 1 : 0);
    }, 0); // The 0 here sets the initial value of totalLikes to 0

let total = 0;
for (let x of chatData){
if(x.liked === true){
total += 1;
} };
return total
}

return(
<div id = "App">
<header>
<h1>Application title</h1>
<h1>Chat Log Between Vladimir and Estragon</h1>
<h3> Likes 💜</h3>
<h3> {calcTotalHearts()} </h3>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
chats={chatData}
onUpdateChat={updateChatData}
/>
</main>
</div>
);
};
);
}


export default App;
6 changes: 3 additions & 3 deletions src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Wave 03: clicking like button and rendering App', () => {

// click the first button
fireEvent.click(firstButton)
expect(firstButton.innerHTML).toEqual('❤️')
expect(firstButton.innerHTML).toEqual('💜')

// check that all other buttons haven't changed
for (let i = 1; i < buttons.length; i++) {
Expand All @@ -40,13 +40,13 @@ describe('Wave 03: clicking like button and rendering App', () => {
fireEvent.click(firstButton)
expect(firstButton.innerHTML).toEqual('🤍')
fireEvent.click(firstButton)
expect(firstButton.innerHTML).toEqual('❤️')
expect(firstButton.innerHTML).toEqual('💜')
fireEvent.click(firstButton)
expect(firstButton.innerHTML).toEqual('🤍')

// click the last button a couple times
fireEvent.click(lastButton)
expect(lastButton.innerHTML).toEqual('❤️')
expect(lastButton.innerHTML).toEqual('💜')
fireEvent.click(lastButton)
expect(lastButton.innerHTML).toEqual('🤍')
})
Expand Down
34 changes: 25 additions & 9 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';


const ChatEntry = (props) => {
const onButtonClick = (id) => {
console.log('onButtonClick')
const updatedChat = {
id: props.id,
sender: props.sender,
body: props.body,
liked: !props.liked,
likeCount: props.likeCount
};
props.onUpdateChat(updatedChat);
};
const displayLikes = props.liked ? '💜' : '🤍';

return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<h5>{props.likeCount}</h5>
<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>

<button onClick= {() => onButtonClick(props.id)} className="like">
{displayLikes}
</button>

</section>

</div>
);
};

ChatEntry.propTypes = {
Copy link

Choose a reason for hiding this comment

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

you don't have any prop types. Your test should be looking for prop types for sender, body and timeStamp

//Fill with correct proptypes
};

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


const ChatLog = (props) => {

const chatDataMap = props.chats.map(item =>(
Copy link

Choose a reason for hiding this comment

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

using item.id as the key probably still gives you an error in Learn. You can get rid of this by using the index parameter for the map function like

const chatDataMap = props.chats.map((item, i) =>
<ChatEntry
       key={i}

<ChatEntry
key={item.id}
id={item.id}
sender={item.sender}
body={item.body}
timeStamp={item.timeStamp}
liked={item.liked}
onUpdateChat={props.onUpdateChat}/> ))

return (
<main>
{chatDataMap}
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
Comment on lines +22 to +23
Copy link

Choose a reason for hiding this comment

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

Suggested change
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}

</main>

); }

ChatLog.propTypes = {
chats:PropTypes.arrayOf(PropTypes.shape({
Copy link

Choose a reason for hiding this comment

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

great use of shape. Make sure to only had isRequired to props that are a must for the component to render. If you want them all to be required then you should update the tests to reflect that.

id: PropTypes.number.isRequired,
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
liked: PropTypes.bool.isRequired,
timeStamp: PropTypes.string.isRequired

})),
onUpdateChat: PropTypes.func.isRequired,

};




export default ChatLog;