Vue.component('product', {
props: {
premium: {
type: Boolean,
required: true
}
},
template:
`
{{description}}
In Stock
Out of Stock
{{sale}}
User is premium: {{premium}}
Shipping: {{shipping}}
Reviews
There are no review(s) yet.
-
{{review.name}}
Rating {{review.rating}}
{{review.review}}
{{review.recommend}}
`,
data () {
return {
brand: 'Vue Mastery',
product: 'Socks',
productUrl: 'https://google.com',
description: 'A pair of warm, fuzzy socks',
//image: './assets/vmSocks-green-onWhite.jpg',
selectedVariant: 0,
//inventory: 100,
onSale: false,
details: ['80% cotton', '20% polyester', 'Gender-neutral'],
variants: [
{
id: 1001,
color: 'green',
image: './assets/vmSocks-green-onWhite.jpg',
style: {
'background-color': 'green'
},
quantity: 10
},
{
id: 1002,
color: 'blue',
image: './assets/vmSocks-blue-onWhite.jpg',
style: {
'background-color': 'blue'
},
quantity: 0
}
],
sizes: ['L', 'M'],
reviews: []
}
},
methods: {
addCartEmit: function() {
this.$emit('add-to-cart', this.variants[this.selectedVariant].id)
},
removeCartEmit() {
this.$emit('remove-from-cart', this.variants[this.selectedVariant].id)
},
updateProduct: function(index) {
this.selectedVariant = index
},
addReview(productReview) {
this.reviews.push(productReview)
}
},
computed: {
title() {
return [this.brand, this.product].join(' ')
},
image() {
return this.variants[this.selectedVariant].image
},
inStock() {
return this.variants[this.selectedVariant].quantity
},
sale() {
const title = this.title;
if (this.onSale) {
return title + 'are on sale!'
}
return title + 'are not on sale!'
},
shipping() {
if (this.premium) return 'Free';
return 5.99
},
isCartEmpty() {
return this.$parent.cart <= 0
}
}
});
Vue.component('product-details', {
props: {
details: {
type: Array,
required: true
}
},
template:
`
`
});
Vue.component('product-review', {
template:
`
`,
data() {
return {
name: null,
review: null,
rating: null,
errors: [],
recommend: null
}
},
methods: {
onSubmit() {
if (this.name && this.review && this.rating && this.recommend) {
let productReview = {
name: this.name,
review: this.review,
rating: this.rating,
recommend: this.recommend
}
this.$emit('review-submitted', productReview);
this.name = this.review = this.rating = this.recommend = null
} else {
if (!this.name) this.errors.push("Name required.")
if (!this.review) this.errors.push("Review required.")
if (!this.rating) this.errors.push("Rating required.")
if (!this.recommend) this.errors.push("Recommendation required.")
}
}
}
});
var app = new Vue({
el: "#app",
data: {
premium: true,
cart: []
},
methods: {
updateCart(id) {
this.cart.push(id)
},
removeFromCart(id) {
const index = this.cart.indexOf(id);
if (index > -1) {
this.cart.splice(index, 1);
}
}
}
})