-
Notifications
You must be signed in to change notification settings - Fork 153
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
Elsje S - Snow Leopards #112
base: main
Are you sure you want to change the base?
Changes from all commits
eb75c55
7e82efc
9121f06
fe02f47
471d0ea
ec7049a
fe99714
a0036b8
5995dc4
6fd7f53
481fc94
a1ab30e
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,67 @@ | ||
import React from 'react'; | ||
import React, { useState } from 'react'; | ||
import './App.css'; | ||
import chatMessages from './data/messages.json'; | ||
import ChatLog from './components/ChatLog'; | ||
|
||
const App = () => { | ||
const [chatData, setChatData] = useState(chatMessages); | ||
|
||
const toggleLikeButton = (id) => { | ||
setChatData(chatData => chatData.map(chat => { | ||
if (chat.id === id) { | ||
return {...chat, liked: !chat.liked} | ||
} else { | ||
return chat; | ||
} | ||
})) | ||
}; | ||
|
||
const calcTotalLikes = (chatData) => { | ||
let everyHeart = '❤️' | ||
let numberOfHearts = 0 | ||
return chatData.reduce((likeTotal, chat) => { | ||
if (chat.liked) { | ||
numberOfHearts += 1 | ||
} | ||
return `${numberOfHearts}${everyHeart}`; | ||
}, 1); | ||
}; | ||
|
||
const displayTotalLikes = calcTotalLikes(chatData); | ||
|
||
return ( | ||
<div id="App"> | ||
<header> | ||
<h1>Application title</h1> | ||
<header className="App-header"> | ||
<h1>Ghibli Jabber</h1> | ||
<section> | ||
<h2 className="likes-counter" >Likes: {displayTotalLikes}</h2> | ||
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. Following up on the earlier comment, I mean you can do this if you follow the reduce pattern I laid out:
Which will help your tests to pass as well. |
||
<h2 className="movie-selector">Movie: | ||
<select> | ||
<option>Default Studio Ghibli</option> | ||
<option>Howl's Moving Castle</option> | ||
<option>My Neighbor Totoro</option> | ||
<option>Secret World of Arietty</option> | ||
</select> | ||
</h2> | ||
<h2 className="textbox-selector"> | ||
Textbox (or heart?) color: | ||
<select> | ||
<option>Red</option> | ||
<option>Orange</option> | ||
<option>Yellow</option> | ||
<option>Green</option> | ||
<option>Blue</option> | ||
</select> | ||
</h2> | ||
</section> | ||
</header> | ||
<main> | ||
{/* Wave 01: Render one ChatEntry component | ||
Wave 02: Render ChatLog component */} | ||
<div> | ||
<ChatLog | ||
chats={chatData} | ||
onLiked={toggleLikeButton} | ||
/> | ||
</div> | ||
</main> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ describe('Wave 03: clicking like button and rendering App', () => { | |
let buttons = container.querySelectorAll('button.like') | ||
|
||
// Act | ||
fireEvent.click(buttons[0]) | ||
fireEvent.click(buttons[5]) | ||
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. There's no need to modify the tests here and doing so could cause false positives/negatives unexpectedly. |
||
fireEvent.click(buttons[1]) | ||
fireEvent.click(buttons[10]) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,31 @@ | ||
import React from 'react'; | ||
import './ChatEntry.css'; | ||
import PropTypes from 'prop-types'; | ||
import PropTypes from 'prop-types'; | ||
// import TimeStamp from './TimeStamp.js'; | ||
|
||
const ChatEntry = (props) => { | ||
// const convertedTimeStamp = TimeStamp(props); | ||
const heart = props.liked ? '❤️' : '🤍'; | ||
|
||
return ( | ||
<div className="chat-entry local"> | ||
<h2 className="entry-name">Replace with name of sender</h2> | ||
<h2 className="entry-name">{props.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>{props.body}</p> | ||
<p className="entry-time">{props.timeStamp}</p> | ||
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 usage you're looking for is (first uncomment the Timestamp component import on line 4. Then this line: <p className="entry-time"><TimeStamp time={props.timeStamp}/></p> |
||
<button className="like" onClick={() => props.onLiked(props.id)}>{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, | ||
}; | ||
|
||
|
||
export default ChatEntry; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import ChatEntry from './ChatEntry'; | ||
import './ChatLog.css'; | ||
|
||
const ChatLog = (entries) => { | ||
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 expected name for the argument passed to a React component is |
||
const getChatLogJSX = (chats) => { | ||
return chats.map((chat) => { | ||
return ( | ||
<ChatEntry | ||
id={chat.id} | ||
sender={chat.sender} | ||
body={chat.body} | ||
timeStamp={chat.timeStamp} | ||
liked={chat.liked} | ||
key={chat.id} | ||
onLiked={entries.onLiked} | ||
/> | ||
); | ||
}); | ||
}; | ||
return <div className="chat-log">{getChatLogJSX(entries.chats)}</div> | ||
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. I think I see where the confusion about |
||
}; | ||
|
||
ChatLog.propTypes = { | ||
chats: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
id: PropTypes.number.isRequired, | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool.isRequired, | ||
})), | ||
onLiked: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default ChatLog; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// "avoidEscape": true; | ||
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 can just delete this commented out code. |
||
|
||
import React from "react"; | ||
import "@testing-library/jest-dom/extend-expect"; | ||
import ChatLog from "./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.
Nice job with this calculation! Here's another way to write this that doesn't necessitate a variable outside the scope of the
reduce
:Then you also don't have to do the formatting in the template string and you can do it in the JSX instead.