Skip to content

Commit ad09e31

Browse files
msari-ipe-ext-1rainerhahnekamp
authored andcommittedFeb 11, 2025
fix: improve tree shaking for with-pagination
1 parent 695461c commit ad09e31

File tree

3 files changed

+54
-126
lines changed

3 files changed

+54
-126
lines changed
 

‎apps/demo/src/app/flight-search-with-pagination/flight-search-with-pagination.component.html

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
<h2 class="title">Flight Search (Pagination)</h2>
22

3-
43
<form (ngSubmit)="search()">
54
<div>
65
<mat-form-field>
76
<mat-label>Name</mat-label>
8-
<input [(ngModel)]="searchParams.from" name="from" matInput />
7+
<input
8+
[(ngModel)]="searchParams.from"
9+
name="from"
10+
matInput
11+
aria-label="From airport"
12+
/>
913
</mat-form-field>
1014
</div>
1115

1216
<div>
1317
<mat-form-field>
1418
<mat-label>Name</mat-label>
15-
<input [(ngModel)]="searchParams.to" name="to" matInput />
19+
<input
20+
[(ngModel)]="searchParams.to"
21+
name="to"
22+
matInput
23+
aria-label="To airport"
24+
/>
1625
</mat-form-field>
1726
</div>
1827

19-
<button mat-raised-button>Search</button>
28+
<button mat-raised-button type="submit">Search</button>
2029
</form>
2130

2231
<mat-table [dataSource]="dataSource">
@@ -46,11 +55,13 @@ <h2 class="title">Flight Search (Pagination)</h2>
4655
(click)="selection.toggle(row)"
4756
></mat-row>
4857
</mat-table>
49-
<mat-paginator [length]="flightStore.flightTotalCount()"
50-
[pageSize]="flightStore.flightPageSize()"
51-
[pageIndex]="flightStore.flightCurrentPage()"
52-
[showFirstLastButtons]="true"
53-
[pageSizeOptions]="[5, 10, 25]"
54-
(page)="handlePageEvent($event)"
55-
aria-label="Select page">
58+
<mat-paginator
59+
[length]="flightStore.flightTotalCount()"
60+
[pageSize]="flightStore.flightPageSize()"
61+
[pageIndex]="flightStore.flightCurrentPage()"
62+
[showFirstLastButtons]="true"
63+
[pageSizeOptions]="[5, 10, 25]"
64+
(page)="handlePageEvent($event)"
65+
aria-label="Select page"
66+
>
5667
</mat-paginator>
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
import { FlightService } from '../shared/flight.service';
2-
3-
import { signalStore, type } from '@ngrx/signals';
4-
2+
import { patchState, signalStore, type, withMethods } from '@ngrx/signals';
53
import { withEntities } from '@ngrx/signals/entities';
64
import {
75
withCallState,
86
withDataService,
97
withPagination,
8+
setPageSize,
9+
gotoPage,
1010
} from '@angular-architects/ngrx-toolkit';
1111
import { Flight } from '../shared/flight';
1212

13+
// Name of the collection
14+
const collectionName = 'flight';
15+
1316
export const FlightBookingStore = signalStore(
1417
withCallState({
15-
collection: 'flight',
18+
collection: collectionName,
1619
}),
1720
withEntities({
1821
entity: type<Flight>(),
19-
collection: 'flight',
22+
collection: collectionName,
2023
}),
2124
withDataService({
2225
dataServiceType: FlightService,
2326
filter: { from: 'Wien', to: '' },
24-
collection: 'flight',
27+
collection: collectionName,
2528
}),
2629
withPagination({
2730
entity: type<Flight>(),
28-
collection: 'flight',
29-
})
31+
collection: collectionName,
32+
}),
33+
withMethods((store) => ({
34+
setFlightPageSize: (size: number) => {
35+
patchState(store, setPageSize(size, { collection: collectionName }));
36+
},
37+
gotoFlightPage: (page: number) => {
38+
patchState(store, gotoPage(page, { collection: collectionName }));
39+
},
40+
}))
3041
);

‎libs/ngrx-toolkit/src/lib/with-pagination.ts

+13-107
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,13 @@
99
import { Signal, computed } from '@angular/core';
1010
import {
1111
SignalStoreFeature,
12-
patchState,
1312
signalStoreFeature,
1413
withComputed,
15-
withMethods,
1614
withState,
17-
WritableStateSource,
1815
EmptyFeatureResult,
1916
} from '@ngrx/signals';
2017
import { capitalize } from './with-data-service';
21-
import {
22-
EntityComputed,
23-
EntityState,
24-
NamedEntityComputed,
25-
} from './shared/signal-store-models';
18+
import { MethodsDictionary } from '@ngrx/signals/src/signal-store-models';
2619

2720
// This is a virtual page which is can be used to create a pagination control
2821
export type Page = { label: string | number; value: number };
@@ -63,20 +56,6 @@ export type NamedPaginationServiceSignals<E, Collection extends string> = {
6356
[K in Collection as `hasPrevious${Capitalize<K>}Page`]: Signal<boolean>;
6457
};
6558

66-
export type NamedPaginationServiceMethods<Collection extends string> = {
67-
[K in Collection as `set${Capitalize<K>}PageSize`]: (size: number) => void;
68-
} & {
69-
[K in Collection as `next${Capitalize<K>}Page`]: () => void;
70-
} & {
71-
[K in Collection as `previous${Capitalize<K>}Page`]: () => void;
72-
} & {
73-
[K in Collection as `last${Capitalize<K>}Page`]: () => void;
74-
} & {
75-
[K in Collection as `first${Capitalize<K>}Page`]: () => void;
76-
} & {
77-
[K in Collection as `goto${Capitalize<K>}Page`]: (page: number) => void;
78-
};
79-
8059
export type PaginationServiceState<E> = {
8160
selectedPageEntities: Array<E>;
8261
currentPage: number;
@@ -99,15 +78,6 @@ export type PaginationServiceSignals<E> = {
9978
hasPreviousPage: Signal<boolean>;
10079
};
10180

102-
export type PaginationServiceMethods = {
103-
setPageSize: (size: number) => void;
104-
nextPageKey: () => void;
105-
previousPage: () => void;
106-
lastPage: () => void;
107-
firstPage: () => void;
108-
gotoPage: (page: number) => void;
109-
};
110-
11181
export type SetPaginationState<
11282
E,
11383
Collection extends string | undefined
@@ -119,31 +89,27 @@ export function withPagination<E, Collection extends string>(options: {
11989
entity: E;
12090
collection: Collection;
12191
}): SignalStoreFeature<
122-
EmptyFeatureResult & { props: NamedEntityComputed<E, Collection> },
92+
EmptyFeatureResult,
12393
{
12494
state: NamedPaginationServiceState<E, Collection>;
12595
props: NamedPaginationServiceSignals<E, Collection>;
126-
methods: NamedPaginationServiceMethods<Collection>;
96+
methods: MethodsDictionary;
12797
}
12898
>;
12999

130100
export function withPagination<E>(): SignalStoreFeature<
131-
EmptyFeatureResult & {
132-
state: EntityState<E>;
133-
props: EntityComputed<E>;
134-
},
101+
EmptyFeatureResult,
135102
{
136103
state: PaginationServiceState<E>;
137104
props: PaginationServiceSignals<E>;
138-
methods: PaginationServiceMethods;
105+
methods: MethodsDictionary;
139106
}
140107
>;
141108

142109
export function withPagination<E, Collection extends string>(options?: {
143110
entity: E;
144111
collection: Collection;
145-
}): /* eslint-disable @typescript-eslint/no-explicit-any */
146-
SignalStoreFeature<any, any> {
112+
}): SignalStoreFeature {
147113
const {
148114
pageKey,
149115
pageSizeKey,
@@ -153,12 +119,6 @@ SignalStoreFeature<any, any> {
153119
pageCountKey,
154120
pageNavigationArrayMaxKey,
155121
pageNavigationArrayKey,
156-
setPageSizeKey,
157-
nextPageKey,
158-
previousPageKey,
159-
lastPageKey,
160-
firstPageKey,
161-
gotoPageKey,
162122
hasNextPageKey,
163123
hasPreviousPageKey,
164124
} = createPaginationKeys<Collection>(options);
@@ -216,37 +176,7 @@ SignalStoreFeature<any, any> {
216176
return page() > 1;
217177
}),
218178
};
219-
}),
220-
withMethods(
221-
(store: Record<string, unknown> & WritableStateSource<object>) => {
222-
return {
223-
[setPageSizeKey]: (size: number) => {
224-
patchState(store, setPageSize(size, options));
225-
},
226-
[nextPageKey]: () => {
227-
patchState(store, nextPage(options));
228-
},
229-
230-
[previousPageKey]: () => {
231-
patchState(store, previousPage(options));
232-
},
233-
234-
[lastPageKey]: () => {
235-
const lastPage = (store[pageCountKey] as Signal<number>)();
236-
if (lastPage === 0) return;
237-
patchState(store, gotoPage(lastPage - 1, options));
238-
},
239-
240-
[firstPageKey]: () => {
241-
patchState(store, firstPage());
242-
},
243-
244-
[gotoPageKey]: (page: number) => {
245-
patchState(store, gotoPage(page, options));
246-
},
247-
};
248-
}
249-
)
179+
})
250180
);
251181
}
252182

@@ -330,49 +260,31 @@ function createPaginationKeys<Collection extends string>(
330260
const selectedPageEntitiesKey = options?.collection
331261
? `selectedPage${capitalize(options?.collection)}Entities`
332262
: 'selectedPageEntities';
263+
333264
const pageKey = options?.collection
334265
? `${options.collection}CurrentPage`
335266
: 'currentPage';
267+
336268
const pageSizeKey = options?.collection
337269
? `${options.collection}PageSize`
338270
: 'pageSize';
271+
339272
const totalCountKey = options?.collection
340273
? `${options.collection}TotalCount`
341274
: 'totalCount';
275+
342276
const pageCountKey = options?.collection
343277
? `${options.collection}PageCount`
344278
: 'pageCount';
279+
345280
const pageNavigationArrayMaxKey = options?.collection
346281
? `${options.collection}PageNavigationArrayMax`
347282
: 'pageNavigationArrayMax';
283+
348284
const pageNavigationArrayKey = options?.collection
349285
? `${options.collection}PageNavigationArray`
350286
: 'pageNavigationArray';
351287

352-
const setPageSizeKey = options?.collection
353-
? `set${capitalize(options.collection)}PageSize`
354-
: 'setPageSize';
355-
356-
const nextPageKey = options?.collection
357-
? `next${capitalize(options.collection)}Page`
358-
: 'nextPage';
359-
360-
const previousPageKey = options?.collection
361-
? `previous${capitalize(options.collection)}Page`
362-
: 'previousPage';
363-
364-
const lastPageKey = options?.collection
365-
? `last${capitalize(options.collection)}Page`
366-
: 'lastPage';
367-
368-
const firstPageKey = options?.collection
369-
? `first${capitalize(options.collection)}Page`
370-
: 'firstPage';
371-
372-
const gotoPageKey = options?.collection
373-
? `goto${capitalize(options.collection)}Page`
374-
: 'gotoPage';
375-
376288
const hasNextPageKey = options?.collection
377289
? `hasNext${capitalize(options.collection)}Page`
378290
: 'hasNextPage';
@@ -390,12 +302,6 @@ function createPaginationKeys<Collection extends string>(
390302
pageCountKey,
391303
pageNavigationArrayKey,
392304
pageNavigationArrayMaxKey,
393-
setPageSizeKey,
394-
nextPageKey,
395-
previousPageKey,
396-
lastPageKey,
397-
firstPageKey,
398-
gotoPageKey,
399305
hasNextPageKey,
400306
hasPreviousPageKey,
401307
};

0 commit comments

Comments
 (0)
Please sign in to comment.