Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

MAPS-748 autocomplete sessions into javascript sdk #141

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Radar.initialize('prj_test_pk_...', { /* options */ });

Add the following script in your `html` file
```html
<script src="https://js.radar.com/v4.1.12/radar.min.js"></script>
<script src="https://js.radar.com/v4.1.12-beta.0/radar.min.js"></script>
```

Then initialize the Radar SDK
Expand All @@ -73,8 +73,8 @@ To create a map, first initialize the Radar SDK with your publishable key. Then
```html
<html>
<head>
<link href="https://js.radar.com/v4.1.12/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.12/radar.min.js"></script>
<link href="https://js.radar.com/v4.1.12-beta.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.12-beta.0/radar.min.js"></script>
</head>

<body>
Expand All @@ -98,8 +98,8 @@ To create an autocomplete input, first initialize the Radar SDK with your publis
```html
<html>
<head>
<link href="https://js.radar.com/v4.1.12/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.12/radar.min.js"></script>
<link href="https://js.radar.com/v4.1.12-beta.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.12-beta.0/radar.min.js"></script>
</head>

<body>
Expand Down Expand Up @@ -130,8 +130,8 @@ To power [geofencing](https://radar.com/documentation/geofencing/overview) exper
```html
<html>
<head>
<link href="https://js.radar.com/v4.1.12/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.12/radar.min.js"></script>
<link href="https://js.radar.com/v4.1.12-beta.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.12-beta.0/radar.min.js"></script>
</head>

<body>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radar-sdk-js",
"version": "4.1.12",
"version": "4.1.12-beta.0",
"description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.",
"homepage": "https://radar.com",
"type": "module",
Expand Down
13 changes: 13 additions & 0 deletions src/api/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
RadarSearchPlacesResponse,
RadarSearchGeofencesParams,
RadarSearchGeofencesResponse,
RadarAutocompleteSessionSelectionParams,
} from '../types';

class SearchAPI {
Expand All @@ -22,6 +23,7 @@ class SearchAPI {
layers,
countryCode,
expandUnits,
session,
} = params;

// near can be provided as a string or Location object
Expand All @@ -42,6 +44,7 @@ class SearchAPI {
layers,
countryCode,
expandUnits,
session,
},
});

Expand All @@ -56,6 +59,16 @@ class SearchAPI {
return autocompleteRes;
}

static async autocompleteSelect(params: RadarAutocompleteSessionSelectionParams) {
await Http.request({
method: 'GET',
path: 'search/autocomplete/selection',
data: {
...params
},
});
}

static async searchGeofences(params: RadarSearchGeofencesParams): Promise<RadarSearchGeofencesResponse> {
const options = Config.get();

Expand Down
17 changes: 17 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,23 @@ export interface RadarAutocompleteParams {
layers?: RadarGeocodeLayer[];
countryCode?: string;
expandUnits?: boolean;
session?: string;
}

export interface RadarAutocompleteSessionSelectionParams {
session: string;
selection: number;
radarSessionId?: string;
userId?: string;
deviceId?: string;
installId?: string;

query?: string;
near?: Location | string;
limit?: number;
layers?: RadarGeocodeLayer[];
countryCode?: string;
expandUnits?: boolean;
}

export interface RadarAutocompleteResponse extends RadarResponse {
Expand Down
49 changes: 48 additions & 1 deletion src/ui/autocomplete.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import Logger from '../logger';
import SearchAPI from '../api/search';
import Device from '../device';
import Session from '../session';
import Storage from '../storage';

import { RadarAutocompleteContainerNotFound } from '../errors';
import type { RadarAutocompleteUIOptions, RadarAutocompleteConfig, RadarAutocompleteParams, Location } from '../types';
import type { RadarAutocompleteUIOptions, RadarAutocompleteConfig, RadarAutocompleteParams, Location, RadarAutocompleteSessionSelectionParams } from '../types';

import crypto from 'crypto';

const CLASSNAMES = {
WRAPPER: 'radar-autocomplete-wrapper',
Expand Down Expand Up @@ -86,6 +91,8 @@
resultsList: HTMLElement;
wrapper: HTMLElement;
poweredByLink?: HTMLElement;
session?: string;
autocompleteParams?: RadarAutocompleteParams;

// create a new AutocompleteUI instance
public static createAutocomplete(autocompleteOptions: RadarAutocompleteUIOptions): AutocompleteUI {
Expand Down Expand Up @@ -238,12 +245,18 @@
public async fetchResults(query: string) {
const { limit, layers, countryCode, expandUnits, onRequest } = this.config;

if (!this.session) {
this.session = this.generateUUID();
}
const session = this.session;

const params: RadarAutocompleteParams = {
query,
limit,
layers,
countryCode,
expandUnits,
session,
}

if (this.near) {
Expand All @@ -254,6 +267,8 @@
onRequest(params);
}

this.autocompleteParams = params; // set previous params for autocomplete session selections

const { addresses } = await SearchAPI.autocomplete(params);
return addresses;
}
Expand Down Expand Up @@ -442,6 +457,28 @@
onSelection(result);
}

// Radar Autocomplete Session
if (this.session) {
const userId = Storage.getItem(Storage.USER_ID) || undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now I'm still including these even though right now we don't use them, but in case we wanted to store them in the future so we don't have to update the sdk twice

const deviceId = Device.getDeviceId() || undefined;
const installId = Device.getInstallId() || undefined;
const radarSessionId = Session.getSessionId() || undefined;
const params: RadarAutocompleteSessionSelectionParams = {
session: this.session,
selection: index,
userId,
deviceId,
installId,
radarSessionId,
...this.autocompleteParams
}
SearchAPI.autocompleteSelect(params);
}

// Close out session
this.session = undefined;
this.autocompleteParams = undefined;

// clear results list
this.close();
}
Expand Down Expand Up @@ -511,6 +548,16 @@
return this;
}

public generateUUID = (): string => {
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (char) => {
const r = Math.random() * 16 | 0;
const v = (char == 'x') ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
Dismissed Show dismissed Hide dismissed

return uuid;
}

public setShowMarkers(showMarkers: boolean) {
this.config.showMarkers = showMarkers;
if (showMarkers) {
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '4.1.12';
export default '4.1.12-beta.0';
Loading