-
Notifications
You must be signed in to change notification settings - Fork 117
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
chatlog, am I talking to myself? #95
base: main
Are you sure you want to change the base?
Changes from all commits
7eccb9d
d838f7b
5a7428b
1388d73
d796a3a
29ab162
0d4a787
20f7492
b61ef22
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,40 @@ | ||
import React from 'react'; | ||
import { useState } from 'react'; | ||
import './App.css'; | ||
import chatMessages from './data/messages.json'; | ||
import ChatLog from './components/ChatLog'; | ||
|
||
const App = () => { | ||
const [messages, setMessages] = useState(chatMessages); | ||
|
||
const toggleLike = (id) => { | ||
setMessages( | ||
messages.map((message) => | ||
message.id === id ? { ...message, liked: !message.liked } : message | ||
) | ||
); | ||
}; | ||
|
||
const countLikes = () => { | ||
let heartCount = 0; | ||
for (let message of messages) { | ||
if (message.liked) { | ||
heartCount += 1; | ||
} | ||
}return heartCount; | ||
}; | ||
|
||
|
||
return ( | ||
<div id="App"> | ||
<header> | ||
<h1>Application title</h1> | ||
<h1>Waiting for Chat GDT</h1> | ||
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. 😸 |
||
<section> | ||
<h2>{countLikes()} ❤️s</h2> | ||
</section> | ||
</header> | ||
<main> | ||
{/* Wave 01: Render one ChatEntry component | ||
Wave 02: Render ChatLog component */} | ||
<ChatLog entries ={messages} toggleLike={toggleLike}/> | ||
</main> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ button { | |
width: fit-content; | ||
} | ||
|
||
|
||
.chat-entry .entry-bubble:hover { | ||
background-color: #fefea2; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,41 @@ | ||
import React from 'react'; | ||
import './ChatEntry.css'; | ||
import PropTypes from 'prop-types'; | ||
import TimeStamp from './TimeStamp'; | ||
|
||
const ChatEntry = (props) => { | ||
const ChatEntry = ({id, sender, body, timeStamp, liked, toggleLike}) => { | ||
const handleLikeClick = () => { | ||
toggleLike(id); | ||
}; | ||
|
||
const localOrRemote = (sender) => { | ||
if (sender === 'Vladimir') { | ||
return 'chat-entry local'; | ||
} else if (sender === 'Estragon') { | ||
return 'chat-entry remote'; | ||
}; | ||
}; | ||
Comment on lines
+11
to
+17
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. Small style note: the indentation got a little messed up here, this is something we might add to a checklist of things to review our code for before opening PRs. |
||
return ( | ||
<div className="chat-entry local"> | ||
<h2 className="entry-name">Replace with name of sender</h2> | ||
<div className= {localOrRemote(sender)}> | ||
Comment on lines
+11
to
+19
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. We could simplify this a little and remove the duplication of the const localOrRemote = (sender) => {
return (sender === 'Vladimir') ? 'local' : 'remote';
};
return (
{`chat-entry ${chatLocal}`}
<div className= {`chat-entry ${localOrRemote(sender)}`}>
... |
||
<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={handleLikeClick}>{liked ? '❤️' : '🤍'}</button> | ||
</section> | ||
</div> | ||
); | ||
}; | ||
|
||
ChatEntry.propTypes = { | ||
//Fill with correct proptypes | ||
id: PropTypes.number.isRequired, | ||
body: PropTypes.string.isRequired, | ||
sender: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool, | ||
toggleLike: PropTypes.func, | ||
}; | ||
|
||
export default ChatEntry; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import './ChatEntry.css' | ||
import ChatEntry from './ChatEntry'; | ||
import PropTypes from 'prop-types'; | ||
|
||
|
||
const Chatlog = ({entries, toggleLike}) => { | ||
const getEntries = () => { | ||
return entries.map((entry, index) => { | ||
return ( | ||
<ChatEntry | ||
key={index} | ||
id={entry.id} | ||
sender={entry.sender} | ||
body={entry.body} | ||
timeStamp={entry.timeStamp} | ||
liked={entry.liked} | ||
toggleLike={toggleLike} | ||
/> | ||
); | ||
}); | ||
} | ||
return <main> | ||
<section className='chat-log'> | ||
{getEntries()} | ||
</section> | ||
</main> | ||
}; | ||
|
||
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. Nice use of PropTypes.shape for more detail on what data is expected for this component =] |
||
{ | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool, | ||
}) | ||
).isRequired, | ||
toggleLike: 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.
Great use of the chat data to calculate the total hearts! Another way we could do this is with the Array.reduce function: