diff --git a/addons/website_sale_autocomplete/static/src/interactions/address_form.js b/addons/website_sale_autocomplete/static/src/interactions/address_form.js index 8cd5fa48c1793..b1e0f7335e778 100644 --- a/addons/website_sale_autocomplete/static/src/interactions/address_form.js +++ b/addons/website_sale_autocomplete/static/src/interactions/address_form.js @@ -1,11 +1,11 @@ import { Interaction } from "@web/public/interaction"; import { registry } from "@web/core/registry"; -import { rpc } from "@web/core/network/rpc"; import { KeepLast } from "@web/core/utils/concurrency"; import { renderToElement } from "@web/core/utils/render"; +import { googlePlacesSession } from "@google_address_autocomplete/google_places_session"; -class AddressForm extends Interaction { +export class AddressForm extends Interaction { static selector = ".oe_cart .checkout_autoformat"; static selectorHas = "input[name='street'][data-autocomplete-enabled='1']"; dynamicContent = { @@ -20,31 +20,19 @@ class AddressForm extends Interaction { this.countrySelect = this.el.querySelector("select[name='country_id']"); this.stateSelect = this.el.querySelector("select[name='state_id']"); this.keepLast = new KeepLast(); - this.sessionId = this.generateUUID(); } - generateUUID() { - return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) { - const r = (Math.random() * 16) | 0, v = c == "x" ? r : (r & 0x3) | 0x8; - return v.toString(16); - }); - } - - async onInputStreet(ev, inputEl) { + onInputStreet(ev, inputEl) { const inputContainerEl = inputEl.parentNode; if (inputEl.value.length >= 5) { this.keepLast.add( - rpc("/autocomplete/address", { + googlePlacesSession.getAddressPropositions({ partial_address: inputEl.value, - session_id: this.sessionId || null, }).then((response) => { inputContainerEl.querySelector(".dropdown-menu")?.remove(); inputContainerEl.appendChild(renderToElement("website_sale_autocomplete.AutocompleteDropDown", { results: response.results, })); - if (response.session_id) { - this.sessionId = response.session_id; - } }) ); } else { @@ -61,10 +49,9 @@ class AddressForm extends Interaction { spinnerEl.classList.add("spinner-border", "text-warning", "text-center", "m-auto"); dropdownEl.appendChild(spinnerEl); - const address = await this.waitFor(rpc("/autocomplete/address_full", { + const address = await this.waitFor(googlePlacesSession.getAddressDetails({ address: ev.currentTarget.innerText, google_place_id: ev.currentTarget.dataset.googlePlaceId, - session_id: this.sessionId || null, })); if (address.formatted_street_number) { @@ -76,7 +63,7 @@ class AddressForm extends Interaction { // Selects based on odoo ids if (address.country) { - this.countrySelect.value = address.country; + this.countrySelect.value = address.country[0]; // Let the state select know that the country has changed so that it may fetch the correct states or disappear. this.countrySelect.dispatchEvent(new Event("change", { bubbles: true })); }