diff --git a/README.md b/README.md index 258a878..76b17ec 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ Fork, clone, run yarn install, yarn start, pull request #### Do - * Add a new class component for Reviews - * Make sure to use extends and super - * Import and use this component in ProductDetail - * This component will take a product from props - * It will show the number of reviews followed by "review" or "reviews" depending on if there is one or more reviews - * It will create a list of the reviews description which will inititally be hidden - * When the word "review" is clicked show the reviews - * When clicked again, hide the reviews \ No newline at end of file + *- Add a new class component for Reviews + *- Make sure to use extends and super + *- Import and use this component in ProductDetail + *- This component will take a product from props + *- It will show the number of reviews followed by "review" or "reviews" depending on if there is one or more reviews + *- It will create a list of the reviews description which will inititally be hidden + *- When the word "review" is clicked show the reviews + *- When clicked again, hide the reviews diff --git a/src/App.js b/src/App.js index 8dfa97f..d37bf15 100644 --- a/src/App.js +++ b/src/App.js @@ -8,7 +8,7 @@ import Carousel from './components/Carousel'; function App (props) { var products = props.state.products.map(function(prod){ - return ; + return ; }); return (
diff --git a/src/components/ProductDetail.js b/src/components/ProductDetail.js index 885919a..0ffcd32 100644 --- a/src/components/ProductDetail.js +++ b/src/components/ProductDetail.js @@ -1,10 +1,11 @@ import React from "react"; +import Reviews from "./Reviews"; function ProductDetail(props) { const {name,description,rating,imgUrl} = props.product; const stars = []; for (let i = 0; i < rating; i++) { - stars.push(); + stars.push(); } return ( @@ -14,11 +15,13 @@ function ProductDetail(props) {

{name}

-

{description} +

{description}

-

15 reviews

+

+ +

{stars}

diff --git a/src/components/Reviews.js b/src/components/Reviews.js new file mode 100644 index 0000000..50d2c3b --- /dev/null +++ b/src/components/Reviews.js @@ -0,0 +1,43 @@ +import React from "react"; + +class Reviews extends React.Component{ + constructor(props){ + super(props); + this.state = { + visible: false, + }; + this.handleReviewClick = this.handleReviewClick.bind(this); + } + + handleReviewClick() { + this.setState(function(prevState) { + return { + visible: !prevState.visible + } + }); + } + + render() { + let reviews = this.props.product; + let numOfReviews = reviews.length; + + const reviewDiv = reviews.map(review => { + if(this.state.visible) { + return ( +
+

{review.description} {review.rating}

+
+ ) + } + }); + + return ( +
+ {numOfReviews} review{numOfReviews <= 1 ? " " : "s" } +
{reviewDiv}
+
+ ); + } +} + +export default Reviews;