[IMP] website_sale: quantity in js sessionStorage to avoid server calls

The session value is updated each time the user modifies the cart.
The number is also updated when the user hovers the mouse over the cart,
and if the value is different from that of the HTML content (if the user
does undo in his history).

opw-2737985

Part-of: odoo/odoo#92962
This commit is contained in:
Gorash
2022-06-17 11:13:43 +02:00
parent 9d5cfe4f34
commit a0a0feabf6
4 changed files with 18 additions and 6 deletions
@@ -80,6 +80,9 @@ publicWidget.registry.websiteSaleCartLink = publicWidget.Widget.extend({
$('.popover').on('mouseleave', function () {
self.$el.trigger('mouseleave');
});
self.cartQty = +$(data).find('.o_wsale_cart_quantity').text();
sessionStorage.setItem('website_sale_cart_quantity', self.cartQty);
self._updateCartQuantityText();
});
}, 300);
},
@@ -129,10 +132,16 @@ publicWidget.registry.websiteSaleCartLink = publicWidget.Widget.extend({
/**
* @private
*/
_updateCartQuantityValue() {
return this._rpc({route: "/shop/cart/quantity"}).then((cartQty) => {
this.cartQty = cartQty;
});
async _updateCartQuantityValue() {
if ('website_sale_cart_quantity' in sessionStorage) {
this.cartQty = sessionStorage.getItem('website_sale_cart_quantity');
}
if (this.el.querySelector('.my_cart_quantity').innerText != this.cartQty) {
return this._rpc({route: "/shop/cart/quantity"}).then((cartQty) => {
this.cartQty = cartQty;
sessionStorage.setItem('website_sale_cart_quantity', this.cartQty);
});
}
},
/**
* @private
@@ -359,6 +368,7 @@ publicWidget.registry.WebsiteSale = publicWidget.Widget.extend(VariantMixin, car
$input.trigger('change');
return;
}
sessionStorage.setItem('website_sale_cart_quantity', data.cart_quantity);
if (!data.cart_quantity) {
return window.location = '/shop/cart';
}
@@ -34,6 +34,7 @@ const cartHandlerMixin = {
route: "/shop/cart/update_json",
params: params,
}).then(async data => {
sessionStorage.setItem('website_sale_cart_quantity', data.cart_quantity);
if (data.cart_quantity && (data.cart_quantity !== parseInt($(".my_cart_quantity").text()))) {
await animateClone($('header .o_wsale_my_cart').first(), this.$itemImgContainer, 25, 40);
updateCartNavBar(data);
+2 -2
View File
@@ -1268,8 +1268,8 @@
</t>
</span>
<a role="button" class="btn btn-primary" href="/shop/cart">
View Cart (<t t-esc="website_sale_order.cart_quantity" /> item(s))
</a>
View Cart (<span class="o_wsale_cart_quantity" t-esc="website_sale_order.cart_quantity" /> item(s))
</a>
</div>
</t>
</template>
@@ -116,6 +116,7 @@ publicWidget.registry.WebsiteSale.include({
const $quantity = $(".my_cart_quantity");
$quantity.parent().parent().removeClass('d-none');
$quantity.text(quantity).hide().fadeIn(600);
sessionStorage.setItem('website_sale_cart_quantity', quantity);
});
});
},