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

Commit

Permalink
feat(store): allow to set and get all search parameters at once
Browse files Browse the repository at this point in the history
This allows to save the state for later and make it easy to observe the store.
  • Loading branch information
rayrutjes committed May 25, 2017
1 parent 2a27712 commit 32cc1a1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/__tests__/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,34 @@ describe('Store', () => {
distinct: null,
attributesToRetrieve: ['objectID'],
};
store.addFacet('price');

// Make sure distinct parameter is gone when overrided with null.
expect(store.queryParameters).not.toHaveProperty('distinct');
});

test('should allow to retrieve all the searchParameters', () => {
const client = algoliaClient('app_id', 'api_key');
const helper = algoliaHelper(client);

const store = new Store(helper);

const searchParameters = helper.getState();
expect(store.searchParameters).toEqual(searchParameters);
});

test('should allow to accept new search parameters', () => {
const client = algoliaClient('app_id', 'api_key');
const helper = algoliaHelper(client);

const store = new Store(helper);

const searchParameters = helper.getState();
const newSearchParameters = Object.assign({}, searchParameters, {
page: 3,
});

store.searchParameters = newSearchParameters;
expect(store.searchParameters).toEqual(newSearchParameters);
});
});
11 changes: 10 additions & 1 deletion src/store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import algolia from 'algoliasearch';
import algoliaHelper from 'algoliasearch-helper';
import algoliaHelper, { SearchParameters } from 'algoliasearch-helper';
import { version } from '../package.json';

export const FACET_AND = 'and';
Expand Down Expand Up @@ -306,6 +306,15 @@ export class Store {
return parameters;
}

get searchParameters() {
return Object.assign({}, this._helper.state);
}

set searchParameters(searchParameters) {
const newSearchParameters = SearchParameters.make(searchParameters);
this._helper.setState(newSearchParameters);
}

// Todo: find a better name for this function.
refresh() {
this._helper.search();
Expand Down

0 comments on commit 32cc1a1

Please sign in to comment.