Skip to content

Commit

Permalink
feat(pagination): setPage() -> refine() / currentPage -> `curre…
Browse files Browse the repository at this point in the history
…ntRefinement`
  • Loading branch information
iam4x committed Mar 27, 2017
1 parent 998faf1 commit f783fea
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/connectors/pagination/__tests__/connectPagination-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('connectPagination', () => {

// should provide good values for the first rendering
const firstRenderingOptions = rendering.lastCall.args[0];
expect(firstRenderingOptions.currentPage).toBe(0);
expect(firstRenderingOptions.currentRefinement).toBe(0);
expect(firstRenderingOptions.nbHits).toBe(0);
expect(firstRenderingOptions.nbPages).toBe(0);
expect(firstRenderingOptions.widgetParams).toEqual({
Expand All @@ -64,7 +64,7 @@ describe('connectPagination', () => {

// should call the rendering with values from the results
const secondRenderingOptions = rendering.lastCall.args[0];
expect(secondRenderingOptions.currentPage).toBe(0);
expect(secondRenderingOptions.currentRefinement).toBe(0);
expect(secondRenderingOptions.nbHits).toBe(1);
expect(secondRenderingOptions.nbPages).toBe(1);
}
Expand All @@ -88,8 +88,8 @@ describe('connectPagination', () => {

{ // first rendering
const renderOptions = rendering.lastCall.args[0];
const {setPage} = renderOptions;
setPage(2);
const {refine} = renderOptions;
refine(2);
expect(helper.getPage()).toBe(2);
expect(helper.search.callCount).toBe(1);
}
Expand All @@ -103,8 +103,8 @@ describe('connectPagination', () => {

{ // Second rendering
const renderOptions = rendering.lastCall.args[0];
const {setPage} = renderOptions;
setPage(7);
const {refine} = renderOptions;
refine(7);
expect(helper.getPage()).toBe(7);
expect(helper.search.callCount).toBe(2);
}
Expand Down
20 changes: 10 additions & 10 deletions src/connectors/pagination/connectPagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const usage = `Usage:
var customPagination = connectPagination(function render(params, isFirstRendering) {
// params = {
// createURL,
// currentPage,
// currentRefinement,
// nbHits,
// nbPages,
// setPage,
// refine,
// widgetParams,
// }
});
Expand All @@ -27,10 +27,10 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
/**
* @typedef PaginationRenderingOptions
* @property {function(number)} createURL create URL's for the next state, the number is the page to generate the URL for
* @property {number} currentPage the number of the page currently displayed
* @property {number} currentRefinement the number of the page currently displayed
* @property {number} nbHits the number of hits computed for the last query (can be approximated)
* @property {number} nbPages the number of pages for the result set
* @property {function} setPage set the current page and trigger a search
* @property {function} refine set the current page and trigger a search
* @property {Object} widgetParams all original options forwarded to rendering
* @property {InstantSearch} instantSearchInstance the instance of instantsearch on which the widget is attached
*/
Expand All @@ -48,19 +48,19 @@ export default function connectPagination(renderFn) {

return {
init({helper, createURL, instantSearchInstance}) {
this.setPage = page => {
this.refine = page => {
helper.setPage(page);
helper.search();
};

this.createURL = state => page => createURL(state.setPage(page));
this.createURL = state => page => createURL(state.refine(page));

renderFn({
createURL: this.createURL(helper.state),
currentPage: helper.getPage() || 0,
currentRefinement: helper.getPage() || 0,
nbHits: 0,
nbPages: 0,
setPage: this.setPage,
refine: this.refine,
widgetParams,
instantSearchInstance,
}, true);
Expand All @@ -75,8 +75,8 @@ export default function connectPagination(renderFn) {
render({results, state, instantSearchInstance}) {
renderFn({
createURL: this.createURL(state),
currentPage: state.page,
setPage: this.setPage,
currentRefinement: state.page,
refine: this.refine,
nbHits: results.nbHits,
nbPages: this.getMaxPage(results),
widgetParams,
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/pagination/__tests__/pagination-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('pagination()', () => {
});

it('sets the page', () => {
widget.setPage(helper, 42);
widget.refine(helper, 42);
expect(helper.setPage.calledOnce).toBe(true);
expect(helper.search.calledOnce).toBe(true);
});
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('pagination()', () => {
it('should not scroll', () => {
widget = pagination({container, scrollTo: false});
widget.init({helper});
widget.setPage(helper, 2);
widget.refine(helper, 2);
expect(scrollIntoView.calledOnce).toBe(false, 'scrollIntoView never called');
});

Expand Down
12 changes: 6 additions & 6 deletions src/widgets/pagination/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ const renderer = ({
scrollToNode,
}) => ({
createURL,
currentPage,
currentRefinement,
nbHits,
nbPages,
setPage,
refine,
}, isFirstRendering) => {
if (isFirstRendering) return;

const setCurrentPage = () => {
setPage();
const setCurrrentPage = () => {
refine();

if (scrollToNode !== false) {
scrollToNode.scrollIntoView();
Expand All @@ -49,12 +49,12 @@ const renderer = ({
<Pagination
createURL={createURL}
cssClasses={cssClasses}
currentPage={currentPage}
currentPage={currentRefinement}
labels={labels}
nbHits={nbHits}
nbPages={nbPages}
padding={padding}
setCurrentPage={setCurrentPage}
setCurrentPage={setCurrrentPage}
shouldAutoHideContainer={shouldAutoHideContainer}
showFirstLast={showFirstLast}
/>,
Expand Down

0 comments on commit f783fea

Please sign in to comment.