Skip to content

Commit

Permalink
Merge pull request #24 from taro-28/fix-pagination-page-index
Browse files Browse the repository at this point in the history
Fix pagination pageIndex
  • Loading branch information
taro-28 authored Aug 12, 2024
2 parents 4d76964 + 0ec86e4 commit 30c3080
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("pagination", () => {
{
name: "basic",
pagination: { pageIndex: 2, pageSize: 50 },
want: { pageIndex: "2", pageSize: "50" },
want: { pageIndex: "3", pageSize: "50" },
},
{
name: "default pageIndex and pageSize",
Expand All @@ -30,7 +30,7 @@ describe("pagination", () => {
{
name: "default pageSize",
pagination: { pageIndex: 2, pageSize: defaultPagination.pageSize },
want: { pageIndex: "2", pageSize: undefined },
want: { pageIndex: "3", pageSize: undefined },
},
{
name: "0 pageIndex and pageSize",
Expand All @@ -45,7 +45,7 @@ describe("pagination", () => {
{
name: "0 pageSize",
pagination: { pageIndex: 2, pageSize: 0 },
want: { pageIndex: "2", pageSize: "0" },
want: { pageIndex: "3", pageSize: "0" },
},
])("$name", ({ pagination, want }) =>
expect(encodePagination(pagination)).toEqual(want),
Expand All @@ -60,7 +60,7 @@ describe("pagination", () => {
{
name: "basic",
queryValues: { pageIndex: "2", pageSize: "25" },
want: { pageIndex: 2, pageSize: 25 },
want: { pageIndex: 1, pageSize: 25 },
},
{
name: "invalid pageIndex and pageSize",
Expand All @@ -75,7 +75,7 @@ describe("pagination", () => {
{
name: "invalid pageSize",
queryValues: { pageIndex: "2", pageSize: "foo" },
want: { pageIndex: 2, pageSize: defaultPagination.pageSize },
want: { pageIndex: 1, pageSize: defaultPagination.pageSize },
},
{
name: "undefined pageIndex and pageSize",
Expand All @@ -85,7 +85,7 @@ describe("pagination", () => {
{
name: "undefined pageSize",
queryValues: { pageIndex: "2", pageSize: undefined },
want: { pageIndex: 2, pageSize: defaultPagination.pageSize },
want: { pageIndex: 1, pageSize: defaultPagination.pageSize },
},
{
name: "undefined pageIndex",
Expand All @@ -105,7 +105,7 @@ describe("pagination", () => {
{
name: "empty pageSize",
queryValues: { pageIndex: "2", pageSize: "" },
want: { pageIndex: 2, pageSize: defaultPagination.pageSize },
want: { pageIndex: 1, pageSize: defaultPagination.pageSize },
},
{
name: "0 pageIndex and pageSize",
Expand All @@ -120,7 +120,7 @@ describe("pagination", () => {
{
name: "pageSize is 0",
queryValues: { pageIndex: "2", pageSize: "0" },
want: { pageIndex: 2, pageSize: 0 },
want: { pageIndex: 1, pageSize: 0 },
},
])("$name", ({ queryValues, want }) =>
expect(decodePagination(queryValues)).toEqual(want),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const encodePagination = (
pageIndex:
pagination.pageIndex === defaultPagination.pageIndex
? undefined
: pagination.pageIndex.toString(),
: (pagination.pageIndex + 1).toString(),
pageSize:
pagination.pageSize === defaultPagination.pageSize
? undefined
Expand All @@ -30,9 +30,9 @@ export const decodePagination = (queryValues: {
const pageSize = Number(queryValues.pageSize);
return {
pageIndex:
queryValues.pageIndex === "" || Number.isNaN(pageIndex)
queryValues.pageIndex === "" || Number.isNaN(pageIndex) || pageIndex < 1
? defaultPagination.pageIndex
: pageIndex,
: pageIndex - 1,
pageSize:
queryValues.pageSize === "" || Number.isNaN(pageSize)
? defaultPagination.pageSize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe("pagination", () => {
pageIndex: 1,
pageSize: 20,
}) ?? {
[paramName.pageIndex]: "1",
[paramName.pageIndex]: "2",
[paramName.pageSize]: "20",
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe("pagination", () => {
pageIndex: 1,
pageSize: 20,
}) ?? {
[paramName.pageIndex]: "1",
[paramName.pageIndex]: "2",
[paramName.pageSize]: "20",
},
);
Expand Down

0 comments on commit 30c3080

Please sign in to comment.