Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit 32cc1a1

Browse files
committed
feat(store): allow to set and get all search parameters at once
This allows to save the state for later and make it easy to observe the store.
1 parent 2a27712 commit 32cc1a1

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/__tests__/store.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,34 @@ describe('Store', () => {
238238
distinct: null,
239239
attributesToRetrieve: ['objectID'],
240240
};
241+
store.addFacet('price');
241242

242243
// Make sure distinct parameter is gone when overrided with null.
243244
expect(store.queryParameters).not.toHaveProperty('distinct');
244245
});
246+
247+
test('should allow to retrieve all the searchParameters', () => {
248+
const client = algoliaClient('app_id', 'api_key');
249+
const helper = algoliaHelper(client);
250+
251+
const store = new Store(helper);
252+
253+
const searchParameters = helper.getState();
254+
expect(store.searchParameters).toEqual(searchParameters);
255+
});
256+
257+
test('should allow to accept new search parameters', () => {
258+
const client = algoliaClient('app_id', 'api_key');
259+
const helper = algoliaHelper(client);
260+
261+
const store = new Store(helper);
262+
263+
const searchParameters = helper.getState();
264+
const newSearchParameters = Object.assign({}, searchParameters, {
265+
page: 3,
266+
});
267+
268+
store.searchParameters = newSearchParameters;
269+
expect(store.searchParameters).toEqual(newSearchParameters);
270+
});
245271
});

src/store.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import algolia from 'algoliasearch';
2-
import algoliaHelper from 'algoliasearch-helper';
2+
import algoliaHelper, { SearchParameters } from 'algoliasearch-helper';
33
import { version } from '../package.json';
44

55
export const FACET_AND = 'and';
@@ -306,6 +306,15 @@ export class Store {
306306
return parameters;
307307
}
308308

309+
get searchParameters() {
310+
return Object.assign({}, this._helper.state);
311+
}
312+
313+
set searchParameters(searchParameters) {
314+
const newSearchParameters = SearchParameters.make(searchParameters);
315+
this._helper.setState(newSearchParameters);
316+
}
317+
309318
// Todo: find a better name for this function.
310319
refresh() {
311320
this._helper.search();

0 commit comments

Comments
 (0)