-
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
Sharks - Camilla #107
base: main
Are you sure you want to change the base?
Sharks - Camilla #107
Changes from all commits
d42ffd4
98817e7
776afb5
a555afc
b7a8cec
f774c6d
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,54 @@ | ||
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 [chatMessagesData, setChatMessagesData] = useState(chatMessages); | ||
const users = []; | ||
let likesCount = 0; | ||
let user1 = ''; | ||
let user2 = ''; | ||
|
||
const updateChatData = (updatedMessage) => { | ||
const messages = chatMessagesData.map((message) => { | ||
if (message.id === updatedMessage.id) { | ||
return updatedMessage; | ||
} else { | ||
return message; | ||
} | ||
}); | ||
|
||
setChatMessagesData(messages); | ||
}; | ||
|
||
for (let i = 0; i < chatMessagesData.length; i++) { | ||
let message = chatMessagesData[i]; | ||
if (users.indexOf(message['sender']) === -1) { | ||
users.push(message['sender']) | ||
} | ||
if (message['liked']) { | ||
likesCount++; | ||
} | ||
} | ||
Comment on lines
+25
to
+33
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. Putting this logic into a method like <p className="header">{calculateHeartCount()} 💙s</p> Having said that, your logic as it is right now would need to be reworked so that when the method is called it would return an integer |
||
|
||
if (users.length === 2) { | ||
user1 = users[0].toUpperCase(); | ||
user2 = users[1].toUpperCase(); | ||
} | ||
Comment on lines
+35
to
+38
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 also directly hard code these user names into your <h1>CHAT BETWEEN {{chatMessages[0].sender}} AND {{chatMessages[1].sender}} </h1> |
||
|
||
return ( | ||
<div id="App"> | ||
<header> | ||
<h1>Application title</h1> | ||
</header> | ||
|
||
<header> | ||
<h1>CHAT BETWEEN {user1} AND {user2} </h1> | ||
<p className="header">{likesCount} 💙s</p> | ||
</header> | ||
<main> | ||
{/* Wave 01: Render one ChatEntry component | ||
Wave 02: Render ChatLog component */} | ||
<ChatLog | ||
entries={chatMessagesData} | ||
onUpdateChatData={updateChatData}> | ||
</ChatLog> | ||
</main> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,4 +97,4 @@ button { | |
|
||
.chat-entry.remote .entry-bubble:hover::before { | ||
background-color: #a9f6f6; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,21 +2,51 @@ import React from 'react'; | |
import './ChatEntry.css'; | ||
import PropTypes from 'prop-types'; | ||
|
||
function getDifferenceInYears(timeStamp) { | ||
const today = new Date(); | ||
const year = today.getFullYear(); | ||
|
||
const pastDay = new Date(timeStamp); | ||
const pastYear = pastDay.getFullYear(); | ||
|
||
const difference = year - pastYear; | ||
return difference; | ||
} | ||
Comment on lines
+5
to
+14
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 job implementing your own solution for determining the time elapsed since a message was sent. If you check the components directory, you'll see You can see how we implemented finding the time elapsed |
||
|
||
const ChatEntry = (props) => { | ||
const timeInYears = getDifferenceInYears(props.timeStamp) + ' years ago'; | ||
|
||
const onLikeButtonClick = () => { | ||
const updatedMessage = { | ||
id: props.id, | ||
sender: props.sender, | ||
body: props.body, | ||
timeStamp: props.timeStamp, | ||
liked: !props.liked | ||
}; | ||
|
||
props.onUpdateChatData(updatedMessage); | ||
}; | ||
Comment on lines
+19
to
+29
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. If you refactor onUpdateChatData as I suggested in App.js, then your onLikeButtonClick() method only needs to modify the The benefit of only changing the Reworking a solution so only |
||
|
||
return ( | ||
<div className="chat-entry local"> | ||
<h2 className="entry-name">Replace with name of sender</h2> | ||
<div className={props.sender==='Vladimir'?'chat-entry local':'chat-entry remote'}> | ||
<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">{timeInYears}</p> | ||
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. How would you refactor line 36 to use the custom TimeStamp component? What would you need to pass into the component as a prop? |
||
<button onClick={onLikeButtonClick} className="like">{!props['liked']?'🤍':'💙'}</button> | ||
</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, | ||
onUpdateChatData: PropTypes.func | ||
}; | ||
|
||
export default ChatEntry; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import './ChatLog.css'; | ||
import ChatEntry from './ChatEntry'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const ChatLog = (props) => { | ||
const messageComponents = props.entries.map((message) => { | ||
return ( | ||
<ChatEntry | ||
key={message['timeStamp']} | ||
id={message['id']} | ||
sender={message['sender']} | ||
body={message['body']} | ||
timeStamp={message['timeStamp']} | ||
liked={message['liked']} | ||
onUpdateChatData={props.onUpdateChatData}> | ||
</ChatEntry> | ||
); | ||
}); | ||
|
||
return ( | ||
<div>{messageComponents}</div> | ||
); | ||
}; | ||
|
||
ChatLog.propTypes = { | ||
entries: PropTypes.arrayOf(PropTypes.shape({ | ||
id: PropTypes.number, | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool | ||
})).isRequired, | ||
onUpdateChatData: PropTypes.func | ||
}; | ||
|
||
export default ChatLog; | ||
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. 👍 Nicely done |
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.
We use this method to update how a chat entry renders after someone likes an entry. To constrain the scope of this method so that it is only responsible for re-rendering a chat entry after it's been liked, we might refactor this method so that we're not returning the entire message.
updateChatData could become something like:
If you do this, then you can update ChatEntry.js too so you only need to pass the prop
id
instead of the whole message object