Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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 }));
}
Expand Down