-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathCouponCode.vue
63 lines (54 loc) · 1.66 KB
/
CouponCode.vue
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
<template>
<div>
<input type="text" class="coupon-code" v-model="code" @input="validate">
<p v-text="feedback"></p>
</div>
</template>
<script>
export default {
data () {
return {
code: '',
// In real life, you wouldn't hardcode this coupons array.
// Instead, your validate() method would fire an AJAX
// request to your server to check if the coupon is real.
// To keep the demo simple, we'll hardcode the list.
coupons: [
{
code: '10OFF',
message: '10% Off!',
discount: 10
},
{
code: 'FREE',
message: 'Entirely Free!',
discount: 100
}
],
valid: false
};
},
computed: {
selectedCoupon () {
return this.coupons.find(coupon => coupon.code == this.code);
},
message () {
return this.selectedCoupon.message;
},
feedback () {
if (this.valid) {
return `Coupon Redeemed: ${this.message}`;
}
return 'Invalid Coupon Code';
}
},
methods: {
validate () {
this.valid = !! this.selectedCoupon;
if (this.valid) {
this.$emit('applied', this.selectedCoupon.discount);
}
}
}
}
</script>