@@ -50,6 +50,26 @@ Vue.component('product', {
50
50
</button>
51
51
<button @click="removeFromCart">Remove from cart</button>
52
52
</div>
53
+
54
+ <div>
55
+ <h2>Reviews</h2>
56
+
57
+ <p v-if="!reviews.length">There are no reviews yet.</p>
58
+
59
+ <ul>
60
+ <li v-for="review in reviews">
61
+ <p>{{ review.name }}</p>
62
+
63
+ <p>Rating: {{ review.rating }}</p>
64
+
65
+ <p>Recommended: {{ review.recommended }}</p>
66
+
67
+ <p>{{ review.review }}</p>
68
+ </li>
69
+ </ul>
70
+ </div>
71
+
72
+ <productReview @review-submitted="addReview"></productReview>
53
73
</div>
54
74
` ,
55
75
data ( ) {
@@ -73,7 +93,8 @@ Vue.component('product', {
73
93
image : '/img/vmSocks-green-onWhite.jpg' ,
74
94
quantity : 2
75
95
}
76
- ]
96
+ ] ,
97
+ reviews : [ ]
77
98
} ;
78
99
} ,
79
100
methods : {
@@ -88,6 +109,9 @@ Vue.component('product', {
88
109
} ,
89
110
updateProduct ( index ) {
90
111
this . selectedVariant = index ;
112
+ } ,
113
+ addReview ( productReview ) {
114
+ this . reviews . push ( productReview ) ;
91
115
}
92
116
} ,
93
117
computed : {
@@ -120,6 +144,86 @@ Vue.component('productDetails', {
120
144
`
121
145
} ) ;
122
146
147
+ Vue . component ( 'productReview' , {
148
+ template : `
149
+ <form class="review-form" @submit.prevent="onSubmit">
150
+ <p v-if="errors.length">
151
+ <b>Please correct the following error(s):</b>
152
+
153
+ <ul>
154
+ <li v-for="error in errors">{{ error }}</li>
155
+ </ul>
156
+ </p>
157
+
158
+ <p>
159
+ <label for="name">Name:</label>
160
+ <input id="name" v-model="name" placeholder="name" />
161
+ </p>
162
+
163
+ <p>
164
+ <label for="recommended">Will you recommend this product?</label>
165
+ <div>yes: <input id="recommended" v-model="recommended" type="radio" value="yes" /></div>
166
+ <div>no: <input id="recommended" v-model="recommended" type="radio" value="no" /></div>
167
+ </p>
168
+
169
+ <p>
170
+ <label for="review">Review:</label>
171
+ <textarea id="review" v-model="review"></textarea>
172
+ </p>
173
+
174
+ <p>
175
+ <label for="rating">Rating:</label>
176
+ <select id="rating" v-model.number="rating">
177
+ <option>5</option>
178
+ <option>4</option>
179
+ <option>3</option>
180
+ <option>2</option>
181
+ <option>1</option>
182
+ </select>
183
+ </p>
184
+
185
+ <p>
186
+ <input type="submit" value="Submit" />
187
+ </p>
188
+ </form>
189
+ ` ,
190
+ data ( ) {
191
+ return {
192
+ name : null ,
193
+ rating : null ,
194
+ review : null ,
195
+ recommended : null ,
196
+ errors : [ ]
197
+ } ;
198
+ } ,
199
+ methods : {
200
+ onSubmit ( ) {
201
+ if ( this . name && this . review && this . rating ) {
202
+ let productReview = {
203
+ name : this . name ,
204
+ review : this . review ,
205
+ rating : this . rating ,
206
+ recommended : this . recommended
207
+ } ;
208
+
209
+ this . $emit ( 'review-submitted' , productReview ) ;
210
+
211
+ this . name = null ;
212
+ this . review = null ;
213
+ this . rating = null ;
214
+ this . recommended = null ;
215
+ } else {
216
+ this . errors = [ ] ;
217
+
218
+ if ( ! this . name ) this . errors . push ( 'Name required.' ) ;
219
+ if ( ! this . review ) this . errors . push ( 'Review required.' ) ;
220
+ if ( ! this . rating ) this . errors . push ( 'Rating required.' ) ;
221
+ if ( ! this . recommended ) this . errors . push ( 'Recomendation required.' ) ;
222
+ }
223
+ }
224
+ }
225
+ } ) ;
226
+
123
227
var app = new Vue ( {
124
228
el : '#app' ,
125
229
data : {
0 commit comments