-
Notifications
You must be signed in to change notification settings - Fork 40
Viktoriia_L_MagicMessages #29
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
283827f
1456699
206ca40
8ded406
4ed24e2
428eb89
d6b7b49
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,30 @@ | ||
| import './App.css'; | ||
|
|
||
| import ChatLog from './components/ChatLog'; | ||
| import chatMessages from './data/messages.json'; | ||
| import { useState } from 'react'; | ||
| import { | ||
| toggleLikedStatus, | ||
| calculateTotalLikeCount, | ||
| getParticipants, | ||
| } from './Helper_functions/helper_message'; | ||
| const App = () => { | ||
| const [messages, setMessages] = useState(chatMessages); | ||
|
|
||
| const toggleLike = (id) => { | ||
| const updatedMessages = toggleLikedStatus(messages, id); | ||
| setMessages(updatedMessages); | ||
| }; | ||
| const countLikes = calculateTotalLikeCount(messages); | ||
| const chatHeader = getParticipants(messages); | ||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1> {chatHeader} </h1> | ||
|
Author
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. @yangashley Hi Ashley! I accidentally clicked "Resolve conversation" earlier sorry about that! Here is your comment on the header "I like that you put your own spin on this project, it's cute! An element missing from your project is the header "Chat Between X and Y". Here you have "Magic Messages". @Viktoria2609 How would you create a header that reflects the screenshot below? It can be done by hardcoding the string in the h1 tag, but I'm wondering how would you programmatically get the names of the participants in the chat? Please respond to this comment with your answer, thank you!" ---- so thank you for pointing out for this. I misunderstood the requirements . Now, I created a getParticipants() helper to dynamically generate the "Magic Messages between X and Y" header based on the message data. |
||
| <h2>{countLikes} 💖</h2> | ||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog entries={messages} onLikeToggle={toggleLike} /> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| export const toggleLikedStatus = (messages, id) => { | ||
| return messages.map((msg) => | ||
| msg.id === id ? { ...msg, liked: !msg.liked } : msg | ||
| ); | ||
| }; | ||
|
|
||
| export const calculateTotalLikeCount = (messages) => { | ||
| return messages.reduce((acc, msg) => (msg.liked ? acc + 1 : acc), 0); | ||
| }; | ||
|
|
||
| export const getParticipants = (messages) => { | ||
| const uniqueSenders = [...new Set(messages.map(msg => msg.sender))]; | ||
| return `Magic Messages between ${uniqueSenders.join(" & ")}`; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,33 @@ | ||
| import './ChatEntry.css'; | ||
| import PropTypes from 'prop-types'; | ||
| import TimeStamp from './TimeStamp'; | ||
|
|
||
| const ChatEntry = ({ id, sender, body, timeStamp, liked, onLikeToggle }) => { | ||
| const bubbleClass = sender === 'Allen' ? 'local' : 'remote'; | ||
|
|
||
| const ChatEntry = () => { | ||
| return ( | ||
| <div className="chat-entry local"> | ||
| <h2 className="entry-name">Replace with name of sender</h2> | ||
| <div className={`chat-entry ${bubbleClass}`}> | ||
| <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} /> | ||
|
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 using the provided |
||
| </p> | ||
| <button className="like" onClick={() => onLikeToggle(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, | ||
| }; | ||
|
|
||
| export default ChatEntry; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import ChatEntry from './ChatEntry'; | ||
| import PropTypes from 'prop-types'; | ||
| import './ChatLog.css'; | ||
|
|
||
| const ChatLog = ({ entries, onLikeToggle }) => { | ||
| return ( | ||
| <div className="chat-log"> | ||
| {entries.map((entry) => ( | ||
| <ChatEntry | ||
| key={entry.id} | ||
| id={entry.id} | ||
| sender={entry.sender} | ||
| body={entry.body} | ||
| timeStamp={entry.timeStamp} | ||
| liked={entry.liked} | ||
| onLikeToggle={onLikeToggle} | ||
| /> | ||
| ))} | ||
| </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, | ||
| }) | ||
| ).isRequired, | ||
| onLikeToggle: 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.
👍