-
Notifications
You must be signed in to change notification settings - Fork 17
Ocelots - Lisa Utsett #3
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
base: main
Are you sure you want to change the base?
Changes from all commits
5b87502
550da5c
489b3f8
a9d932e
0633267
46f4025
e95f702
9c7a352
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,19 +1,58 @@ | ||
| import React from 'react'; | ||
| import './App.css'; | ||
| import chatMessages from './data/messages.json'; | ||
| import ChatLog from './components/ChatLog.js'; | ||
| import { useState } from 'react'; | ||
|
|
||
|
|
||
| const App = () => { | ||
| const sender1 = chatMessages[0].sender; | ||
| const sender2 = chatMessages[1].sender; | ||
| const [chatEntries, setChatEntries] = useState(chatMessages); | ||
| const [likes, setLikes] = useState(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. Since the liked status of a message lives in the // This could be returned from a helper function
// totalLikes is a variable that accumulates a value as we loop over each entry in chatEntries
const likesCount = chatEntries.reduce((totalLikes, currentMessage) => {
// If currentMessage.liked is true add 1 to totalLikes, else add 0
return (totalLikes += currentMessage.liked ? 1 : 0);
}, 0); // The 0 here sets the initial value of totalLikes to 0 |
||
|
|
||
| const incLikes = () =>{ | ||
| setLikes(likes + 1); | ||
| }; | ||
|
|
||
| const decLikes = () =>{ | ||
| setLikes(likes - 1); | ||
| }; | ||
|
|
||
| const updateEntry = updatedEntry => { | ||
| const entries = chatEntries.map(entry => { | ||
| if (entry.id === updatedEntry.id) { | ||
| if (updatedEntry.liked) { | ||
| incLikes(); | ||
| } | ||
| else { | ||
| decLikes(); | ||
| } | ||
| return updatedEntry; | ||
| } else { | ||
| return entry; | ||
| } | ||
| }); | ||
| setChatEntries(entries) | ||
| } | ||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1>Chat between {sender1} and {sender2}</h1> | ||
| <section> | ||
| <p className='widget'>{likes} ❤️s</p> | ||
| </section> | ||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| {<ChatLog | ||
| entries = {chatEntries} | ||
| onUpdateEntry = {updateEntry} | ||
| sender1 = {sender1} // local sender | ||
| />} | ||
| </main> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default App; | ||
| export default App; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,4 +97,5 @@ button { | |
|
|
||
| .chat-entry.remote .entry-bubble:hover::before { | ||
| background-color: #a9f6f6; | ||
| } | ||
| } | ||
|
|
||
| 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 onUpdateButtonClick = () => { | ||
| const updatedEntry = { | ||
| id: props.id, | ||
| sender: props.sender, | ||
| timeStamp: props.timeStamp, | ||
| body: props.body, | ||
| liked: !props.isLiked, | ||
| sender1: props.localSender | ||
| }; | ||
| props.onUpdate(updatedEntry); | ||
| }; | ||
|
Comment on lines
+7
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. I would consider passing the This made me think of a related concept in secure design for APIs. Imagine we had an API for creating and updating messages, and it has an endpoint |
||
|
|
||
| const heart = props.isLiked ? '❤️' : '🤍'; | ||
| const senderClass = props.sender === props.localSender ? 'chat-entry local' : 'chat-entry remote'; | ||
|
|
||
| return ( | ||
| <div className="chat-entry local"> | ||
| <h2 className="entry-name">Replace with name of sender</h2> | ||
| <div className={senderClass} > | ||
|
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. Another option could be to have an interpolated string here that always holds const senderClass = (props.sender === 'Vladimir') ? 'local' : 'remote';
...
<div className={`chat-entry ${senderClass}`}> |
||
| <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={onUpdateButtonClick}>{heart}</button> | ||
| </section> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| ChatEntry.propTypes = { | ||
| //Fill with correct proptypes | ||
| }; | ||
| /* ChatEntry.propTypes = { | ||
| id: PropTypes.number.isRequired, | ||
| sender: PropTypes.string.isRequired, | ||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
| isLiked: PropTypes.bool.isRequired, | ||
| onUpdate: PropTypes.func.isRequired, | ||
| localSender: PropTypes.string.isRequired | ||
| }; */ | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ | |
| margin: auto; | ||
| max-width: 50rem; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import React from 'react'; | ||
| import ChatEntry from './ChatEntry.js'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatLog = (props) => { | ||
| const chatEntries = props.entries.map((entry) => ( | ||
| <ChatEntry | ||
| id = {entry.id} | ||
| sender = {entry.sender} | ||
| timeStamp = {entry.timeStamp} | ||
| body = {entry.body} | ||
| isLiked = {entry.liked} | ||
| onUpdate = {props.onUpdateEntry} | ||
| localSender = {props.sender1} | ||
| /> | ||
|
|
||
| )); | ||
|
|
||
| return ( | ||
| <section className="chat-log"> | ||
| {chatEntries} | ||
| </section> | ||
| ); | ||
| }; | ||
|
|
||
| /* 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. Really nice use of PropTypes. |
||
| id: PropTypes.number.isRequired, | ||
| sender: PropTypes.string.isRequired, | ||
| body: PropTypes.string.isRequired, | ||
| timeStamp: PropTypes.string.isRequired, | ||
| liked: PropTypes.bool.isRequired | ||
| })), | ||
| onUpdate: PropTypes.func.isRequired, | ||
| sender1: PropTypes.func.isRequired | ||
| }; | ||
| */ | ||
| 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.
We could combine the react imports into a single line: