-
Notifications
You must be signed in to change notification settings - Fork 44
Sphinx C22 Izzy #23
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?
Sphinx C22 Izzy #23
Changes from all commits
871f340
2a3e42e
b0861ca
ed7fa88
9ec16b9
5b0d262
751c665
d3746a2
1a8691a
6adaa63
9efd802
1ee7ee3
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,95 @@ | ||
| import './App.css'; | ||
| // import ChatEntry from './components/ChatEntry'; | ||
| import ChatLog from './components/ChatLog'; | ||
| import data from './data/messages.json'; | ||
| import { useState } from 'react'; | ||
|
|
||
| const App = () => { | ||
| const [chatData, setChatData] = useState(data); | ||
| const [fontColor, setFontColor] = useState(null); | ||
|
|
||
| const senderList = []; | ||
| const sendersList = (chatData) => { | ||
| for (const chat of chatData) { | ||
| if (!senderList.includes(chat.sender)) { | ||
| senderList.push(chat.sender); | ||
| } | ||
| } | ||
| let title = ''; | ||
| for (const sender of senderList) { | ||
| title += `${sender} and `; | ||
| } | ||
| return title.slice(0, -5); | ||
| }; | ||
|
|
||
| const sendersTitle = sendersList(chatData); | ||
| const local = senderList[0]; | ||
|
|
||
| const handleLike = (id) => { | ||
| setChatData((chatData) => | ||
| chatData.map((chat) => { | ||
| if (chat.id === id) { | ||
| return { ...chat, liked: !chat.liked }; | ||
| } else { | ||
| return chat; | ||
| } | ||
| }) | ||
| ); | ||
| }; | ||
|
Comment on lines
+28
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. 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 calculateLikes = (chatData) => { | ||
| let total = 0; | ||
| for (const chat of chatData) { | ||
| if (chat.liked) { | ||
| total += chat.liked; | ||
| } | ||
| } | ||
| return total; | ||
| }; | ||
|
Comment on lines
+40
to
+48
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); |
||
|
|
||
| const totalLikes = calculateLikes(chatData); | ||
|
|
||
| const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']; | ||
| const onFontColor = (color) => { | ||
| setFontColor(color); | ||
| }; | ||
| const Colors = () => { | ||
| const divs = []; | ||
| for (let i = 0; i < colors.length; i++) { | ||
| const divClass = `${colors[i]} test`; | ||
| divs.push( | ||
| <div | ||
| key={i} | ||
| className={divClass} | ||
| style={{ | ||
| backgroundColor: colors[i], | ||
| }} | ||
| onClick={() => onFontColor(colors[i])} | ||
| ></div> | ||
| ); | ||
| } | ||
| return <div className='color-container'>{divs}</div>; | ||
| }; | ||
|
Comment on lines
+56
to
+72
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. All the colors of the rainbow! 🌈 |
||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1>Chat Between {sendersTitle}</h1> | ||
| <div className="color-selection-container"> | ||
| <h2>{totalLikes} ❤️s</h2> | ||
| <div className="color-selection-group"> | ||
| <h2>Select Color:</h2> | ||
| <Colors /> | ||
| </div> | ||
| </div> | ||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog | ||
| entries={chatData} | ||
| onLike={handleLike} | ||
| localSender={local} | ||
| fontColor={fontColor} | ||
| ></ChatLog> | ||
|
Comment on lines
+87
to
+92
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. Parents passing on things to their children, we love it! |
||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,4 +97,8 @@ button { | |
|
|
||
| .chat-entry.remote .entry-bubble:hover::before { | ||
| background-color: #a9f6f6; | ||
| } | ||
|
|
||
| .like { | ||
| color: red; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,43 @@ | ||
| import './ChatEntry.css'; | ||
| import TimeStamp from './TimeStamp'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatEntry = (props) => { | ||
| const onClickLike = () => { | ||
| props.onLike(props.id); | ||
| }; | ||
|
Comment on lines
+6
to
+8
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> |
||
|
|
||
| const heart = props.liked ? '❤️' : '🤍'; | ||
| const senderPlacement = | ||
| props.localSender == props.sender | ||
| ? 'chat-entry local' | ||
| : 'chat-entry remote'; | ||
|
Comment on lines
+11
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. ⭐️ |
||
|
|
||
| const ChatEntry = () => { | ||
| 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> | ||
| <div className={senderPlacement}> | ||
| <h2 className='entry-name'>{props.sender}</h2> | ||
| <section className='entry-bubble'> | ||
| <p className={props.fontColor}>{props.body}</p> | ||
| <p className='entry-time'> | ||
| <TimeStamp time={props.timeStamp}></TimeStamp> | ||
| </p> | ||
| <button className='like' onClick={onClickLike}> | ||
| {heart} | ||
| </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, | ||
| onLike: PropTypes.func, | ||
| localSender: PropTypes.string, | ||
| fontColor: PropTypes.string, | ||
| }; | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import ChatEntry from './ChatEntry'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const ChatLog = ({ entries, onLike, localSender, fontColor }) => { | ||
| const message = entries.map((chat) => { | ||
| return ( | ||
| <ChatEntry | ||
| key={chat.id} | ||
| id={chat.id} | ||
| sender={chat.sender} | ||
| body={chat.body} | ||
| timeStamp={chat.timeStamp} | ||
| liked={chat.liked} | ||
| onLike={onLike} | ||
| localSender={localSender} | ||
| fontColor={fontColor} | ||
| ></ChatEntry> | ||
|
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. Since the names of the keys on const chatComponents = entries.map((entry) => {
return(
<ChatEntry
{...entry}
onLikeToggle={onLikeBtnToggle}
key={entry.id}
/>
);
});Just recognize that it is not as explicit as what you have. Something to consider! |
||
| ); | ||
| }); | ||
|
|
||
| return <section>{message}</section>; | ||
| }; | ||
|
|
||
| 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, | ||
| onLike: PropTypes.func, | ||
| localSender: PropTypes.string, | ||
| fontColor: PropTypes.string, | ||
|
Comment on lines
+25
to
+36
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.
Oh, so this function can include names from 2+ users! Nice!