66
77 <div class =" product-info" >
88 <h1 >
9- {{ product.name }}
9+ {{ title }}
1010 <!-- eslint-disable-next-line vue/no-unused-vars -->
1111 <i v-for =" i in averageReviewScore" class =" fa fa-star" :key =" i" ></i >
1212 </h1 >
1313
14- <div >All reviews:</div >
14+ <div v-if = " product.reviews.length " >All reviews:</div >
1515 <div v-for =" j in product.reviews" :key =" j.name+j.rating" >
1616 <label >{{ j.name}}: </label >
1717 <i v-for =" k in j.rating" class =" fa fa-star" :key =" k" ></i >
1818 </div >
1919
2020 <div >
21- <p v-if =" product .inventory > 10" >In Stock</p >
22- <p v-else-if =" product .inventory" >Almost sold out</p >
21+ <p v-if =" selectedVariant .inventory > 10" >In Stock</p >
22+ <p v-else-if =" selectedVariant .inventory" >Almost sold out</p >
2323 <p v-else >Out of Stock</p >
2424 </div >
2525
2828
2929 <button
3030 @click =" addToCart"
31- :disabled =" !product .inventory"
32- :class =" ['add-to-cart', product .inventory ? '' : 'disabledButton']"
31+ :disabled =" !selectedVariant .inventory"
32+ :class =" ['add-to-cart', selectedVariant .inventory ? '' : 'disabledButton']"
3333 >
3434 Add to Cart
3535 </button >
3636
37+ <button
38+ @click =" removeFromCart"
39+ :disabled =" cartLength === 0"
40+ :class =" ['remove-from-cart', cartLength > 0 ? '' : 'disabledButton']"
41+ >
42+ Remove from cart
43+ </button >
44+
3745 <div v-for =" (variant, index) in product.variants"
3846 :key =" variant.id"
3947 class =" color-box"
@@ -59,17 +67,19 @@ import ProductReview from './ProductReview.vue';
5967
6068export default class Product extends Vue {
6169 @Prop ({default: false }) private premium! : boolean ;
70+ @Prop () private cartLength! : number ;
6271
6372 product = {
6473 name: " Vue Socks" ,
6574 brand: " Vue" ,
75+ price: 5 ,
6676 variants: [
67- {id: 1 , color: " green" },
68- {id: 2 , color: " blue" }
77+ {id: 1 , color: " green" , inventory: 3 },
78+ {id: 2 , color: " blue" , inventory: 5 }
6979 ],
7080 inventory: 3 ,
7181 reviews: []
72- }
82+ };
7383
7484 selectedVariantIndex = 0 ;
7585
@@ -81,13 +91,27 @@ export default class Product extends Vue {
8191 if (! this .product .reviews .length ) {
8292 return null ;
8393 }
84- return Math .round (this .product .reviews .map (x => x .rating ).reduce ((a , c ) => a + c , 0 ) / this .product .reviews .length );
94+ const reviews = this .product .reviews as any ;
95+ const totalStars = reviews
96+ .map ((x : any ) => x .rating )
97+ .reduce ((a : any , c : any ) => a + c , 0 );
98+
99+ return Math .round (totalStars / this .product .reviews .length );
100+ }
101+
102+ get title() {
103+ return ` ${this .product .name } ($${this .product .price }) ` ;
85104 }
86105
87106 addToCart() {
88- this .product .inventory -- ;
107+ this .selectedVariant .inventory -- ;
108+ this .$emit (' add-to-cart' , {product: this .product , variant: this .selectedVariant });
109+ }
110+
111+ removeFromCart() {
112+ this .product .inventory ++ ;
89113 const selectedVariant = this .product .variants [this .selectedVariantIndex ];
90- this .$emit (' add-to -cart' , {product: this .product , variant: selectedVariant });
114+ this .$emit (' remove-from -cart' , {product: this .product , variant: selectedVariant })
91115 }
92116
93117 addReview(review : any ) {
@@ -148,6 +172,18 @@ button.add-to-cart {
148172 position : absolute ;
149173 top : 85px ;
150174 right : 13px ;
175+ }
176+
177+ button .remove-from-cart {
178+ border : none ;
179+ background-color : #F12321 ;
180+ color : white ;
181+ height : 40px ;
182+ width : 100px ;
183+ font-size : 14px ;
184+ position : absolute ;
185+ top : 135px ;
186+ right : 13px ;
151187}
152188
153189.disabledButton {
0 commit comments