let eventBus = new Vue(
);
Vue.component('product', {
props: {
premium: {
type: Boolean,
required: true
}
},
template: `
{{ title }}
In stock
Out of Stock
Shipping: {{ shipping }}
`,
data() {
return {
product: "Socks",
reviews: [],
brand: 'Vue Mastery',
selectedVariant: 0,
altText: "A pair of socks",
details: ['80% cotton', '20% polyester', 'Gender-neutral'],
variants: [
{
variantId: 2234,
variantColor: 'green',
variantImage: "./assets/vmSocks-green-onWhite.jpg",
variantQuantity: 10
},
{
variantId: 2235,
variantColor: 'blue',
variantImage: "./assets/vmSocks-blue-onWhite.jpg",
variantQuantity: 0
}
],
cart: 0
}
},
mounted() {
eventBus.$on('review-submitted', productReview => {
this.reviews.push(productReview)
})
},
methods: {
addToCart() {
this.$emit('add-to-cart', this.variants[this.selectedVariant].variantId);
},
removeToCart() {
this.$emit('remove-to-cart', this.variants[this.selectedVariant].variantId);
},
updateProduct(index) {
this.selectedVariant = index;
console.log(index);
},
},
computed: {
title() {
return this.brand + ' ' + this.product;
},
image() {
return this.variants[this.selectedVariant].variantImage;
},
inStock() {
return this.variants[this.selectedVariant].variantQuantity
},
shipping() {
if (this.premium) {
return "Free";
} else {
return 2.99
}
}
}
})
Vue.component('product-details', {
data() {
return {
details: ['80% cotton', '20% polyester', 'Gender-neutral'],
}
},
template: `
`
,
});
Vue.component('product-review', {
template: `
`,
data() {
return {
name: null,
review: null,
rating: null,
radio: null,
errors: []
}
},
methods: {
onSubmit() {
if(this.name && this.review && this.rating&& this.radio) {
let productReview = {
name: this.name,
review: this.review,
rating: this.rating,
radio: this.radio,
}
eventBus.$emit('review-submitted', productReview)
this.name = null
this.review = null
this.rating = null
this.radio = 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.radio) this.errors.push("Radio required.")
}
}
}
})
Vue.component('product-tabs', {
props: {
reviews: {
type: Array,
required: false
}
},
template: `
There are no reviews yet.
`,
data() {
return {
tabs: ['Reviews', 'Make a Review'],
selectedTab: 'Reviews' // устанавливается с помощью @click
}
},
})
let app = new Vue({
el: '#app',
data: {
premium: true,
cart: []
},
methods: {
updateCart(id) {
this.cart.push(id);
},
removeCart(id) {
let i = this.cart.indexOf(id);
this.cart.splice(i, 1);
}
}
})