-
Notifications
You must be signed in to change notification settings - Fork 45
C22 Sphinx - Aleida Vieyra #37
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
7a2fce6
3071b79
9ebc32b
8ee2492
ff14fe6
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,17 +1,61 @@ | ||
import './App.css'; | ||
import ChatLog from '/src/components/ChatLog'; | ||
import DATA from '/src/data/messages.json'; | ||
import { useState } from 'react'; | ||
|
||
|
||
function App() { | ||
const [entries, setEntries] = useState(DATA); | ||
const [colors, setTextColor] = useState(DATA); | ||
|
||
const toggleLikeBtnClick= (id) => { | ||
// Refactoring to incorporate func passing state update | ||
setEntries(entries => entries.map(entry=>{ | ||
if (entry.id == id){ | ||
return {...entry, liked: !entry.liked}; | ||
} | ||
else{ | ||
return entry; | ||
} | ||
})); | ||
}; | ||
Comment on lines
+11
to
+21
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 totalLikesCount = entries.reduce((count, entry) => (entry.liked? count+1: count), 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. Perfect use of the |
||
|
||
const identifyAllChatMembers = () => { | ||
const senders = [...new Set(entries.map(entry => entry.sender))]; | ||
let participant1 = ''; | ||
let participant2 = ''; | ||
for (const sender of senders) { | ||
if (!participant1) { | ||
participant1 = sender; | ||
} | ||
else if (participant1 && !participant2) { | ||
participant2 = sender; | ||
} | ||
}; | ||
return `${participant1} and ${participant2}`; | ||
}; | ||
Comment on lines
+25
to
+38
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. Essentially we are just getting the sender values of the first and second message objects in the list of messages. Instead of looping through the messages and making a set from their senders we could simply access them and then store in a string like so: const participant1 = entries[0].sender;
const participant2 = entries[1].sender;
const chatTitle = `${participant1} and ${participant2}`; EDIT: I see now that your method would work in cases where the maybe the first two messages are from a single user. Double texting, I could never! haha |
||
|
||
|
||
const App = () => { | ||
return ( | ||
<div id="App"> | ||
<header> | ||
<h1>Application title</h1> | ||
<header className='header-fonts'> | ||
<h1 > Chat Between {identifyAllChatMembers()}</h1> | ||
<h2 className='likes-background-color'> {totalLikesCount} ❤️s</h2> | ||
</header> | ||
<main> | ||
{/* Wave 01: Render one ChatEntry component | ||
Wave 02: Render ChatLog component */} | ||
{/* // Wave 01: Render one ChatEntry component */} | ||
{/* Wave 02: Render ChatLog component */} | ||
|
||
<ChatLog | ||
entries={entries} | ||
onLikeBtnToggle={toggleLikeBtnClick} | ||
/> | ||
Comment on lines
+51
to
+54
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. perf! |
||
|
||
|
||
</main> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,20 +1,38 @@ | ||||||
import PropTypes from 'prop-types'; | ||||||
import TimeStamp from './TimeStamp'; | ||||||
import './ChatEntry.css'; | ||||||
|
||||||
const ChatEntry = () => { | ||||||
|
||||||
const ChatEntry = ({id, sender, body, timeStamp, liked, onLikeToggle}) => { | ||||||
const likeButtonClicked = () => { | ||||||
onLikeToggle(id); | ||||||
}; | ||||||
|
||||||
const locateSender = () => { | ||||||
return (sender === 'Estragon')? 'remote': 'local'; | ||||||
}; | ||||||
|
||||||
return ( | ||||||
<div className="chat-entry local"> | ||||||
<h2 className="entry-name">Replace with name of sender</h2> | ||||||
<div className={`chat-entry ${locateSender()}`}> | ||||||
<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={likeButtonClicked}>{liked? '❤️': '🤍'}</button> | ||||||
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.
Suggested change
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. The button function could also be implemented like so: <button className='like' onClick={() => likeButtonClicked(id)}>{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.isRequired, | ||||||
onLikeToggle: PropTypes.func.isRequired, | ||||||
Comment on lines
+30
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 ChatEntry; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import PropTypes from 'prop-types'; | ||
import './ChatLog.css'; | ||
import ChatEntry from './ChatEntry'; | ||
|
||
const ChatLog = ({entries, onLikeBtnToggle}) =>{ | ||
const chatComponents = entries.map((entry) => { | ||
return( | ||
<ChatEntry | ||
{...entry} | ||
onLikeToggle={onLikeBtnToggle} | ||
key={entry.id} | ||
/> | ||
); | ||
}); | ||
Comment on lines
+6
to
+14
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. okay spread syntax! 😍 |
||
|
||
|
||
return ( | ||
<> | ||
<ul className='chat-log'> | ||
{chatComponents} | ||
</ul> | ||
</> | ||
); | ||
}; | ||
|
||
ChatLog.propTypes = { | ||
// Implement | ||
entries: PropTypes.arrayOf(PropTypes.shape({ | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool.isRequired | ||
})).isRequired, | ||
onLikeBtnToggle: 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; |
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.