|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import { action } from '@ember/object'; |
| 3 | +import { isTesting } from '@embroider/macros'; |
| 4 | +import { getOwner } from '@ember/application'; |
| 5 | + |
| 6 | +import { Loader } from '@googlemaps/js-api-loader'; |
| 7 | + |
| 8 | +interface UtilsAddressInlineArgs { |
| 9 | + value: ShippingAddress; |
| 10 | + useGoogleAutocomplete?: boolean; |
| 11 | + onChange(address: any): void; |
| 12 | +} |
| 13 | + |
| 14 | +export type ShippingAddress = { |
| 15 | + address: string; |
| 16 | + resolved_address: { |
| 17 | + line_1: string; |
| 18 | + zipcode: string; |
| 19 | + city: string; |
| 20 | + state?: string; |
| 21 | + country_code: string; |
| 22 | + } | null; |
| 23 | +}; |
| 24 | + |
| 25 | +type GAddressComponent = google.maps.GeocoderAddressComponent; |
| 26 | +type GAutoComplete = google.maps.places.Autocomplete; |
| 27 | +type GPlaceResult = google.maps.places.PlaceResult; |
| 28 | + |
| 29 | +export default class extends Component<UtilsAddressInlineArgs> { |
| 30 | + get useGoogleAutocomplete(): boolean { |
| 31 | + return this.args.useGoogleAutocomplete ?? true; |
| 32 | + } |
| 33 | + |
| 34 | + @action |
| 35 | + initAutoCompletion(): void { |
| 36 | + if (isTesting()) return; |
| 37 | + this.appendContainerLocally(); |
| 38 | + const loader = new Loader({ |
| 39 | + apiKey: getOwner(this).resolveRegistration('config:environment').google_map_api_key, |
| 40 | + version: 'weekly' |
| 41 | + }); |
| 42 | + |
| 43 | + loader.importLibrary('places').then(({ Autocomplete }) => { |
| 44 | + const input = document.querySelector('[data-control-name="address-inline"] input') as HTMLInputElement; |
| 45 | + const options = { |
| 46 | + fields: ['address_components'], |
| 47 | + strictBounds: false, |
| 48 | + types: ['address'] |
| 49 | + }; |
| 50 | + const autocomplete = new Autocomplete(input, options); |
| 51 | + this.initInputListeners(autocomplete, input); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + @action |
| 56 | + onChange(value: string): void { |
| 57 | + this.args.onChange({ address: value, resolved_address: null }); |
| 58 | + } |
| 59 | + |
| 60 | + private appendContainerLocally(): void { |
| 61 | + const observer = new MutationObserver((mutationList: any) => { |
| 62 | + for (const mutation of mutationList) { |
| 63 | + if (mutation.type === 'childList') { |
| 64 | + const pacContainer = mutation.addedNodes[0]; |
| 65 | + if (!pacContainer?.classList.contains('pac-container')) return; |
| 66 | + |
| 67 | + document.querySelector('[data-control-name="address-inline"]')?.append(pacContainer); |
| 68 | + observer.disconnect(); |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + observer.observe(document.body, { childList: true }); |
| 73 | + } |
| 74 | + |
| 75 | + private initInputListeners(autocomplete: GAutoComplete, input: HTMLInputElement): void { |
| 76 | + autocomplete.addListener('place_changed', () => { |
| 77 | + const place = autocomplete.getPlace(); |
| 78 | + this.updateAddress(place, input); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + private updateAddress(place: GPlaceResult, input: HTMLInputElement): void { |
| 83 | + let address1: string = ''; |
| 84 | + let zipcode: string = ''; |
| 85 | + let city: string = ''; |
| 86 | + let state: string = ''; |
| 87 | + let country: string = ''; |
| 88 | + |
| 89 | + const mapper: { [key: string]: (comp: GAddressComponent) => void } = { |
| 90 | + street_number: (comp) => { |
| 91 | + address1 = `${comp.long_name} ${address1}`; |
| 92 | + }, |
| 93 | + route: (comp) => { |
| 94 | + address1 += comp.long_name; |
| 95 | + }, |
| 96 | + postal_code: (comp) => { |
| 97 | + zipcode = `${comp.long_name}${zipcode}`; |
| 98 | + }, |
| 99 | + postal_code_suffix: (comp) => { |
| 100 | + zipcode = `${zipcode}-${comp.long_name}`; |
| 101 | + }, |
| 102 | + locality: (comp) => { |
| 103 | + city = comp.long_name; |
| 104 | + }, |
| 105 | + postal_town: (comp) => { |
| 106 | + city = comp.long_name; |
| 107 | + }, |
| 108 | + administrative_area_level_1: (comp) => { |
| 109 | + state = comp.long_name ?? ''; |
| 110 | + }, |
| 111 | + country: (comp) => { |
| 112 | + country = comp.short_name; |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + place.address_components!.reverse().map((component) => { |
| 117 | + const componentType: string = component.types[0]; |
| 118 | + |
| 119 | + mapper[componentType]?.(component); |
| 120 | + }); |
| 121 | + |
| 122 | + this.args.onChange({ |
| 123 | + address: input.value, |
| 124 | + resolved_address: { |
| 125 | + line_1: address1, |
| 126 | + zipcode, |
| 127 | + city, |
| 128 | + state, |
| 129 | + country_code: country |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | +} |
0 commit comments