-
Notifications
You must be signed in to change notification settings - Fork 111
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
base: main
Are you sure you want to change the base?
san chatlog #102
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) => { | ||
const messages = chatMessagesData.map((message) => { | ||
if (message.id === updatedMessage.id) { | ||
return updatedMessage; | ||
} else { | ||
return message; | ||
} | ||
|
||
}); | ||
setChatData(messages); | ||
}; | ||
|
||
|
||
const countLikes = (messages) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could just call your function in jsx like |
||
</header> | ||
|
||
<main> | ||
{/* Wave 01: Render one ChatEntry component | ||
Wave 02: Render ChatLog component */} | ||
{<div> | ||
{ <ChatLog | ||
entries={ chatMessagesData } | ||
onUpdateMessage={ updateChatData } | ||
/> } | ||
</div>} | ||
</main> | ||
</div> | ||
); | ||
|
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 ? '💚' : '🤍'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good!