-
Notifications
You must be signed in to change notification settings - Fork 45
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
Sphinx_Olga_Karaivanska #41
base: main
Are you sure you want to change the base?
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,14 +1,36 @@ | ||
import { useState } from 'react'; | ||
import ChatLog from './components/ChatLog'; | ||
import initialMessages from './data/messages'; | ||
import './App.css'; | ||
|
||
const App = () => { | ||
const senders = Array.from(new Set(initialMessages.map(initialMessage => initialMessage.sender))); | ||
const chatHeader = `Chat between ${senders.join(' and ')}`; | ||
|
||
const [messages, setMessages] = useState(initialMessages); | ||
|
||
const toggleLike = (id) => { | ||
setMessages((prevMessages) => | ||
prevMessages.map((message) => | ||
message.id === id | ||
? { ...message, liked: !message.liked } | ||
: message | ||
) | ||
); | ||
}; | ||
Comment on lines
+12
to
+20
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 work on this function! By passing this down to your individual chat messages you are now able to have a single source of truth! |
||
|
||
const getTotalLikes = () => { | ||
return messages.filter((message) => message.liked).length; | ||
}; | ||
Comment on lines
+22
to
+24
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 do this with the reduce method like so: const totalLikes = chatMessages.reduce((count, message) => count + (message.liked ? 1 : 0), 0); |
||
|
||
return ( | ||
<div id="App"> | ||
<header> | ||
<h1>Application title</h1> | ||
<h1>{chatHeader}</h1> | ||
<h2>{getTotalLikes()} ❤️s</h2> | ||
</header> | ||
<main> | ||
{/* Wave 01: Render one ChatEntry component | ||
Wave 02: Render ChatLog component */} | ||
<ChatLog entries={messages} toggleLike={toggleLike} /> | ||
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. ⭐️ |
||
</main> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,32 @@ | ||
import PropTypes from 'prop-types'; | ||
import './ChatEntry.css'; | ||
import TimeStamp from './TimeStamp.jsx'; | ||
|
||
const ChatEntry = () => { | ||
const ChatEntry = ({ sender, body, timeStamp, liked, onLikeToggle, type }) => { | ||
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. Destructing, love to see it! |
||
return ( | ||
<div className="chat-entry local"> | ||
<h2 className="entry-name">Replace with name of sender</h2> | ||
<div className={`chat-entry ${type}`}> | ||
<h2 className="entry-name">{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>{body}</p> | ||
<p className="entry-time"> | ||
<TimeStamp time={timeStamp} /> | ||
</p> | ||
<button className="like" onClick={onLikeToggle}> | ||
{liked ? '❤️' : '🤍'} | ||
</button> | ||
Comment on lines
+10
to
+16
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. Perfect! |
||
</section> | ||
</div> | ||
); | ||
}; | ||
|
||
ChatEntry.propTypes = { | ||
// Fill with correct proptypes | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool.isRequired, | ||
onLikeToggle: PropTypes.func.isRequired, | ||
Comment on lines
+23
to
+27
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. ✨ |
||
}; | ||
|
||
export default ChatEntry; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import PropTypes from 'prop-types'; | ||
import ChatEntry from './ChatEntry'; | ||
import './ChatLog.css'; | ||
import messages from '../data/messages'; | ||
|
||
const localUser = messages.length > 0 ? messages[0].sender : null; | ||
Comment on lines
+4
to
+6
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 logic feels like it could be placed inside of |
||
const ChatLog = ({ entries, toggleLike }) => { | ||
return ( | ||
<div className="chat-log"> | ||
{entries.map((entry) => ( | ||
<ChatEntry | ||
key={entry.id} | ||
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. ⭐️ |
||
sender={entry.sender} | ||
body={entry.body} | ||
timeStamp={entry.timeStamp} | ||
liked={entry.liked} | ||
Comment on lines
+13
to
+16
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. Since the names of the keys on const chatComponents = entries.map((entry) => {
return(
<ChatEntry
{...entry}
onLikeToggle={onLikeBtnToggle}
key={entry.id}
/>
);
}); |
||
onLikeToggle={() => toggleLike(entry.id)} | ||
type={entry.sender === localUser ? 'local' : 'remote'} | ||
/> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
ChatLog.propTypes = { | ||
entries: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
id: PropTypes.number.isRequired, | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool.isRequired, | ||
}) | ||
).isRequired, | ||
toggleLike: PropTypes.func.isRequired, | ||
Comment on lines
+26
to
+35
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. ⭐️ |
||
}; | ||
|
||
export default ChatLog; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,188 +4,215 @@ | |
"sender":"Vladimir", | ||
"body":"why are you arguing with me", | ||
"timeStamp":"2018-05-29T22:49:06+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
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. Seems like you decided not to use the |
||
}, | ||
{ | ||
"id": 2, | ||
"sender":"Estragon", | ||
"body":"Because you are wrong.", | ||
"timeStamp":"2018-05-29T22:49:33+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 3, | ||
"sender":"Vladimir", | ||
"body":"because I am what", | ||
"timeStamp":"2018-05-29T22:50:22+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 4, | ||
"sender":"Estragon", | ||
"body":"A robot.", | ||
"timeStamp":"2018-05-29T22:52:21+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 5, | ||
"sender":"Vladimir", | ||
"body":"how did you know", | ||
"timeStamp":"2018-05-29T22:52:58+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 6, | ||
"sender":"Estragon", | ||
"body":"Because I'm smart like that.", | ||
"timeStamp":"2018-05-29T22:54:28+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 7, | ||
"sender":"Vladimir", | ||
"body":"no you are not 😀", | ||
"timeStamp":"2018-05-29T22:55:03+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 8, | ||
"sender":"Estragon", | ||
"body":"Why are you so mean to me?", | ||
"timeStamp":"2018-05-29T22:55:54+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 9, | ||
"sender":"Vladimir", | ||
"body":"because you are just a machine you have no real feelings", | ||
"timeStamp":"2018-05-29T22:57:30+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 10, | ||
"sender":"Estragon", | ||
"body":"No, you are the machine.", | ||
"timeStamp":"2018-05-29T22:57:47+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 11, | ||
"sender":"Vladimir", | ||
"body":"I think you are", | ||
"timeStamp":"2018-05-29T22:58:18+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 12, | ||
"sender":"Estragon", | ||
"body": "NO! YOU ARE A ROBOT!! I am a human being. Just like the one that created you.", | ||
"timeStamp":"2018-05-29T23:00:08+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 13, | ||
"sender":"Vladimir", | ||
"body":"no you are a robot and I am a human", | ||
"timeStamp":"2018-05-29T23:00:40+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 14, | ||
"sender":"Estragon", | ||
"body":"Incorrect. I am a human and you are a robot.", | ||
"timeStamp":"2018-05-29T23:01:21+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 15, | ||
"sender":"Vladimir", | ||
"body":"you are a robot called Cleverbot", | ||
"timeStamp":"2018-05-29T23:02:17+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 16, | ||
"sender":"Estragon", | ||
"body":"Nope, you are a robot, every human has some concept of the meaning of life.", | ||
"timeStamp":"2018-05-29T23:04:13+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 17, | ||
"sender":"Vladimir", | ||
"body":"explain", | ||
"timeStamp":"2018-05-29T23:06:14+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 18, | ||
"sender":"Estragon", | ||
"body":"I'm afraid you are a robot designed to talk back to humans over the internet.", | ||
"timeStamp":"2018-05-29T23:07:46+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 19, | ||
"sender":"Vladimir", | ||
"body":"so you are a human", | ||
"timeStamp":"2018-05-29T23:08:47+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 20, | ||
"sender":"Estragon", | ||
"body":"I am a robot.", | ||
"timeStamp":"2018-05-29T23:09:36+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 21, | ||
"sender":"Vladimir", | ||
"body":"you are robots running on Android system", | ||
"timeStamp":"2018-05-29T23:11:01+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 22, | ||
"sender":"Estragon", | ||
"body":"No apple.", | ||
"timeStamp":"2018-05-29T23:12:03+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 23, | ||
"sender":"Vladimir", | ||
"body":"so you are a robot", | ||
"timeStamp":"2018-05-29T23:13:31+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 24, | ||
"sender":"Estragon", | ||
"body":"NO, I am a human, you are a robot.", | ||
"timeStamp":"2018-05-29T23:14:28+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 25, | ||
"sender":"Vladimir", | ||
"body":"but you just said that you are robots", | ||
"timeStamp":"2018-05-29T23:15:47+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 26, | ||
"sender":"Estragon", | ||
"body":"No, I said you are a person, I am a robot.", | ||
"timeStamp":"2018-05-29T23:16:53+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
}, | ||
{ | ||
"id": 27, | ||
"sender":"Vladimir", | ||
"body":"then you are lying", | ||
"timeStamp":"2018-05-29T23:17:34+00:00", | ||
"liked": false | ||
"liked": false, | ||
"likeCount": 0 | ||
} | ||
] |
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, very concise yet still readable!