Skip to content

Commit

Permalink
Merge pull request #38 from dabapps/fix-page-count-defaulting
Browse files Browse the repository at this point in the history
Use action meta page number
  • Loading branch information
lCharlie123l authored Mar 5, 2018
2 parents 9110b60 + 11695f8 commit 1a99f25
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dabapps/redux-api-collections",
"version": "0.2.2",
"version": "0.2.3",
"description": "Type-safe helpers for dealing with Rest-Framework backed collections in Typescript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/collections/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function updateCollectionItemsFromResponse<T extends IdKeyed>(
ordering,
reverseOrdering,
} = action.meta;
const { count, next, results, page } = action.payload;
const { count, next, results } = action.payload;

const oldCollectionItems = (collectionData[subgroup || ''] || { results: [] })
.results;
Expand All @@ -44,7 +44,7 @@ function updateCollectionItemsFromResponse<T extends IdKeyed>(
filters,
next,
ordering,
page: page || 1,
page: action.meta.page || action.payload.page || 1,
results: newCollectionResults,
immutableResults: useImmutable ? List<T>(newCollectionResults) : null,
reverseOrdering,
Expand Down
28 changes: 26 additions & 2 deletions tests/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ describe('Collections', () => {
results: ReadonlyArray<any>,
shouldAppend: boolean,
count?: number,
metaPage?: number,
next?: string
) {
return {
meta: { tag, shouldAppend, subgroup },
meta: { tag, shouldAppend, subgroup, page: metaPage },
payload: {
count,
page: 1,
page: metaPage ? undefined : 1,
next,
results,
},
Expand Down Expand Up @@ -319,6 +320,29 @@ describe('Collections', () => {
expect(results[0].furLength).toBe(5);
});

it('should update the page from the GET_COLLECTION request meta if the server does not return the page number', () => {
const data = collections.reducers.collectionsReducer(
undefined,
getCollectionSuccess(
'llamas',
'',
[
{
furLength: 5,
id: '1',
name: 'Drama',
},
],
false,
12,
6
)
);
const subCollection = getCollectionByName(data, 'llamas');
expect(subCollection.page).toBe(6);
expect(subCollection.count).toBe(12);
});

it('should add an item on ADD_TO_COLLECTION responses', () => {
const data = collections.reducers.collectionsReducer(
undefined,
Expand Down

0 comments on commit 1a99f25

Please sign in to comment.