Skip to content

Commit

Permalink
Remove undefined current page for pagination
Browse files Browse the repository at this point in the history
refs #49
  • Loading branch information
franthormel committed Aug 5, 2024
1 parent 75913bd commit abe56cb
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/listings/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ListingsMap } from "./map"
export interface ListingsContentProps {
listings: PrismaListing[]
pages: number
currentPage?: number
currentPage: number
}

export default function ListingsContent(props: ListingsContentProps) {
Expand Down
14 changes: 4 additions & 10 deletions components/pagination/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* @param currentPage Current page
* @returns `true` if the previous button should be shown, otherwise false.
*/
export function checkShowPreviousButton(currentPage?: number) {
return currentPage !== undefined && currentPage > 1;
export function checkShowPreviousButton(currentPage: number) {
return currentPage > 1;
}

/**
Expand All @@ -16,12 +16,6 @@ export function checkShowPreviousButton(currentPage?: number) {
* @param currentPage Current page
* @returns `true` if the next button should be shown, otherwise false.
*/
export function checkShowNextButton(pages: number, currentPage?: number) {
if (currentPage === undefined) {
if (pages !== 1) {
return true;
}
} else {
return currentPage < pages;
}
export function checkShowNextButton(pages: number, currentPage: number) {
return currentPage < pages;
}
5 changes: 1 addition & 4 deletions components/pagination/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import PaginationItemNumber from "./item-number"

export interface PaginationProps {
pages: number
/**
* Current page defaults to `1` if no value is provided.
*/
currentPage?: number
currentPage: number
}

export default function Pagination(props: PaginationProps) {
Expand Down
4 changes: 3 additions & 1 deletion stories/pagination/Pagination.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {
pages: 5
pages: 5,
currentPage: 1
}
};

Expand Down Expand Up @@ -40,6 +41,7 @@ export const LastPage: Story = {
export const SinglePage: Story = {
args: {
pages: 1,
currentPage: 1
}
};

Expand Down
2 changes: 0 additions & 2 deletions tests/components/pagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ describe("Pagination", () => {
{ currentPage: 1, expected: false },
{ currentPage: 99, expected: true },
{ currentPage: NaN, expected: false },
{ currentPage: undefined, expected: false },
])("checkShowPreviousButton($currentPage)", ({ currentPage, expected }) => {
const output = checkShowPreviousButton(currentPage);
expect(output).toBe(expected);
Expand All @@ -25,7 +24,6 @@ describe("Pagination", () => {
{ pages: 100, currentPage: 101, expected: false },
{ pages: 100, currentPage: 201, expected: false },
{ pages: 100, currentPage: NaN, expected: false },
{ pages: 100, currentPage: undefined, expected: true },
])(
"checkShowNextButton($pages, $currentPage)",
({ pages, currentPage, expected }) => {
Expand Down

0 comments on commit abe56cb

Please sign in to comment.