Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
588c55f
fix(checkout): CHECKOUT-10026 migrate Google Places script loader to …
bc-maxy Jun 17, 2026
bd75dcc
fix(checkout): CHECKOUT-10026 Migrate GoogleAutocompleteService
bc-maxy Jun 17, 2026
a9876e5
fix(checkout): CHECKOUT-10026 Update GoogleAutocomplete file
bc-maxy Jun 19, 2026
1322a5a
fix(checkout): CHECKOUT-10026 Various quick fixes
bc-maxy Jun 19, 2026
f7af121
fix(checkout): CHECKOUT-10026 Resolve cursor comments
bc-maxy Jun 19, 2026
a5d1b4b
fix(checkout): CHECKOUT-10026 Add session token to suggestion calls
bc-maxy Jun 19, 2026
e02bfce
fix(checkout): CHECKOUT-10026 Various small cleanup
bc-maxy Jun 19, 2026
b93d093
fix(checkout): CHECKOUT-10026 Keep the old service alive
bc-maxy Jun 24, 2026
491717a
chore(checkout): CHECKOUT-10026 Clean up the code
bc-maxy Jun 24, 2026
dd051c8
feat(checkout): CHECKOUT-10026 Fall back to new service if old one fails
bc-maxy Jun 24, 2026
0e95de2
fix(checkout): CHECKOUT-10026 Adjust functions to work correctly
bc-maxy Jun 24, 2026
9389b14
fix(checkout): CHECKOUT-10026 Use module scoped variable
bc-maxy Jun 24, 2026
9672f94
fix(checkout): CHECKOUT-10026 Fix review comments
bc-maxy Jun 29, 2026
9796c11
fix(checkout): CHECKOUT-10026 Clean up the implementation
bc-maxy Jun 29, 2026
acad608
fix(checkout): CHECKOUT-10026 Update test file path
bc-maxy Jun 29, 2026
164211b
fix(checkout): CHECKOUT-10026 Try new service first, fall back to old
bc-maxy Jun 30, 2026
34537f2
fix(checkout): CHECKOUT-10026 Clean up the implementation
bc-maxy Jun 30, 2026
bcc8a31
fix(checkout): CHECKOUT-10026 A small refactor
bc-maxy Jun 30, 2026
974d130
chore(checkout): CHECKOUT-10026 Fix typos and unused exports
bc-maxy Jun 30, 2026
45efb30
fix(checkout): CHECKOUT-10026 Fall back to legacy only for permission…
bc-maxy Jul 1, 2026
9648939
fix(checkout): CHECKOUT-10026 Hide behind feature flag WIP
bc-maxy Jul 1, 2026
08fc93e
Merge branch 'master' into checkout-10026
bc-maxy Jul 1, 2026
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
Expand Up @@ -5,6 +5,7 @@ import { Autocomplete, type AutocompleteItem } from '@bigcommerce/checkout/ui';

import GoogleAutocompleteService from './GoogleAutocompleteService';
import { type GoogleAutocompleteOptionTypes } from './googleAutocompleteTypes';
import { mapToAutocompleteItems, mapToGeocoderAddressComponent } from './utils';
import './GoogleAutocomplete.scss';

export interface GoogleAutocompleteProps {
Expand All @@ -21,17 +22,6 @@ export interface GoogleAutocompleteProps {
onChange?(value: string, isOpen: boolean): void;
}

const toAutocompleteItems = (
results?: google.maps.places.AutocompletePrediction[],
): AutocompleteItem[] => {
return (results || []).map((result) => ({
label: result.description,
value: result.structured_formatting.main_text,
highlightedSlices: result.matched_substrings,
id: result.place_id,
}));
};

const GoogleAutocomplete: React.FC<GoogleAutocompleteProps> = ({
initialValue,
onToggleOpen = noop,
Expand All @@ -53,26 +43,26 @@ const GoogleAutocomplete: React.FC<GoogleAutocompleteProps> = ({
googleAutocompleteServiceRef.current = new GoogleAutocompleteService(apiKey);
}

const onSelectHandler = (item: AutocompleteItem) => {
const handleSelect = (item: AutocompleteItem) => {
const service = googleAutocompleteServiceRef.current;

if (!service) return;

service.getPlacesServices().then((placesService) => {
placesService.getDetails(
{
placeId: item.id,
fields: fields || ['address_components', 'name'],
},
(result) => {
if (nextElement) {
nextElement.focus();
}

onSelect(result, item);
},
);
});
service
.getPlaceDetails(item.id, fields || ['addressComponents', 'displayName'])
.then((place) => {
const placeResult: google.maps.places.PlaceResult = {
address_components: place.addressComponents?.map(mapToGeocoderAddressComponent),
name: place.displayName ?? '',
};

if (nextElement) {
nextElement.focus();
}

onSelect(placeResult, item);
})
.catch(() => resetAutocomplete());
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
};

const resetAutocomplete = (): void => {
Expand All @@ -95,23 +85,13 @@ const GoogleAutocomplete: React.FC<GoogleAutocompleteProps> = ({

if (!service) return;

service.getAutocompleteService().then((autocompleteService) => {
autocompleteService.getPlacePredictions(
{
input,
types: types || ['geocode'],
componentRestrictions,
},
(results) => {
const autocompleteItems = toAutocompleteItems(results ?? undefined);

setItems(autocompleteItems);
},
);
});
service
.getSuggestions(input, types || ['geocode'], componentRestrictions)
.then((suggestions) => setItems(mapToAutocompleteItems(suggestions)))
.catch(() => setItems([]));
};

const onChangeHandler = (input: string) => {
const handleChange = (input: string) => {
onChange(input, false);

if (!isAutocompleteEnabled) {
Expand All @@ -133,8 +113,8 @@ const GoogleAutocomplete: React.FC<GoogleAutocompleteProps> = ({
}}
items={items}
listTestId="address-autocomplete-suggestions"
onChange={onChangeHandler}
onSelect={onSelectHandler}
onChange={handleChange}
onSelect={handleSelect}
onToggleOpen={onToggleOpen}
>
<div className="co-googleAutocomplete-footer" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,26 @@
import { getScriptLoader, type ScriptLoader } from '@bigcommerce/script-loader';

import { type GoogleAutocompleteWindow, type GoogleMapsSdk } from './googleAutocompleteTypes';
export class GoogleAutocompleteScriptLoader {
private _scriptLoader: ScriptLoader = getScriptLoader();
private _placesPromise?: Promise<google.maps.PlacesLibrary>;

export default class GoogleAutocompleteScriptLoader {
private _scriptLoader: ScriptLoader;
private _googleAutoComplete?: Promise<GoogleMapsSdk>;

constructor() {
this._scriptLoader = getScriptLoader();
}

loadMapsSdk(apiKey: string): Promise<GoogleMapsSdk> {
if (this._googleAutoComplete) {
return this._googleAutoComplete;
loadPlacesLibrary(apiKey: string): Promise<google.maps.PlacesLibrary> {
if (this._placesPromise) {
return this._placesPromise;
}

this._googleAutoComplete = new Promise((resolve, reject) => {
const callbackName = 'initAutoComplete';
const params = [
'language=en',
`key=${apiKey}`,
'libraries=places',
`callback=${callbackName}`,
].join('&');
const params = ['language=en', `key=${apiKey}`, 'loading=async'].join('&');

(window as GoogleCallbackWindow)[callbackName] = () => {
if (isAutocompleteWindow(window)) {
resolve(window.google.maps);
}
const promise = this._scriptLoader
.loadScript(`//maps.googleapis.com/maps/api/js?${params}`)
.then(() => google.maps.importLibrary('places') as Promise<google.maps.PlacesLibrary>)
.catch((e) => {
this._placesPromise = undefined;
throw e;
});

reject(new Error('Failed to initialize Google Maps Autocomplete SDK.'));
};
this._placesPromise = promise;

this._scriptLoader
.loadScript(`//maps.googleapis.com/maps/api/js?${params}`)
.catch((e) => {
this._googleAutoComplete = undefined;
throw e;
});
});

return this._googleAutoComplete;
return promise;
}
}

function isAutocompleteWindow(window: Window): window is GoogleAutocompleteWindow {
const autocompleteWindow = window as GoogleAutocompleteWindow;

return Boolean(
autocompleteWindow.google &&
autocompleteWindow.google.maps &&
autocompleteWindow.google.maps.places,
);
}

export interface GoogleCallbackWindow extends Window {
initAutoComplete?(): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { type GoogleAutocompleteScriptLoader } from './GoogleAutocompleteScriptLoader';
import GoogleAutocompleteService from './GoogleAutocompleteService';

const mockSuggestions = [
{ placePrediction: { placeId: 'place-1' } },
] as google.maps.places.AutocompleteSuggestion[];

const mockPlace = {
fetchFields: jest.fn().mockResolvedValue(undefined),
addressComponents: [],
displayName: '123 Main St',
} as google.maps.places.Place;

const mockPlacesLibrary = {
AutocompleteSuggestion: {
fetchAutocompleteSuggestions: jest.fn().mockResolvedValue({ suggestions: mockSuggestions }),
},
Place: jest.fn().mockImplementation(() => mockPlace),
} as google.maps.PlacesLibrary;

const mockScriptLoader = {
loadPlacesLibrary: jest.fn().mockResolvedValue(mockPlacesLibrary),
} as unknown as GoogleAutocompleteScriptLoader;

describe('GoogleAutocompleteService', () => {
let service: GoogleAutocompleteService;

beforeEach(() => {
jest.clearAllMocks();
service = new GoogleAutocompleteService('test-api-key', mockScriptLoader);
});

describe('#getSuggestions()', () => {
it('returns suggestions from the API', async () => {
const result = await service.getSuggestions('123 Main', ['address']);

expect(result).toBe(mockSuggestions);
});

it('passes input and types to fetchAutocompleteSuggestions', async () => {
await service.getSuggestions('123 Main', ['address']);

expect(
mockPlacesLibrary.AutocompleteSuggestion.fetchAutocompleteSuggestions,
).toHaveBeenCalledWith(
expect.objectContaining({ input: '123 Main', includedPrimaryTypes: ['address'] }),
);
});

it('maps a string country restriction to includedRegionCodes array', async () => {
await service.getSuggestions('123 Main', ['address'], { country: 'US' });

expect(
mockPlacesLibrary.AutocompleteSuggestion.fetchAutocompleteSuggestions,
).toHaveBeenCalledWith(expect.objectContaining({ includedRegionCodes: ['US'] }));
});

it('maps an array country restriction to includedRegionCodes', async () => {
await service.getSuggestions('123 Main', ['address'], { country: ['US', 'CA'] });

expect(
mockPlacesLibrary.AutocompleteSuggestion.fetchAutocompleteSuggestions,
).toHaveBeenCalledWith(expect.objectContaining({ includedRegionCodes: ['US', 'CA'] }));
});

it('omits includedRegionCodes when no componentRestrictions provided', async () => {
await service.getSuggestions('123 Main', ['address']);

expect(
mockPlacesLibrary.AutocompleteSuggestion.fetchAutocompleteSuggestions,
).toHaveBeenCalledWith(expect.objectContaining({ includedRegionCodes: undefined }));
});
});

describe('#getPlaceDetails()', () => {
it('constructs a Place with the given placeId', async () => {
await service.getPlaceDetails('place-1', ['addressComponents', 'displayName']);

expect(mockPlacesLibrary.Place).toHaveBeenCalledWith({ id: 'place-1' });
});

it('calls fetchFields with the given fields', async () => {
await service.getPlaceDetails('place-1', ['addressComponents', 'displayName']);

expect(mockPlace.fetchFields).toHaveBeenCalledWith({
fields: ['addressComponents', 'displayName'],
});
});

it('returns the place after fetching fields', async () => {
const result = await service.getPlaceDetails('place-1', ['addressComponents']);

expect(result).toBe(mockPlace);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
import getGoogleAutocompleteScriptLoader from './getGoogleAutocompleteScriptLoader';
import type GoogleAutocompleteScriptLoader from './GoogleAutocompleteScriptLoader';
import { type GoogleAutocompleteScriptLoader } from './GoogleAutocompleteScriptLoader';

export default class GoogleAutocompleteService {
private _autocompletePromise?: Promise<google.maps.places.AutocompleteService>;
private _placesPromise?: Promise<google.maps.places.PlacesService>;

constructor(
private _apiKey: string,
private _scriptLoader: GoogleAutocompleteScriptLoader = getGoogleAutocompleteScriptLoader(),
) {}

getAutocompleteService(): Promise<google.maps.places.AutocompleteService> {
if (!this._autocompletePromise) {
this._autocompletePromise = this._scriptLoader
.loadMapsSdk(this._apiKey)
.then((googleMapsSdk) => {
if (!googleMapsSdk.places.AutocompleteService) {
throw new Error('`AutocompleteService` is undefined');
}

return new googleMapsSdk.places.AutocompleteService();
});
}
private convertToRegionCodes(
country: string | string[] | null | undefined,
): string[] | undefined {
if (!country) return undefined;

return this._autocompletePromise;
return Array.isArray(country) ? country : [country];
}

getPlacesServices(): Promise<google.maps.places.PlacesService> {
const node = document.createElement('div');
async getSuggestions(
input: string,
types: string[],
componentRestrictions?: google.maps.places.ComponentRestrictions,
): Promise<google.maps.places.AutocompleteSuggestion[]> {
const { AutocompleteSuggestion } = await this._scriptLoader.loadPlacesLibrary(this._apiKey);
const includedRegionCodes = this.convertToRegionCodes(componentRestrictions?.country);

const { suggestions } = await AutocompleteSuggestion.fetchAutocompleteSuggestions({
input,
includedPrimaryTypes: types,
includedRegionCodes,
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
});

return suggestions;
}

if (!this._placesPromise) {
this._placesPromise = this._scriptLoader
.loadMapsSdk(this._apiKey)
.then((googleMapsSdk) => {
if (!googleMapsSdk.places.PlacesService) {
throw new Error('`PlacesService` is undefined');
}
async getPlaceDetails(placeId: string, fields: string[]): Promise<google.maps.places.Place> {
const { Place } = await this._scriptLoader.loadPlacesLibrary(this._apiKey);
const place = new Place({ id: placeId });

return new googleMapsSdk.places.PlacesService(node);
});
}
await place.fetchFields({ fields });

return this._placesPromise;
return place;
}
}
Loading