Debian Codegraph @

addons/website_sale_collect/static/src/interactions/checkout.js

raw symbols SCIP
1 import { _t } from '@web/core/l10n/translation';
2 import { patch } from '@web/core/utils/patch';
3 import { patchDynamicContent } from '@web/public/utils';
4 import { rpc } from '@web/core/network/rpc';
5 import { Checkout } from '@website_sale/interactions/checkout';
6
7 patch(Checkout.prototype, {
8 setup() {
9 super.setup();
10 patchDynamicContent(this.dynamicContent, {
11 '.js_wsc_update_product_qty': { 't-on-click': this.onClickUpdateProductQty.bind(this) },
12 });
13 this.deliveryAddressTitle = this.el.querySelector('[name="delivery_address_title"]');
14 if (this.deliveryAddressTitle) {
15 this.deliveryTitle = this.deliveryAddressTitle.textContent;
16 this.useDeliveryAsBillingLabel = this.el.querySelector(
17 '[name="use_delivery_as_billing_text"]'
18 );
19 this.useDeliveryAsBillingLabelText = this.useDeliveryAsBillingLabel.textContent;
20 this.inStoreTitle = _t('Contact Details');
21 this.useDeliveryAsBillingLabelInStoreText = _t('Same as contact details');
22 this._adaptDeliveryTitles();
23 }
24 },
25
26 async selectDeliveryMethod(ev) {
27 this._adaptDeliveryTitles();
28 await super.selectDeliveryMethod(...arguments);
29 },
30
31 /**
32 * Remove a product from the cart or update its quantity to match the available quantity.
33 *
34 * @param {Event} ev
35 */
36 async onClickUpdateProductQty(ev) {
37 await this.waitFor(rpc('/shop/cart/update', {
38 line_id: parseInt(ev.currentTarget.dataset.lineId, 10),
39 product_id: parseInt(ev.currentTarget.dataset.productId, 10),
40 quantity: parseInt(ev.currentTarget.dataset.availableQty || 0, 10),
41 }));
42 window.location.reload(); // Reload all cart values.
43 },
44
45 // #=== DOM MANIPULATION ===#
46
47 /**
48 * Change the delivery address title and the 'use delivery as billing' label depending on the
49 * selected delivery method.
50 *
51 * @private
52 * @return {void}
53 */
54 _adaptDeliveryTitles() {
55 const checkedRadio = document.querySelector('input[name="o_delivery_radio"]:checked');
56 if (!checkedRadio || !this.deliveryAddressTitle || !this.useDeliveryAsBillingLabel) {
57 return;
58 }
59 if (checkedRadio.dataset.deliveryType === 'in_store') {
60 this.deliveryAddressTitle.textContent = this.inStoreTitle;
61 this.useDeliveryAsBillingLabel.textContent = this.useDeliveryAsBillingLabelInStoreText;
62 } else {
63 this.deliveryAddressTitle.textContent = this.deliveryTitle;
64 this.useDeliveryAsBillingLabel.textContent = this.useDeliveryAsBillingLabelText;
65 }
66 },
67
68 /**
69 * Remove a warning if available pickup location is selected.
70 *
71 * @override method from `@website_sale/interactions/checkout`
72 */
73 _updatePickupLocation(button, location, jsonLocation) {
74 super._updatePickupLocation(...arguments);
75 const dmContainer = this._getDeliveryMethodContainer(button);
76 const radio = dmContainer.querySelector('[name="o_delivery_radio"]');
77 if (radio.dataset.deliveryType === 'in_store') {
78 dmContainer.querySelector('[name="unavailable_products_warning"]')?.remove();
79 }
80 },
81
82 /**
83 * Return false if there is a warning message, otherwise return the result of the parent method
84 * call.
85 *
86 * @override method from `@website_sale/interactions/checkout`
87 */
88 _isDeliveryMethodReady() {
89 if (this.dmRadios.length === 0) { // If there are no delivery methods.
90 return super._isDeliveryMethodReady(...arguments); // Skip override.
91 }
92 const checkedRadio = this.el.querySelector('input[name="o_delivery_radio"]:checked');
93 let hasWarning = false;
94 if (checkedRadio) {
95 const deliveryContainer = this._getDeliveryMethodContainer(checkedRadio);
96 hasWarning = (
97 checkedRadio.dataset.deliveryType === 'in_store'
98 && deliveryContainer.querySelector('[name="unavailable_products_warning"]')
99 );
100 }
101 return super._isDeliveryMethodReady(...arguments) && !hasWarning;
102 },
103
104 /**
105 * Also hide the warning message, if any.
106 *
107 * @override method from `@website_sale/interactions/checkout`
108 */
109 _hidePickupLocation() {
110 super._hidePickupLocation(...arguments);
111 const warning = this.el.querySelector('[name="unavailable_products_warning"]');
112 if (warning) {
113 warning.classList.add('d-none');
114 }
115 },
116
117 // #=== DELIVERY FLOW ===#
118
119 /**
120 * Display a warning if any when selecting an in_store delivery method.
121 *
122 * @override method from `@website_sale/interactions/checkout`
123 */
124 async _showPickupLocation(radio) {
125 super._showPickupLocation(...arguments);
126 if (radio.dataset.deliveryType === 'in_store') {
127 const dmContainer = this._getDeliveryMethodContainer(radio);
128 const warning = dmContainer.querySelector('[name="unavailable_products_warning"]');
129 if (warning) {
130 warning.classList.remove('d-none');
131 }
132 }
133 },
134 });