forked from AdaGold/react-chatlog
-
Notifications
You must be signed in to change notification settings - Fork 153
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
snow leopards - Aretta B. #129
Open
arettab
wants to merge
4
commits into
Ada-C18:main
Choose a base branch
from
arettab:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,50 @@ | ||
import React from 'react'; | ||
import './App.css'; | ||
import { useState } from 'react'; | ||
import ChatLog from './components/ChatLog'; | ||
import chatMessages from './data/messages.json'; | ||
|
||
const App = () => { | ||
const [likedData, setLikedData] = useState(chatMessages); | ||
|
||
const switchLikeButton = (id) => { | ||
setLikedData(likedData => likedData.map(entry => { | ||
if (entry.id === id) { | ||
return {...entry, liked: !entry.liked} | ||
} else { | ||
return entry; | ||
} | ||
})) | ||
}; | ||
|
||
const countTotalLikes = (likedData) => { | ||
let likedHeart = '❤️' | ||
let numberOfHearts = 0 | ||
return likedData.reduce((totalLikes, chat) => { | ||
if (chat.liked) { | ||
numberOfHearts += 1 | ||
} | ||
return `${numberOfHearts} ${likedHeart}s`; | ||
}, 1); | ||
}; | ||
|
||
const countLikes = countTotalLikes(likedData); | ||
|
||
|
||
return ( | ||
<div id="App"> | ||
<header> | ||
<h1>Application title</h1> | ||
<h1>Chat App</h1> | ||
<p> {countLikes}</p> | ||
</header> | ||
<main> | ||
{/* Wave 01: Render one ChatEntry component | ||
Wave 02: Render ChatLog component */} | ||
<ChatLog entries={likedData} onLiked={switchLikeButton} | ||
/> | ||
</main> | ||
</div> | ||
); | ||
}; | ||
|
||
|
||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 TimeStamp from './TimeStamp'; | ||
|
||
|
||
const ChatEntry = (props) => { | ||
const heart = props.liked ? '❤️' : '🤍'; | ||
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"><TimeStamp time={props.timeStamp}/></p> | ||
<button className="like" onClick={() => props.onLiked(props.id)}>{heart}</button> | ||
</section> | ||
</div> | ||
); | ||
}; | ||
|
||
ChatEntry.propTypes = { | ||
//Fill with correct proptypes | ||
sender: PropTypes.string.isRequired, | ||
id: PropTypes.number.isRequired, | ||
body: PropTypes.string.isRequired, | ||
liked: PropTypes.bool.isRequired, | ||
onLiked: PropTypes.func.isRequired, | ||
timeStamp: PropTypes.any.isRequired | ||
|
||
}; | ||
|
||
export default ChatEntry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import ChatEntry from './ChatEntry'; | ||
|
||
const ChatLog = (props) => { | ||
const getEntries = | ||
props.entries.map((entry)=> { | ||
return ( | ||
<ChatEntry | ||
sender={entry.sender} | ||
id={entry.id} | ||
body={entry.body} | ||
liked={entry.liked} | ||
timeStamp={entry.timeStamp} | ||
key={entry.id} | ||
onLiked={props.onLiked} | ||
/> | ||
); | ||
}); | ||
return <div>{getEntries}</div>; | ||
}; | ||
|
||
ChatLog.propTypes = { | ||
entries: PropTypes.arrayOf(PropTypes.shape({ | ||
sender: PropTypes.string.isRequired, | ||
id: PropTypes.number.isRequired, | ||
body: PropTypes.string.isRequired, | ||
liked: PropTypes.bool.isRequired, | ||
timeStamp: PropTypes.string.isRequired | ||
})), | ||
onLiked: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default ChatLog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nice use of
.reduce
here! A simpler version that just counts likes would be preferable since it's easier to see what's going on in the function and adhering to the single-purpose function paradigm is a good goal. Then you could avoid formatting the heart emoji string in the reduce and just do it in the JSX itself. You could also avoid declaring variables outside the reduce function...something like:so that function would return a count like how it's named. Then you can assign it to a variable like you already have on line 31 use it in the JSX like