-
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
Erina P. - Kunzite #98
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,19 +1,45 @@ | ||
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 [entries, setEntriesData] = useState(chatMessages); | ||
const updateLikedCount = (id) => { | ||
setEntriesData((prev) => { | ||
return prev.map((entry) => { | ||
if(id === entry.id) { | ||
return { | ||
...entry, | ||
liked: !entry.liked, | ||
}; } else { | ||
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 else should be on a new line: if(id === entry.id) {
return {
...entry,
liked: !entry.liked,
};
} else {
...
} |
||
return entry; | ||
} | ||
}); | ||
}); | ||
} | ||
const totalLikesTally = entries.reduce((total, entry) => { | ||
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 |
||
if (entry.liked === true) { | ||
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. It's ok to compare if (entry.liked) { |
||
total += 1; | ||
} | ||
return total; | ||
}, 0); | ||
|
||
return ( | ||
<div id="App"> | ||
<header> | ||
<h1>Application title</h1> | ||
<h1>🤖 Chatterbot 🤖</h1> | ||
<section className='header section'>{totalLikesTally} ❤️s</section> | ||
</header> | ||
<main> | ||
{/* Wave 01: Render one ChatEntry component | ||
Wave 02: Render ChatLog component */} | ||
<ChatLog | ||
className='chat-log' | ||
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 thing but these props should be indented two spaces to the right |
||
entries={entries} | ||
updateLikedCount={updateLikedCount}/> | ||
</main> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; | ||
export default App; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,36 @@ | ||
import React from 'react'; | ||
import './ChatEntry.css'; | ||
import PropTypes from 'prop-types'; | ||
import TimeStamp from './TimeStamp'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const ChatEntry = (props) => { | ||
return ( | ||
<div className="chat-entry local"> | ||
<h2 className="entry-name">Replace with name of 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> | ||
</section> | ||
</div> | ||
); | ||
}; | ||
const onLikeButtonClick = () => { | ||
props.updateLikedCount(props.id); | ||
} | ||
|
||
const formatMessages = props.sender === 'Vladimir' ? 'chat-entry local' : 'chat-entry remote'; | ||
return ( | ||
<div className={formatMessages}> | ||
<h2 className="entry-name">{props.sender}</h2> | ||
<section className="entry-bubble"> | ||
<p>{props.body}</p> | ||
<p className="entry-time"> | ||
<TimeStamp time={props.timeStamp}/> | ||
</p> | ||
<button className='like'onClick={onLikeButtonClick}> | ||
{props.liked ? '❤️' : '🤍'} | ||
</button> | ||
</section> | ||
</div> | ||
)}; | ||
|
||
ChatEntry.propTypes = { | ||
//Fill with correct proptypes | ||
id: PropTypes.number.isRequired, | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool, | ||
onUpdate: PropTypes.func | ||
}; | ||
|
||
export default ChatEntry; | ||
export default ChatEntry; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
margin: auto; | ||
max-width: 50rem; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react'; | ||
import './ChatLog.css'; | ||
import PropTypes from 'prop-types'; | ||
import ChatEntry from './ChatEntry.js'; | ||
|
||
const ChatLog = (props) => { | ||
const chatComponents = props.entries.map(entry => { | ||
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. Learn showed this pattern of creating an array of React elements and then referencing it in a JSX expression, so it's definitely okay to do here, but I should just add that I have not found it to be common pattern in practice - most people would just return the array in the component's return statement, e.g. return (
<div>
{props.entries.map(entry => (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
liked={entry.liked}
timeStamp={entry.timeStamp}
updateLikedCount={props.updateLikedCount}
/>
)}
</div>
); |
||
return ( | ||
<ChatEntry | ||
key={entry.id} | ||
id={entry.id} | ||
sender={entry.sender} | ||
body={entry.body} | ||
liked={entry.liked} | ||
timeStamp={entry.timeStamp} | ||
updateLikedCount={props.updateLikedCount}/> | ||
); | ||
}); | ||
return ( | ||
<div> | ||
{chatComponents} | ||
</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 | ||
})) | ||
}; | ||
|
||
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.
Since we've already got a line that imports React, we want to avoid adding another line that duplicates it, so better to add to line 1: