This repository has been archived by the owner on Apr 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
255 lines (221 loc) · 6.99 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
var eventBus = new Vue();
Vue.component('product-review', {
data() {
return {
name: null,
review: null,
rating: null,
recommendation: null,
errors: [],
}
},
methods: {
onSubmit() {
console.log(this.recommendation)
if( this.name && this.rating && this.review && this.recommendation ) {
let productReview = {
name: this.name,
review: this.review,
rating: this.rating,
recommendation: this.recommendation
}
eventBus.$emit('review-submitted', productReview)
this.name = null,
this.review = null,
this.rating = null,
this.recommendation = null
}
else {
this.errors = [];
if( !this.name ) this.errors.push("Name is required.")
if( !this.review ) this.errors.push("Review is required.")
if( !this.rating ) this.errors.push("Rating is required.")
if( !this.recommendation ) this.errors.push("Must decide on a recommendation.")
}
}
},
template: `
<form class="review-form" @submit.prevent="onSubmit">
<p class="errors" v-if="errors.length">
<b>Please correct the following error(s):</b>
<ul v-for="error in errors">
<li>{{ error }}</li>
</ul>
</p>
<p>
<label for="name">Name:</label>
<input id="name" v-model="name">
</p>
<p>
<label for="review">Review:</label>
<textarea id="review" v-model="review"></textarea>
</p>
<p>
<label for="rating">Rating:</label>
<select id="rating" v-model.number="rating">
<option>5</option>
<option>4</option>
<option>3</option>
<option>2</option>
<option>1</option>
</select>
</p>
<p>Would you recommend this product?</p>
<label style="display: inline-block">
Yes <input type="radio" name="recommendation" v-model="recommendation" value="yes">
</label>
<label style="display: inline-block">
No <input type="radio" name="recommendation" v-model="recommendation" value="no">
</label>
<p>
<input type="submit" value="Submit">
</p>
</form>
`
})
Vue.component('product-tabs', {
props: {
reviews: {
type: Array,
required: false,
default() { return [] }
}
},
data() {
return {
tabs: ['Reviews', 'Add a review'],
selectedTab: 'Reviews',
}
},
template: `
<div>
<a class="tab"
:class="{ activeTab: selectedTab === tab }"
v-for="(tab, idx) in tabs"
:key="idx"
@click="selectedTab = tab"
@keyup.enter="selectedTab = tab"
@keyup.space="selectedTab = tab"
tabindex="0"
>{{ tab }}</a>
<div v-show="selectedTab === 'Reviews'">
<p v-if="!reviews.length">There are no reviews yet.</p>
<ul v-else>
<li v-for="review in reviews">
<p class="ratingTitle">{{ review.name }} (Rating: {{ review.rating }})</p>
<p class="ratingText">{{ review.review }}</p>
<small v-if="review.recommendation == 'yes'">Product is recommended</small>
</li>
</ul>
</div>
<product-review v-show="selectedTab === 'Add a review'"></product-review>
</div>
`
});
Vue.component( 'product', {
props: {
premium: {
type: Boolean,
required: true
}
},
data() {
return {
brand: 'Vue Mastery',
product: "Socks",
details: ["80% cotton", "20% polyester", "Gender-neutral"],
selectedVariant: 0,
variants: [
{
variantId: 2234,
variantColor: "green",
variantImage: "./assets/vmSocks-green.jpg",
variantQty: 10,
},
{
variantId: 2235,
variantColor: "blue",
variantImage: "./assets/vmSocks-blue.jpg",
variantQty: 5,
}
],
onSale: false,
reviews: [],
}
},
computed: {
title() {
return this.brand + ' ' + this.product;
},
image() {
return this.variants[ this.selectedVariant ].variantImage;
},
inStock() {
return this.variants[ this.selectedVariant ].variantQty;
},
productSale() {
return this.onSale ? 'ON SALE!' : '';
},
shipping() {
if (this.premium) {
return "Free"
}
return 2.99
}
},
mounted() {
eventBus.$on('review-submitted', (productReview) => {
this.reviews.push(productReview)
})
},
methods: {
addToCart() {
this.$emit('add-to-cart', this.variants[ this.selectedVariant ].variantId);
},
updateProduct(index) {
this.selectedVariant = index;
},
},
template:
`
<div class="product">
<div class="product-image">
<img v-bind:src="image" alt="">
</div>
<div class="product-info">
<h1>{{ title }} <span style="color:red; font-style:italic;">{{ productSale }}</span></h1>
<p v-if="inStock">In Stock</p>
<p v-else :class="{ outOfStockClass: !inStock }">Out of Stock</p>
<p>Shipping: {{ shipping }}</p>
<ul>
<li v-for="detail in details">{{ detail }}</li>
</ul>
<div v-for="(variant, index) in variants"
:key="variant.variantId"
class="color-box"
:style="{ backgroundColor: variant.variantColor }"
@mouseover="updateProduct(index)"
>
</div>
<button
@click="addToCart"
:class="{disabledButton: !inStock}"
:disabled="!inStock"
>Add to Cart</button>
</div>
<product-tabs :reviews="reviews"></product-tabs>
</div>
`
});
var app = new Vue({
el: "#app",
data: {
premium: true,
cart: [],
},
methods: {
updateCart( id ) {
this.cart.push(id);
},
}
});