Debian Codegraph @

addons/website_sale/static/src/interactions/cart_line.js

raw symbols SCIP
1 import { Interaction } from '@web/public/interaction';
2 import { browser } from '@web/core/browser/browser';
3 import { registry } from '@web/core/registry';
4 import { rpc } from '@web/core/network/rpc';
5 import { redirect } from '@web/core/utils/urls';
6 import wSaleUtils from '@website_sale/js/website_sale_utils';
7
8 export class CartLine extends Interaction {
9 static selector = '.o_cart_product';
10 dynamicContent = {
11 '.css_quantity > input.js_quantity': {
12 't-on-change.withTarget': this.locked(this.debounced(this.changeQuantity, 500)),
13 },
14 '.css_quantity > a': {
15 't-on-click.prevent.withTarget': this.locked(this.incOrDecQuantity),
16 },
17 '.js_delete_product': { 't-on-click.prevent': this.locked(this.deleteProduct) },
18 };
19
20 /**
21 * @param {Event} ev
22 * @param {HTMLElement} currentTargetEl
23 */
24 async changeQuantity(ev, currentTargetEl) {
25 await this._changeQuantity(currentTargetEl);
26 }
27
28 /**
29 * @param {Event} ev
30 * @param {HTMLElement} currentTargetEl
31 */
32 async incOrDecQuantity(ev, currentTargetEl) {
33 const input = currentTargetEl.closest('.css_quantity').querySelector('input.js_quantity');
34 const maxQuantity = parseFloat(input.dataset.max || Infinity);
35 const oldQuantity = parseFloat(input.value || 0);
36 const newQuantity = currentTargetEl.querySelector('i').classList.contains('oi-minus')
37 ? Math.min(Math.max(oldQuantity - 1, 0), maxQuantity)
38 : Math.min(oldQuantity + 1, maxQuantity);
39 if (oldQuantity !== newQuantity) {
40 input.value = newQuantity;
41 await this._changeQuantity(input);
42 }
43 }
44
45 /**
46 * @param {Event} ev
47 */
48 async deleteProduct(ev) {
49 const input = ev.currentTarget.closest('.o_cart_product')
50 .querySelector('.css_quantity > input.js_quantity');
51 input.value = 0;
52 await this._changeQuantity(input);
53 }
54
55 async _changeQuantity(input) {
56 let quantity = parseInt(input.value || 0);
57 if (isNaN(quantity)) quantity = 1;
58 const lineId = parseInt(input.dataset.lineId);
59 const data = await this.waitFor(rpc('/shop/cart/update', {
60 line_id: lineId,
61 product_id: parseInt(input.dataset.productId),
62 quantity: quantity,
63 }));
64
65 if (!data.cart_quantity) {
66 // Ensure the last cart removal is recorded.
67 browser.sessionStorage.setItem('website_sale_cart_quantity', 0);
68 return redirect('/shop/cart');
69 }
70 input.value = data.quantity;
71 this.el.querySelectorAll(`.js_quantity[data-line-id="${lineId}"]`).forEach(input =>
72 input.value = data.quantity
73 );
74
75 const cart = this.el.closest('#shop_cart');
76 // `updateCartNavBar` regenerates the cart lines and `updateQuickReorderSidebar`
77 // regenerates the quick reorder products, so we need to stop and start interactions
78 // to make sure the regenerated cart lines and reorder products are properly handled.
79 this.services['public.interactions'].stopInteractions(cart);
80 wSaleUtils.updateCartNavBar(data);
81 wSaleUtils.updateQuickReorderSidebar(data);
82 this.services['public.interactions'].startInteractions(cart);
83 wSaleUtils.showWarning(data.warning);
84 // Propagate the change to the express checkout forms.
85 this.env.bus.trigger('cart_amount_changed', [data.amount, data.minor_amount]);
86 }
87 }
88
89 registry
90 .category('public.interactions')
91 .add('website_sale.cart_line', CartLine);