-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement and export createPaginationMeta helper.
- Loading branch information
Showing
16 changed files
with
188 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,7 @@ export const loadGroups = gql` | |
createdOn | ||
} | ||
meta { | ||
count | ||
totalCount | ||
totalPages | ||
to | ||
from | ||
nextPage | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/webiny-entity/__tests__/createPaginationMeta.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { createPaginationMeta } from "webiny-entity"; | ||
|
||
describe("createPaginationMeta test", () => { | ||
test("should return correct pagination meta data", async () => { | ||
let meta = createPaginationMeta({ | ||
page: 1, | ||
perPage: 10, | ||
totalCount: 100 | ||
}); | ||
|
||
expect(meta).toEqual({ | ||
from: 1, | ||
nextPage: 2, | ||
page: 1, | ||
perPage: 10, | ||
previousPage: null, | ||
to: 10, | ||
totalCount: 100, | ||
totalPages: 10 | ||
}); | ||
|
||
meta = createPaginationMeta({ | ||
totalCount: 100, | ||
page: 3, | ||
perPage: 7 | ||
}); | ||
|
||
expect(meta).toEqual({ | ||
page: 3, | ||
perPage: 7, | ||
totalCount: 100, | ||
from: 15, | ||
nextPage: 4, | ||
previousPage: 2, | ||
to: 21, | ||
totalPages: 15 | ||
}); | ||
|
||
meta = createPaginationMeta({ | ||
totalCount: 100, | ||
page: 3, | ||
perPage: 10 | ||
}); | ||
|
||
expect(meta).toEqual({ | ||
from: 21, | ||
nextPage: 4, | ||
page: 3, | ||
perPage: 10, | ||
previousPage: 2, | ||
to: 30, | ||
totalCount: 100, | ||
totalPages: 10 | ||
}); | ||
|
||
meta = createPaginationMeta({ | ||
totalCount: 15, | ||
page: 3, | ||
perPage: 6 | ||
}); | ||
|
||
expect(meta).toEqual({ | ||
from: 13, | ||
nextPage: null, | ||
page: 3, | ||
perPage: 6, | ||
previousPage: 2, | ||
to: 15, | ||
totalCount: 15, | ||
totalPages: 3 | ||
}); | ||
|
||
meta = createPaginationMeta({ | ||
totalCount: 0, | ||
page: 1, | ||
perPage: 10 | ||
}); | ||
|
||
expect(meta).toEqual({ | ||
page: 1, | ||
perPage: 10, | ||
nextPage: null, | ||
previousPage: null, | ||
to: 0, | ||
from: 0, | ||
totalCount: 0, | ||
totalPages: 0 | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// @flow | ||
type PaginationMeta = { | ||
page: number, | ||
perPage: number, | ||
totalCount: number, | ||
|
||
totalPages: ?number, | ||
from: ?number, | ||
to: ?number, | ||
nextPage: ?number, | ||
previousPage: ?number | ||
}; | ||
|
||
type Params = { | ||
page: number, | ||
perPage: number, | ||
totalCount: number | ||
}; | ||
|
||
export default (params: ?Params): PaginationMeta => { | ||
const meta: PaginationMeta = { | ||
page: 0, | ||
perPage: 0, | ||
totalCount: 0, | ||
totalPages: null, | ||
from: null, | ||
to: null, | ||
nextPage: null, | ||
previousPage: null, | ||
...params | ||
}; | ||
|
||
if (meta.page && meta.perPage) { | ||
meta.totalPages = Math.ceil(meta.totalCount / meta.perPage); | ||
|
||
if (meta.totalCount) { | ||
meta.from = 1 + meta.perPage * (meta.page - 1); | ||
} else { | ||
meta.from = 0; | ||
} | ||
|
||
meta.to = meta.perPage * meta.page; | ||
if (meta.to > meta.totalCount) { | ||
meta.to = meta.totalCount; | ||
} | ||
|
||
meta.nextPage = meta.page < meta.totalPages ? meta.page + 1 : null; | ||
meta.previousPage = meta.page === 1 ? null : meta.page - 1; | ||
} | ||
|
||
return meta; | ||
}; |
Oops, something went wrong.