Skip to content

Commit 05ddaf8

Browse files
AndyAndy
authored andcommitted
L11-end
1 parent 5acf089 commit 05ddaf8

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

src/components/ProductDisplay.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<script setup>
22
import { ref, computed } from 'vue'
3+
import ReviewForm from '@/components/ReviewForm.vue'
4+
import ReviewList from '@/components/ReviewList.vue'
35
import socksGreenImage from '@/assets/images/socks_green.jpeg'
46
import socksBlueImage from '@/assets/images/socks_blue.jpeg'
57
@@ -24,6 +26,8 @@ const variants = ref([
2426
{ id: 2235, color: 'blue', image: socksBlueImage, quantity: 0 },
2527
])
2628
29+
const reviews = ref([])
30+
2731
const title = computed(() => {
2832
return brand.value + ' ' + product.value
2933
})
@@ -52,6 +56,10 @@ const addToCart = () => {
5256
const updateVariant = (index) => {
5357
selectedVariant.value = index
5458
}
59+
60+
const addReview = (review) => {
61+
reviews.value.push(review)
62+
}
5563
</script>
5664

5765
<template>
@@ -70,7 +78,7 @@ const updateVariant = (index) => {
7078
</ul>
7179
<div
7280
v-for="(variant, index) in variants"
73-
key="variant.key"
81+
:key="variant.id"
7482
@mouseover="updateVariant(index)"
7583
class="color-circle"
7684
:style="{ backgroundColor: variant.color }"
@@ -86,5 +94,7 @@ const updateVariant = (index) => {
8694
</button>
8795
</div>
8896
</div>
97+
<ReviewList v-if="reviews.length > 0" :reviews="reviews"></ReviewList>
98+
<ReviewForm @review-submitted="addReview"></ReviewForm>
8999
</div>
90100
</template>

src/components/ReviewForm.vue

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@ import { reactive } from 'vue'
33
44
const emit = defineEmits(['review-submitted'])
55
6-
const getDefaultFormData = () => ({
6+
const review = reactive({
77
name: '',
88
content: '',
99
rating: null
1010
})
1111
12-
const review = reactive(getDefaultFormData())
13-
1412
const onSubmit = () => {
15-
const reviewData = JSON.parse(JSON.stringify(review))
16-
emit('review-submitted', reviewData)
17-
Object.assign(review, getDefaultFormData())
13+
if (review.name === '' || review.content === '' || review.rating === null) {
14+
alert('Review is incomplete. Please fill out every field.')
15+
return
16+
}
17+
18+
const productReview = {
19+
name: review.name,
20+
content: review.content,
21+
rating: review.rating
22+
}
23+
emit('review-submitted', productReview)
24+
25+
review.name = ''
26+
review.content = ''
27+
review.rating = null
1828
}
1929
</script>
2030

src/components/ReviewList.vue

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<script setup>
2+
defineProps({
3+
reviews: {
4+
type: Array,
5+
required: true
6+
}
7+
})
8+
</script>
9+
10+
<template>
11+
<div class="review-container">
12+
<h3>Reviews:</h3>
13+
<ul>
14+
<li v-for="(review, index) in reviews" :key="index">
15+
<span>{{ review.name }} gave this {{ review.rating }} stars</span>
16+
<br/>
17+
<span>"{{ review.content }}"</span>
18+
</li>
19+
</ul>
20+
</div>
21+
</template>

0 commit comments

Comments
 (0)