Skip to content

Commit bdc0330

Browse files
committed
Added functionality for adding new reviews
1 parent a8240b1 commit bdc0330

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/ViewProduct.vue

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@
2222
<div v-else>
2323
<p>No reviews have been added for this product.</p>
2424
</div>
25+
26+
<h3>Add Review</h3>
27+
28+
<form @submit.prevent="addNewReview(newReview)">
29+
<div class="form-group">
30+
<label for="reviewName">Name</label>
31+
<input type="text" v-model="newReview.reviewer" class="form-control" id="reviewName" placeholder="Name">
32+
</div>
33+
34+
<div class="form-group">
35+
<label for="reviewRating">Rating</label>
36+
<input type="number" v-model.number="newReview.rating" class="form-control" id="reviewRating" placeholder="Rating">
37+
</div>
38+
39+
<div class="form-group">
40+
<label for="reviewText">Text</label>
41+
<textarea v-model="newReview.text" class="form-control" id="reviewText" cols="30" rows="10"></textarea>
42+
</div>
43+
44+
<button type="submit" class="btn btn-primary">Submit Review</button>
45+
</form>
2546
</div>
2647

2748
<div v-else>
@@ -39,7 +60,12 @@
3960
},
4061
data() {
4162
return {
42-
product: null
63+
product: null,
64+
newReview: {
65+
text: '',
66+
rating: 0,
67+
reviewer: ''
68+
}
4369
};
4470
},
4571
created() {
@@ -65,6 +91,16 @@
6591
},
6692
goBack() {
6793
this.$router.history.go(-1);
94+
},
95+
addNewReview(review) {
96+
this.$http.post('http://localhost:3000/products/{productId}/reviews', review, {
97+
params: {
98+
productId: this.product.id
99+
}
100+
}).then(
101+
response => response.json(),
102+
response => alert("Could not add review!")
103+
).then(newReview => this.product.reviews.push(newReview));
68104
}
69105
}
70106
}

0 commit comments

Comments
 (0)