Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💄 Refined the paginator #135

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions src/components/paginator-ui-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
Nullable,
} from '../internal/types';

export type PaginatorActionIdFn = StringReturnableFn<PaginatorState
& { buttonId: PaginatorButtonId }>;
export type PaginatorActionIdFn =

Check failure on line 13 in src/components/paginator-ui-component.ts

View workflow job for this annotation

GitHub Actions / Lint, Test, and Build on Node.js 12.x

Trailing spaces not allowed

Check failure on line 13 in src/components/paginator-ui-component.ts

View workflow job for this annotation

GitHub Actions / Lint, Test, and Build on Node.js 13.x

Trailing spaces not allowed

Check failure on line 13 in src/components/paginator-ui-component.ts

View workflow job for this annotation

GitHub Actions / Lint, Test, and Build on Node.js 14.x

Trailing spaces not allowed
StringReturnableFn<PaginatorState & { buttonId: PaginatorButtonId }>;

export interface PageCountTextFnParams {
page: number;
Expand All @@ -28,10 +28,11 @@
previousButtonText: Nullable<string>;
pageCountTextFunction: Nullable<PaginatorPageCountTextFn>;
actionIdFunction: PaginatorActionIdFn;
builderFunction: PaginatorBuilderFn<T>,
builderFunction: PaginatorBuilderFn<T>;
}

const defaultPageCountText = ({ page, totalPages }: PageCountTextFnParams) => `Page ${page} of ${totalPages}`;
const defaultPageCountText = ({ page, totalPages }: PageCountTextFnParams) =>

Check failure on line 34 in src/components/paginator-ui-component.ts

View workflow job for this annotation

GitHub Actions / Lint, Test, and Build on Node.js 12.x

Trailing spaces not allowed

Check failure on line 34 in src/components/paginator-ui-component.ts

View workflow job for this annotation

GitHub Actions / Lint, Test, and Build on Node.js 13.x

Trailing spaces not allowed

Check failure on line 34 in src/components/paginator-ui-component.ts

View workflow job for this annotation

GitHub Actions / Lint, Test, and Build on Node.js 14.x

Trailing spaces not allowed
`Page ${page} of ${totalPages}`;

Check failure on line 35 in src/components/paginator-ui-component.ts

View workflow job for this annotation

GitHub Actions / Lint, Test, and Build on Node.js 12.x

Expected no linebreak before this expression

Check failure on line 35 in src/components/paginator-ui-component.ts

View workflow job for this annotation

GitHub Actions / Lint, Test, and Build on Node.js 13.x

Expected no linebreak before this expression

Check failure on line 35 in src/components/paginator-ui-component.ts

View workflow job for this annotation

GitHub Actions / Lint, Test, and Build on Node.js 14.x

Expected no linebreak before this expression

export class PaginatorUIComponent<T> {
private readonly items: T[];
Expand Down Expand Up @@ -65,6 +66,28 @@
blocksForEach.push(this.builderFunction({ item: this.items[i] }).flat());
}

const actionButtons = [];

if (this.paginator.getPage() > 1) {
actionButtons.push(Elements.Button({
text: this.previousButtonText,
actionId: this.actionIdFunction({
buttonId: PaginatorButtonId.Previous,
...this.paginator.getPreviousPageState(),
}),
}));
}

if (this.paginator.getPage() < this.paginator.getTotalPages()) {
actionButtons.push(Elements.Button({
text: this.nextButtonText,
actionId: this.actionIdFunction({
buttonId: PaginatorButtonId.Next,
...this.paginator.getNextPageState(),
}),
}));
}

const unpruned = this.paginator.getTotalPages() > 1
? [
...blocksForEach.flat(),
Expand All @@ -73,23 +96,7 @@
totalPages: this.paginator.getTotalPages(),
})),
Blocks.Divider(),
Blocks.Actions()
.elements(
Elements.Button({
text: this.previousButtonText,
actionId: this.actionIdFunction({
buttonId: PaginatorButtonId.Previous,
...this.paginator.getPreviousPageState(),
}),
}),
Elements.Button({
text: this.nextButtonText,
actionId: this.actionIdFunction({
buttonId: PaginatorButtonId.Next,
...this.paginator.getNextPageState(),
}),
}),
),
Blocks.Actions().elements(...actionButtons),
]
: blocksForEach.flat();

Expand Down
20 changes: 2 additions & 18 deletions tests/components/easy-paginator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const getModalString = (blocks: BlockBuilder[]): string => Modal({ title: 'Testi
.buildToJSON();

describe('Testing Easy Paginator:', () => {
test('Creating a paginator with the default parameters creates the correct UI and callback data.', () => {
test('Creating a paginator with the default parameters creates the correct UI and callback data for the first page.', () => {
const result = getModalString(EasyPaginator<Human>({
page: 1,
perPage: 5,
Expand Down Expand Up @@ -184,14 +184,6 @@ describe('Testing Easy Paginator:', () => {
},
{
elements: [
{
text: {
type: 'plain_text',
text: 'Previous',
},
action_id: '{"buttonId":"previous","totalItems":20,"perPage":5,"totalPages":4,"offset":15,"page":4}',
type: 'button',
},
{
text: {
type: 'plain_text',
Expand Down Expand Up @@ -401,7 +393,7 @@ describe('Testing Easy Paginator:', () => {
}));
});

test('Creating a paginator with the default parameters creates the correct UI and callback data.', () => {
test('Creating a paginator with the default parameters creates the correct UI and callback data for the last page.', () => {
const result = getModalString(EasyPaginator({
items: humans,
page: 4,
Expand Down Expand Up @@ -581,14 +573,6 @@ describe('Testing Easy Paginator:', () => {
action_id: '{"buttonId":"previous","totalItems":20,"perPage":5,"totalPages":4,"offset":10,"page":3}',
type: 'button',
},
{
text: {
type: 'plain_text',
text: 'TEST FORWARD',
},
action_id: '{"buttonId":"next","totalItems":20,"perPage":5,"totalPages":4,"offset":0,"page":1}',
type: 'button',
},
],
type: 'actions',
},
Expand Down
28 changes: 2 additions & 26 deletions tests/components/paginator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const getModalString = (blocks: BlockBuilder[]): string => Modal({ title: 'Testi
.buildToJSON();

describe('Testing Paginator:', () => {
test('Creating a paginator with the default parameters creates the correct UI and callback data.', () => {
test('Creating a paginator with the default parameters creates the correct UI and callback data for the first page.', () => {
const items = [humans[0], humans[1], humans[2], humans[3], humans[4]];
const result = getModalString(Paginator<Human>({
items,
Expand Down Expand Up @@ -190,14 +190,6 @@ describe('Testing Paginator:', () => {
},
{
elements: [
{
text: {
type: 'plain_text',
text: 'Previous',
},
action_id: '{"buttonId":"previous","totalItems":20,"perPage":5,"totalPages":4,"offset":15,"page":4}',
type: 'button',
},
{
text: {
type: 'plain_text',
Expand Down Expand Up @@ -409,7 +401,7 @@ describe('Testing Paginator:', () => {
}));
});

test('Creating a paginator with the default parameters creates the correct UI and callback data.', () => {
test('Creating a paginator on the last page creates the correct UI and callback data.', () => {
const items = [humans[15], humans[16], humans[17], humans[18], humans[19]];
const result = getModalString(Paginator({
items,
Expand Down Expand Up @@ -591,14 +583,6 @@ describe('Testing Paginator:', () => {
action_id: '{"buttonId":"previous","totalItems":20,"perPage":5,"totalPages":4,"offset":10,"page":3}',
type: 'button',
},
{
text: {
type: 'plain_text',
text: 'TEST FORWARD',
},
action_id: '{"buttonId":"next","totalItems":20,"perPage":5,"totalPages":4,"offset":0,"page":1}',
type: 'button',
},
],
type: 'actions',
},
Expand Down Expand Up @@ -789,14 +773,6 @@ describe('Testing Paginator:', () => {
},
{
elements: [
{
text: {
type: 'plain_text',
text: 'Previous',
},
action_id: '{"buttonId":"previous","totalItems":20,"perPage":5,"totalPages":4,"offset":15,"page":4}',
type: 'button',
},
{
text: {
type: 'plain_text',
Expand Down
Loading