Skip to content

Commit fc03cee

Browse files
authored
feat: Add local pagination feature (#56)
This commit adds the implementation for local pagination in the `withPagination` function. It allows for displaying a subset of items from a larger collection, based on the current page and page size. The `withPagination` function accepts an optional `collection` parameter to support multiple paginated collections. It calculates the selected page entities, total count, total pages, and page navigation array based on the provided options. Additionally, utility functions like `gotoPage`, `setPageSize`, `nextPage`, `previousPage`, `firstPage`, and `setMaxPageNavigationArrayItems` are provided to easily update the pagination state. Export with-pagination Downgrade signals package Update signal package
1 parent f0d8b96 commit fc03cee

File tree

6 files changed

+459
-6
lines changed

6 files changed

+459
-6
lines changed

libs/ngrx-toolkit/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/angular-architects/ngrx-toolkit"
88
},
99
"peerDependencies": {
10-
"@ngrx/signals": "^17.0.0"
10+
"@ngrx/signals": "^17.2.0"
1111
},
1212
"dependencies": {
1313
"tslib": "^2.3.0"

libs/ngrx-toolkit/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export * from './lib/with-data-service';
1212
export { withStorageSync, SyncConfig } from './lib/with-storage-sync';
1313
export * from './lib/redux-connector';
1414
export * from './lib/redux-connector/rxjs-interop';
15+
export * from './lib/with-pagination';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { patchState, signalStore, type } from '@ngrx/signals';
2+
import { createPageArray, gotoPage, setPageSize, withPagination } from './with-pagination';
3+
import { setAllEntities, withEntities } from '@ngrx/signals/entities';
4+
5+
type Book = { id: number; title: string; author: string };
6+
const generateBooks = (count = 10) => {
7+
const books = [] as Book[];
8+
for (let i = 1; i <= count; i++) {
9+
books.push({ id: i, title: `Book ${i}`, author: `Author ${i}` });
10+
}
11+
return books;
12+
};
13+
14+
describe('withPagination', () => {
15+
it('should use and update a pagination', () => {
16+
const Store = signalStore(
17+
withEntities({ entity: type<Book>() }),
18+
withPagination()
19+
);
20+
21+
const store = new Store();
22+
23+
patchState(store, setAllEntities(generateBooks(55)));
24+
expect(store.currentPage()).toBe(1);
25+
expect(store.pageCount()).toBe(6);
26+
}),
27+
it('should use and update a pagination with collection', () => {
28+
const Store = signalStore(
29+
withEntities({ entity: type<Book>(), collection: 'books' }),
30+
withPagination({ collection: 'books' })
31+
);
32+
33+
const store = new Store();
34+
35+
patchState(
36+
store,
37+
setAllEntities(generateBooks(55), { collection: 'books' })
38+
);
39+
40+
patchState(store, gotoPage(6, { collection: 'books' }));
41+
expect(store.booksCurrentPage()).toBe(6);
42+
expect(store.selectedPageBooksEntities().length).toBe(5);
43+
expect(store.booksPageCount()).toBe(6);
44+
}),
45+
it('should react on enitiy changes', () => {
46+
const Store = signalStore(
47+
withEntities({ entity: type<Book>()}),
48+
withPagination()
49+
);
50+
51+
const store = new Store();
52+
53+
patchState(
54+
store,
55+
setAllEntities(generateBooks(100))
56+
);
57+
58+
expect(store.pageCount()).toBe(10);
59+
60+
patchState(
61+
store,
62+
setAllEntities(generateBooks(20))
63+
);
64+
65+
expect(store.pageCount()).toBe(2);
66+
67+
68+
patchState(
69+
store,
70+
setPageSize(5)
71+
);
72+
73+
expect(store.pageCount()).toBe(4);
74+
75+
}),
76+
describe('internal pageNavigationArray', () => {
77+
it('should return an array of page numbers', () => {
78+
const pages = createPageArray(8, 10, 500, 7);
79+
expect(pages).toEqual([
80+
{ label: 5, value: 5 },
81+
{ label: '...', value: 6 },
82+
{ label: 7, value: 7 },
83+
{ label: 8, value: 8 },
84+
{ label: 9, value: 9 },
85+
{ label: '...', value: 10 },
86+
{ label: 50, value: 50 },
87+
]);
88+
});
89+
});
90+
});

0 commit comments

Comments
 (0)