Skip to content

Commit

Permalink
Adding/added states
Browse files Browse the repository at this point in the history
  • Loading branch information
royduin committed Oct 20, 2021
1 parent 79a273c commit e712f48
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 33 deletions.
84 changes: 54 additions & 30 deletions resources/js/components/amastybundles.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
export default {
mixins: [GetCart],
props: ['mainProduct', 'bundle'],
props: {
mainProduct: Object,
bundle: Object,
addedDuration: {
type: Number,
default: 3000,
}
},
data: () => ({
selectedProducts: [],
options: {}
options: {},
adding: false,
added: false,
}),
render() {
Expand All @@ -18,6 +27,9 @@
selectedProducts: this.selectedProducts,
addToCart: this.addToCart,
options: this.options,
adding: this.adding,
added: this.added,
});
},
Expand All @@ -39,39 +51,51 @@
methods: {
async addToCart() {
this.added = false
this.adding = true
await this.getMask()
Object.entries(this.selectedProducts).forEach(async ([itemIndex, itemChecked]) => {
if (itemChecked) {
let product = this.bundle.items[itemIndex]
await this.magentoCart('post', 'items', {
cartItem: {
sku: product.product.sku,
qty: 1,
quote_id: localStorage.mask
}
})
}
})
try {
Object.entries(this.selectedProducts).forEach(async ([itemIndex, itemChecked]) => {
if (itemChecked) {
let product = this.bundle.items[itemIndex]
await this.magentoCart('post', 'items', {
cartItem: {
sku: product.product.sku,
qty: 1,
quote_id: localStorage.mask
}
})
}
})
let response = await this.magentoCart('post', 'items', {
cartItem: {
sku: this.mainProduct.sku,
qty: 1,
quote_id: localStorage.mask,
product_option: this.productOptions
}
})
let response = await this.magentoCart('post', 'items', {
cartItem: {
sku: this.mainProduct.sku,
qty: 1,
quote_id: localStorage.mask,
product_option: this.productOptions
}
})
// Just a workaround to make sure all calculations are triggered.
await this.magentoCart('put', 'items/' + response.data.item_id, {
cartItem: {
quote_id: localStorage.mask,
qty: response.data.qty
}
})
// Just a workaround to make sure all calculations are triggered.
await this.magentoCart('put', 'items/' + response.data.item_id, {
cartItem: {
quote_id: localStorage.mask,
qty: response.data.qty
}
})
await this.refreshCart()
this.added = true
setTimeout(() => { this.added = false }, this.addedDuration)
} catch (error) {
Notify(error.response.data.message, 'error')
}
await this.refreshCart()
this.adding = false
},
discountMultiplier(percentage) {
Expand Down
10 changes: 7 additions & 3 deletions resources/views/components/productbundles.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div slot-scope="{ data }" v-if="data">
<div v-for="bundle in data.amMostviewedBundlePacks.items">
<amastybundles :main-product="data.amMostviewedBundlePacks.main_product" :bundle="bundle">
<div class="mb-3" slot-scope="{ bundlePrice, bundleDiscountAmount, selectedProducts, addToCart, options }">
<div class="mb-3" slot-scope="{ bundlePrice, bundleDiscountAmount, selectedProducts, addToCart, options, adding, added }">
<div class="mb-3 font-bold text-lg">@{{ bundle.block_title }}</div>
<form class="flex flex-col sm:flex-row" v-on:submit.prevent="addToCart">
<x-amastyrelatedproducts::productbundle-item
Expand Down Expand Up @@ -75,8 +75,12 @@ class="object-contain h-40 w-full"
@{{ bundleDiscountAmount | price }}
</div>
<x-rapidez::button type="submit" class="flex items-center mx-auto mt-3">
<svg v-if="$root.loading" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="spinner" class="w-5 h-5 animate-spin mr-1" role="img" viewBox="0 0 512 512"><path fill="currentColor" d="M304 48c0 26.51-21.49 48-48 48s-48-21.49-48-48 21.49-48 48-48 48 21.49 48 48zm-48 368c-26.51 0-48 21.49-48 48s21.49 48 48 48 48-21.49 48-48-21.49-48-48-48zm208-208c-26.51 0-48 21.49-48 48s21.49 48 48 48 48-21.49 48-48-21.49-48-48-48zM96 256c0-26.51-21.49-48-48-48S0 229.49 0 256s21.49 48 48 48 48-21.49 48-48zm12.922 99.078c-26.51 0-48 21.49-48 48s21.49 48 48 48 48-21.49 48-48c0-26.509-21.491-48-48-48zm294.156 0c-26.51 0-48 21.49-48 48s21.49 48 48 48 48-21.49 48-48c0-26.509-21.49-48-48-48zM108.922 60.922c-26.51 0-48 21.49-48 48s21.49 48 48 48 48-21.49 48-48-21.491-48-48-48z"/></svg>
@lang('Add to cart')
<x-heroicon-o-shopping-cart class="h-5 w-5 mr-2" v-if="!adding && !added" />
<x-heroicon-o-refresh class="h-5 w-5 mr-2 animate-spin" v-if="adding" />
<x-heroicon-o-check class="h-5 w-5 mr-2" v-if="added" />
<span v-if="!adding && !added">@lang('Add bundle')</span>
<span v-if="adding">@lang('Adding')...</span>
<span v-if="added">@lang('Added')</span>
</x-rapidez::button>
</div>
</form>
Expand Down

0 comments on commit e712f48

Please sign in to comment.