-
Notifications
You must be signed in to change notification settings - Fork 113
Chatlog #102
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?
Chatlog #102
Changes from all commits
d69ec3a
0a516ea
27287be
72611b9
b706be4
afeec56
e0a3b04
d968b36
37ad3e2
e8fe8ad
ba2c32f
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,66 @@ | ||
| import React from 'react'; | ||
| import './App.css'; | ||
| import chatMessages from './data/messages.json'; | ||
| import ChatLog from './components/ChatLog'; | ||
| import ColorChoice from './components/ColorChoice'; | ||
|
|
||
| const App = () => { | ||
| const [messages, setMessages] = React.useState(chatMessages); | ||
|
|
||
| const likeMessage = (id) => { | ||
| setMessages( | ||
| messages.map((message) => (message.id === id ? { | ||
| ...message, | ||
| liked: !message.liked | ||
| } : message)) | ||
| ); | ||
| }; | ||
|
Comment on lines
+10
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. This is an optimal approach to update the state of the heart without needing the entire entry, all you need to do is send the id of the liked entry up to App.js. |
||
|
|
||
| const [colorChoice, setColor] = React.useState({ | ||
| [messages[0].sender]: 'red', | ||
| [messages[1].sender]: 'green' | ||
| }) | ||
|
|
||
| const setColorChoice = (sender, chosenColor) => { | ||
| console.log(sender, chosenColor) | ||
| setColor(prevColor => { | ||
| return { | ||
| ...prevColor, | ||
| [sender]: chosenColor | ||
| } | ||
| }) | ||
| } | ||
|
|
||
|
|
||
| console.log(colorChoice) | ||
|
|
||
| let totalHearts = 0; | ||
|
|
||
| for (let message of messages) { | ||
| if (message.liked) { | ||
| totalHearts++ | ||
| } | ||
| } | ||
|
Comment on lines
+37
to
+43
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 is an optimal approach to calculating the number of likes, great job 😁 This is a great place to calculate & display the total likes |
||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1>Chat between | ||
| <i className={colorChoice[messages[0].sender]}>{messages[0].sender}</i> and <i className={colorChoice[messages[1].sender]}>{messages[1].sender}</i></h1> | ||
| <section> | ||
| <span className="widget"> | ||
| <ColorChoice sender={messages[0].sender} setColorCallback={setColorChoice}/> | ||
| </span> | ||
| <span id="heartWidget"> | ||
| <p>{totalHearts} ❤️s</p> | ||
| </span> | ||
| <span className="widget"> | ||
| <ColorChoice sender={messages[1].sender} setColorCallback={setColorChoice}/> | ||
| </span> | ||
| </section> | ||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog entries={messages} colorChoice={colorChoice} likeMessage={likeMessage} /> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| 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) => { | ||
| 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 ChatEntry = ({ id, sender, body, timeStamp, liked, likeMessage, colorChoice = 'black' }) => { | ||
| const likedHeart = '❤️'; | ||
| const notLikedHeart = '🤍'; | ||
| const likedClass = liked ? likedHeart : notLikedHeart; | ||
|
|
||
| const bubbleClass = colorChoice[sender] ?? 'black' | ||
|
|
||
| // console.log(bubbleClass) | ||
|
|
||
| const stateMessage = (sender) => { | ||
| return sender === 'Vladimir' ? 'local' : '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. Nice work completing this optional alignment feature |
||
|
|
||
| return ( | ||
| <div className={'chat-entry ' + stateMessage(sender)}> | ||
| <h2 className={`entry-name ${bubbleClass}`}>{sender}</h2> | ||
| <section className={`entry-bubble ${bubbleClass}`}> | ||
| <p>{body}</p> | ||
| <p className="entry-time"> | ||
| <TimeStamp time={timeStamp} /> | ||
| </p> | ||
|
Comment on lines
+24
to
+26
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 job utilizing the TimeStamp component provided! |
||
| <button className="like" onClick={() => likeMessage(id)}> | ||
|
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 need a function that will update the state of the entry that is click |
||
| {likedClass} | ||
| </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, | ||
| colorChoice: PropTypes.func | ||
|
Comment on lines
35
to
+41
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,27 @@ | ||
| import React from 'react' | ||
| import './ChatLog.css' | ||
| import ChatEntry from './ChatEntry' | ||
|
|
||
| const ChatLog = ({ entries, likeMessage, colorChoice }) => { | ||
| const entryComponents = entries.map(entry => { | ||
| return ( | ||
| <ChatEntry | ||
| id= {entry.id} | ||
| sender= {entry.sender} | ||
| body= {entry.body} | ||
| timeStamp= {entry.timeStamp} | ||
| liked= {entry.liked} | ||
| likeMessage= {likeMessage} | ||
| colorChoice={colorChoice} | ||
| /> | ||
|
Comment on lines
+6
to
+16
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 job utilizing this map feature to iterate over the entries 👍🏾 |
||
| ) | ||
| }) | ||
|
|
||
| return ( | ||
| <div className="chat-log"> | ||
| <section>{entryComponents}</section> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default ChatLog | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| ul { | ||
| list-style: none; | ||
| display: flex; | ||
| flex-direction: row; | ||
| gap: 10px; | ||
| overflow: hidden; | ||
| padding: 0 | ||
| } | ||
|
|
||
| #color-picker { | ||
| display: flex; | ||
| flex-direction: column; | ||
| justify-content: center; | ||
| align-items: center; | ||
| } | ||
|
|
||
| li { | ||
| float: left; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import React from 'react'; | ||
| import './ColorChoice.css' | ||
|
|
||
| const ColorChoice = ({ setColorCallback, sender }) => { | ||
| return ( | ||
| <div> | ||
| <p>{`${sender}'s Color:`}</p> | ||
| <ul> | ||
| <li onClick={() => setColorCallback(sender, 'red')}>🔴</li> | ||
| <li onClick={() => setColorCallback(sender, 'orange')}>🟠</li> | ||
|
Comment on lines
+4
to
+10
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. 🎉 |
||
| <li onClick={() => setColorCallback(sender, 'yellow')}>🟡</li> | ||
| <li onClick={() => setColorCallback(sender, 'green')}>🟢</li> | ||
| <li onClick={() => setColorCallback(sender, 'blue')}>🔵</li> | ||
| <li onClick={() => setColorCallback(sender,'purple')}>🟣</li> | ||
| </ul> | ||
| </div> | ||
|
|
||
| ) | ||
| } | ||
|
|
||
| export default ColorChoice | ||
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 need a piece of state to track changes to our messages, initialized from the json file we loaded.