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

san chatlog #102

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

const App = () => {
const [chatMessagesData, setChatData] = useState(chatMessages);

const updateChatData = (updatedMessage) => {
Copy link

Choose a reason for hiding this comment

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

looks good!

const messages = chatMessagesData.map((message) => {
if (message.id === updatedMessage.id) {
return updatedMessage;
} else {
return message;
}

});
setChatData(messages);
};


const countLikes = (messages) => {
Copy link

Choose a reason for hiding this comment

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

This is a great way to use existing data. You could also use the reduce function

    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 likes = 0;
messages.forEach((message) => {
if (message.liked) {
likes++;
}
});
return likes;
};

const likes = countLikes(chatMessagesData);

return (
<div id="App">

<header>
<h1>Application title</h1>
<h1>A tale of two cities</h1>
<section><span id="heartWidget" className="widget"> {likes} 💚s</span></section>
Copy link

Choose a reason for hiding this comment

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

You could just call your function in jsx like {countLikes(chatMessageData} and in Learn you were getting an error message because of the style of heart you are using. You can alter the tests to accept the heart you want to use.

</header>

<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
{<div>
{ <ChatLog
entries={ chatMessagesData }
onUpdateMessage={ updateChatData }
/> }
</div>}
</main>
</div>
);
Expand Down
36 changes: 29 additions & 7 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = (props) => {
const onLikeButtonClick = () => {
const updatedMessage = {
id: props.id,
sender: props.sender,
body: props.body,
timeStamp: props.timeStamp,
liked: !props.liked,
};
props.onUpdate(updatedMessage);
}

const likeButton = props.liked ? '💚' : '🤍';
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 ternary operator here. Your test are currently looking for ❤️. If you don't want to use that update your tests to reflect that.


return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of 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>
<div className="chat-entry local" key={props.id}>
<h2 className="entry-name"> { props.sender } </h2>
<section className="chat-entry.remote entry-bubble" >
<p> { props.body } </p>
<p className="entry-time"> <TimeStamp time={props.timeStamp} />
</p>
<button className="like" onClick={onLikeButtonClick}>
{likeButton}
</button>
</section>
</div>
);
};

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

export default ChatEntry;
46 changes: 46 additions & 0 deletions src/components/ChatLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

import React from 'react';
import './ChatLog.css';
import PropTypes from 'prop-types';
import ChatEntry from './ChatEntry';

const ChatLog = ({ entries, onUpdateMessage }) => {
const getMessageLog = (entries) => {

return entries.map((entry, index) => {
return (
<div key={index}>
<ChatEntry
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onUpdate={onUpdateMessage}
/>
</div>
);
});
};


return getMessageLog(entries);

};

ChatLog.propTypes = {
entries: 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 way to specific about what the objects content should be.

id: PropTypes.number,
sender: PropTypes.string,
body: PropTypes.string,
timeStamp: PropTypes.string,
liked: PropTypes.bool,
})
).isRequired,
onUpdateMessage: PropTypes.func,
};



export default ChatLog;