Skip to content

Commit 8bae6b1

Browse files
committed
feat(paginator): add paginator config
Adds the ability to configure the pager during initialization. Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
1 parent d4c64b9 commit 8bae6b1

File tree

5 files changed

+37
-46
lines changed

5 files changed

+37
-46
lines changed

src/app/features/books/books.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ <h1 class="text-3xl font-bold text-gray-800 mb-2">Bibliothèque</h1>
3030

3131
<!-- Pagination -->
3232
<div class="flex justify-center">
33-
<shared-paginator [paginatorState]="paginatorConfig()" (pageChange)="store.setPaginator($event)" />
33+
<shared-paginator [pager]="store.pager()" [total]="store.total()" (pageChange)="store.changePage($event)" />
3434
</div>
3535
} @else {
3636
<!-- Empty State -->

src/app/features/books/books.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Component, inject, Signal } from '@angular/core';
2-
import { deepComputed } from '@ngrx/signals';
3-
import { PaginatorComponent, PaginatorConfig } from '../../shared/component/paginator';
1+
import { Component, inject } from '@angular/core';
2+
import { PaginatorComponent } from '../../shared/component/paginator';
43
import { Book } from './book';
54
import { Search } from './search';
65
import { OpenLibraryStore } from './store/open-library.store';
@@ -13,10 +12,4 @@ import { OpenLibraryStore } from './store/open-library.store';
1312
})
1413
export default class Books {
1514
protected store = inject(OpenLibraryStore);
16-
17-
protected paginatorConfig: Signal<PaginatorConfig> = deepComputed(() => ({
18-
first: this.store.pager.firstRecord(),
19-
rows: this.store.pager.rows(),
20-
total: this.store.total()
21-
}));
2215
}

src/app/features/books/store/open-library.store.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import { inject } from "@angular/core";
22
import { toObservable } from "@angular/core/rxjs-interop";
33
import { patchState, signalStore, withHooks, withMethods, withState } from "@ngrx/signals";
44
import { rxMethod } from "@ngrx/signals/rxjs-interop";
5-
import { PaginatorState } from "primeng/paginator";
65
import { debounceTime, pipe, skip, switchMap, tap } from "rxjs";
7-
import { withPaginator } from "../../../shared/store/paginator-feature";
6+
import { Pager, withPaginator } from "../../../shared/store/paginator-feature";
87
import { setFulfilled, setPending, withRequestStatus } from "../../../shared/store/request-status-feature";
98
import { OpenLibraryApiResult, OpenLibraryRecord } from "../model/open-library.model";
109
import { OpenLibraryBase } from "../service/open-library-base";
@@ -21,16 +20,23 @@ const initialOpenLibraryState = {
2120
documents: []
2221
};
2322

23+
const initialPagerConfig: Pager = {
24+
page: 1,
25+
first: 1,
26+
rows: 9,
27+
rowsPerPageOptions: [9, 18, 36]
28+
}
29+
2430
export const OpenLibraryStore = signalStore(
2531
withState<OpenLibraryState>(initialOpenLibraryState),
2632
withRequestStatus(),
27-
withPaginator(),
33+
withPaginator(initialPagerConfig),
2834
withMethods((store, api = inject(OpenLibraryBase)) => ({
2935
search: rxMethod<string>(
3036
pipe(
3137
debounceTime(500),
3238
tap(() => patchState(store, setPending())),
33-
switchMap((query: string) => api.search(query, store.pager.currentPage(), store.pager.rows())),
39+
switchMap((query: string) => api.search(query, store.pager.page(), store.pager.rows())),
3440
tap((result?: OpenLibraryApiResult) => {
3541
if (result) {
3642
patchState(
@@ -45,9 +51,6 @@ export const OpenLibraryStore = signalStore(
4551
reset(): void {
4652
patchState(store, { total: 0, filter: '', documents: []});
4753
},
48-
setPaginator(paginator: PaginatorState) {
49-
store.changePage(paginator);
50-
}
5154
})),
5255
withHooks((store) => ({
5356
onInit: () => {
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
import { Component, input, output } from '@angular/core';
22
import { Paginator, PaginatorState } from 'primeng/paginator';
3-
4-
export type PaginatorConfig = {
5-
first: number;
6-
rows: number;
7-
total: number;
8-
}
3+
import { Pager } from '../store/paginator-feature';
94

105
@Component({
116
selector: 'shared-paginator',
127
imports: [Paginator],
138
template: `
149
<p-paginator
1510
alwaysShow="false"
16-
[first]="paginatorState().first"
17-
[rows]="paginatorState().rows"
18-
[totalRecords]="paginatorState().total"
19-
[rowsPerPageOptions]="[10, 20, 50]"
11+
[first]="pager().first"
12+
[rows]="pager().rows"
13+
[totalRecords]="total()"
14+
[rowsPerPageOptions]="pager().rowsPerPageOptions"
2015
(onPageChange)="pageChange.emit($event)" />
2116
`
2217
})
2318
export class PaginatorComponent {
2419

25-
paginatorState = input.required<PaginatorConfig>();
20+
pager = input.required<Pager>();
21+
22+
total = input.required<number>();
2623

2724
pageChange = output<PaginatorState>();
2825
}

src/app/shared/store/paginator-feature.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
import { patchState, signalMethod, signalStoreFeature, withMethods, withState } from "@ngrx/signals";
22
import { PaginatorState } from "primeng/paginator";
33

4+
export type Pager = {
5+
page: number;
6+
first: number;
7+
rows: number;
8+
rowsPerPageOptions: number[];
9+
};
10+
411
export type Paginator = {
5-
pager: {
6-
currentPage: number;
7-
firstRecord: number;
8-
rows: number;
9-
}
12+
pager: Pager;
1013
}
1114

12-
const paginatorInitialState: Paginator = {
13-
pager: {
14-
currentPage: 1,
15-
firstRecord: 1,
16-
rows: 10
17-
}
18-
};
19-
20-
export function withPaginator() {
15+
export function withPaginator(pager: Pager) {
2116
return signalStoreFeature(
22-
withState<Paginator>(paginatorInitialState),
17+
withState<Paginator>({
18+
pager: pager
19+
}),
2320
withMethods((store) => ({
2421
changePage: signalMethod<PaginatorState>(event => {
2522
if (event.rows !== store.pager.rows()) {
@@ -28,9 +25,10 @@ export function withPaginator() {
2825
}
2926
patchState(store, {
3027
pager: {
31-
currentPage: (event.page || 0) + 1,
32-
firstRecord: (event.page || 0) * (event.rows || 10) + 1,
33-
rows: (event.rows || 10)
28+
page: (event.page || 0) + 1,
29+
first: (event.page || 0) * (event.rows || pager.rows) + 1,
30+
rows: (event.rows || pager.rows),
31+
rowsPerPageOptions: store.pager().rowsPerPageOptions
3432
}
3533
});
3634
}),

0 commit comments

Comments
 (0)