Skip to content

Commit e3390f7

Browse files
authored
Merge pull request #22 from TheLoof/master
issue6 delete button for cart
2 parents 1e455da + 526673e commit e3390f7

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

cli-socks/src/components/Product.vue

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
Add to Cart
2929
</button>
3030

31+
<button
32+
@click="removeFromCart"
33+
:disabled="cartLength === 0"
34+
:class="['remove-from-cart', cartLength > 0 ? '' : 'disabledButton']"
35+
>
36+
Remove from cart
37+
</button>
38+
3139
<div v-for="(variant, index) in product.variants"
3240
:key="variant.id"
3341
class="color-box"
@@ -52,6 +60,7 @@ import ProductReview from './ProductReview.vue';
5260
})
5361
export default class Product extends Vue {
5462
@Prop({default: false}) private premium!: boolean;
63+
@Prop() private cartLength: number;
5564
5665
product = {
5766
name: "Vue Socks",
@@ -88,6 +97,12 @@ export default class Product extends Vue {
8897
this.$emit('add-to-cart', {product: this.product, variant: selectedVariant});
8998
}
9099
100+
removeFromCart() {
101+
this.product.inventory++;
102+
const selectedVariant = this.product.variants[this.selectedVariantIndex];
103+
this.$emit('remove-from-cart', {product: this.product, variant: selectedVariant})
104+
}
105+
91106
addReview(review: any) {
92107
const reviews = this.product.reviews as any;
93108
reviews.push(review.rating);
@@ -146,6 +161,18 @@ button.add-to-cart {
146161
position: absolute;
147162
top: 85px;
148163
right: 13px;
164+
}
165+
166+
button.remove-from-cart {
167+
border: none;
168+
background-color: #F12321;
169+
color: white;
170+
height: 40px;
171+
width: 100px;
172+
font-size: 14px;
173+
position: absolute;
174+
top: 135px;
175+
right: 13px;
149176
}
150177
151178
.disabledButton {

cli-socks/src/views/Home.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div>
3-
<Product :premium="premium" @add-to-cart="updateCart"></Product>
3+
<Product :premium="premium" @add-to-cart="updateCart" @remove-from-cart="removeFromCart" :cartLength="cart.length"></Product>
44

55
<div class="cart">
66
{{ cart.length }}
@@ -26,5 +26,10 @@ export default class Home extends Vue {
2626
console.log("Adding to cart:", product);
2727
this.cart.push(product);
2828
}
29+
30+
removeFromCart(product: any) {
31+
let index = this.cart.indexOf(product);
32+
this.cart.splice(index);
33+
}
2934
}
3035
</script>

0 commit comments

Comments
 (0)