Skip to content

Commit

Permalink
Merge pull request #14 from afosto/Feature/query-session-id
Browse files Browse the repository at this point in the history
Feature/query-session-id
  • Loading branch information
Rapid0o authored Apr 19, 2022
2 parents 4b30a2d + b497c3b commit a607362
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/adapters/SearchRequestAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const SearchRequestAdapter = () => {
q: query,
threshold: options.threshold || DEFAULT_OPTIONS.threshold,
session_id: request.session_id,
user_id: request.user_id,
__queryID: request.__queryID,
...pagination,
}];
Expand Down
7 changes: 7 additions & 0 deletions src/adapters/SearchResponseAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ const SearchResponseAdapter = () => {
page,
query,
queryID,
renderingContent: {
facetOrdering: {
facets: {
order: Object.keys(facets),
},
}
}
};
};

Expand Down
35 changes: 31 additions & 4 deletions src/afostoInstantSearch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SearchResponseAdapter, SearchRequestAdapter } from './adapters';
import { DEFAULT_OPTIONS } from './constants';
import getSessionID from './utils/getSessionID';
import getUserID from './utils/getUserID';
import uuid from './utils/uuid';

/**
Expand All @@ -19,7 +19,11 @@ const afostoInstantSearch = (searchEngineKey, options = {}) => {
const url = clientOptions.baseUrl?.replace('{key}', searchEngineKey);
const searchRequestAdapter = SearchRequestAdapter();
const searchResponseAdapter = SearchResponseAdapter();
const sessionID = getSessionID();
const userID = getUserID();
const searchQueryState = {
querySessionID: null,
query: null,
};

const getSettings = async () => {
try {
Expand All @@ -30,12 +34,28 @@ const afostoInstantSearch = (searchEngineKey, options = {}) => {
});
const response = await settingsResponse.json();

return response.data;
return response.data || {};
} catch (error) {
return {};
}
};

const setSearchQueryState = requests => {
const { allowEmptyQuery = true } = clientOptions;
const [firstRequest] = requests || [];
const { query } = firstRequest?.params || {};
const lastQuery = searchQueryState.query;
const querySessionID = searchQueryState.querySessionID;
const isNewQuery = !querySessionID || (allowEmptyQuery && lastQuery && !query) ||
(!allowEmptyQuery && query?.length === 1 && query?.charAt(0) !== lastQuery?.charAt(0));

if (isNewQuery) {
searchQueryState.querySessionID = uuid();
}

searchQueryState.query = query;
};

const searchRequest = async contexts => {
const requestOptions = clientOptions.requestOptions || {};
const hasContextFormatter = clientOptions.transformContext && typeof clientOptions.transformContext === 'function';
Expand All @@ -58,7 +78,14 @@ const afostoInstantSearch = (searchEngineKey, options = {}) => {

const search = async requests => {
try {
const searchRequests = requests.map(request => ({ ...request, session_id: sessionID, __queryID: uuid() }));
setSearchQueryState(requests);

const searchRequests = requests.map(request => ({
...request,
session_id: searchQueryState.querySessionID,
user_id: userID,
__queryID: uuid()
}));
const searchContexts = searchRequestAdapter.transform(searchRequests, clientOptions);
const [searchRequestContext] = searchContexts;

Expand Down
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ export const OPERATOR_CONVERSION = {
'=': 'in',
};

export const SESSION_KEY = 'afostoInstantSearch_sessionID';
export const USER_SESSION_KEY = 'afostoInstantSearch_userSessionID';
22 changes: 0 additions & 22 deletions src/utils/getSessionID.js

This file was deleted.

22 changes: 22 additions & 0 deletions src/utils/getUserID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { USER_SESSION_KEY } from '../constants';
import uuid from '../utils/uuid';

const getUserID = () => {
const isBrowser = typeof window !== 'undefined';
const hasSessionStorage = isBrowser && !!window.sessionStorage;

if (!hasSessionStorage) {
return uuid();
}

let userSessionID = window.sessionStorage.getItem(USER_SESSION_KEY);

if (!userSessionID) {
userSessionID = uuid();
window.sessionStorage.setItem(USER_SESSION_KEY, userSessionID);
}

return userSessionID;
};

export default getUserID;

0 comments on commit a607362

Please sign in to comment.