-
Notifications
You must be signed in to change notification settings - Fork 111
Alexa Coffman - Whales #94
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
412f49a
d3d6022
ffb9fdc
b0587cb
36cbede
3512f97
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,46 @@ | ||
| import React from 'react'; | ||
| import React, { useState } from 'react'; | ||
| import './App.css'; | ||
| import ChatLog from './components/ChatLog'; | ||
| import chatMessages from './data/messages.json'; | ||
|
|
||
| let totalLikes = 0; | ||
|
|
||
| const updateLikes = (entryData) => { | ||
| let likes = 0; | ||
| for (const entry of entryData) { | ||
| if (entry.liked) { | ||
| likes += 1; | ||
| } | ||
| } | ||
| totalLikes = likes; | ||
| }; | ||
|
|
||
| const App = () => { | ||
| const [entryData, setEntryData] = useState(chatMessages); | ||
| const updateEntryData = (updatedEntry) => { | ||
| const entries = entryData.map((chatEntry) => { | ||
| if (chatEntry.id === updatedEntry.id) { | ||
| return updatedEntry; | ||
| } else { | ||
| return chatEntry; | ||
| } | ||
| }); | ||
| setEntryData(entries); | ||
| updateLikes(entries); | ||
| }; | ||
|
|
||
| return ( | ||
| <div id="App"> | ||
| <header> | ||
| <h1>Application title</h1> | ||
| <h1>Chat between Vladimir and Estragon</h1> | ||
| <section className="header section"> | ||
| <h4 id="heartWidget" className="widget"> | ||
|
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. Remember, the If you want to change the size of text you should use CSS. |
||
| {totalLikes} ❤️s | ||
| </h4> | ||
| </section> | ||
| </header> | ||
| <main> | ||
| {/* Wave 01: Render one ChatEntry component | ||
| Wave 02: Render ChatLog component */} | ||
| <ChatLog entries={entryData} onUpdateEntry={updateEntryData}></ChatLog> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,68 @@ | ||
| 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 onLikeButtonClick = () => { | ||
| const updatedEntry = { | ||
| id: props.id, | ||
| sender: props.sender, | ||
| body: props.body, | ||
| timeStamp: props.timeStamp, | ||
| liked: !props.liked, | ||
| }; | ||
| props.onUpdate(updatedEntry); | ||
| }; | ||
|
|
||
| const heart = props.liked ? '❤️' : '🤍'; | ||
|
|
||
| if (props.sender === 'Vladimir') { | ||
| return ( | ||
| <div className="chat-entry local"> | ||
| <h2 className="entry-name"> {props.sender} </h2> | ||
| <section className="entry-bubble"> | ||
| <p> {props.body} </p> | ||
| <p className="entry-time"> | ||
| <TimeStamp | ||
| time={props.timeStamp} | ||
| className="entry-time" | ||
| ></TimeStamp> | ||
| </p> | ||
| <button className="like" onClick={onLikeButtonClick}> | ||
| {heart} | ||
| </button> | ||
| </section> | ||
| </div> | ||
| ); | ||
| } else { | ||
| return ( | ||
| <div className="chat-entry remote"> | ||
| <h2 className="entry-name"> {props.sender} </h2> | ||
| <section className="entry-bubble"> | ||
| <p> {props.body} </p> | ||
| <p className="entry-time"> | ||
| <TimeStamp | ||
| time={props.timeStamp} | ||
| className="entry-time" | ||
| ></TimeStamp> | ||
| </p> | ||
| <button className="like" onClick={onLikeButtonClick}> | ||
| {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, | ||
| onUpdate: PropTypes.func.isRequired, | ||
| }; | ||
|
|
||
| export default ChatEntry; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import React from 'react'; | ||
| import ChatEntry from './ChatEntry'; | ||
| import PropTypes from 'prop-types'; | ||
| import './ChatLog.css'; | ||
|
|
||
| const ChatLog = (props) => { | ||
| const chatComponents = props.entries.map((chatEntry) => { | ||
| return ( | ||
| <section> | ||
| <ChatEntry | ||
| id={chatEntry.id} | ||
| sender={chatEntry.sender} | ||
| body={chatEntry.body} | ||
| timeStamp={chatEntry.timeStamp} | ||
| liked={chatEntry.liked} | ||
| onUpdate={props.onUpdateEntry} | ||
| ></ChatEntry> | ||
| </section> | ||
|
Comment on lines
+9
to
+18
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 extra section doesn't hurt anything but isn't required, FYI. |
||
| ); | ||
| }); | ||
|
|
||
| return ( | ||
| <section> | ||
| <ul className="chat-log">{chatComponents}</ul> | ||
| </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, | ||
| }) | ||
| ), | ||
| onUpdateEntry: 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.
totalLikesshould have been stored in auseStatevariable.When you update state in React it triggers a re-render automatically. Updating a global variable won't. (Also if you instantiated the app twice on the same page you'd be in trouble.)