9
9
import { Signal , computed } from '@angular/core' ;
10
10
import {
11
11
SignalStoreFeature ,
12
- patchState ,
13
12
signalStoreFeature ,
14
13
withComputed ,
15
- withMethods ,
16
14
withState ,
17
- WritableStateSource ,
18
15
EmptyFeatureResult ,
19
16
} from '@ngrx/signals' ;
20
17
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' ;
26
19
27
20
// This is a virtual page which is can be used to create a pagination control
28
21
export type Page = { label : string | number ; value : number } ;
@@ -63,20 +56,6 @@ export type NamedPaginationServiceSignals<E, Collection extends string> = {
63
56
[ K in Collection as `hasPrevious${Capitalize < K > } Page`] : Signal < boolean > ;
64
57
} ;
65
58
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
-
80
59
export type PaginationServiceState < E > = {
81
60
selectedPageEntities : Array < E > ;
82
61
currentPage : number ;
@@ -99,15 +78,6 @@ export type PaginationServiceSignals<E> = {
99
78
hasPreviousPage : Signal < boolean > ;
100
79
} ;
101
80
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
-
111
81
export type SetPaginationState <
112
82
E ,
113
83
Collection extends string | undefined
@@ -119,31 +89,27 @@ export function withPagination<E, Collection extends string>(options: {
119
89
entity : E ;
120
90
collection : Collection ;
121
91
} ) : SignalStoreFeature <
122
- EmptyFeatureResult & { props : NamedEntityComputed < E , Collection > } ,
92
+ EmptyFeatureResult ,
123
93
{
124
94
state : NamedPaginationServiceState < E , Collection > ;
125
95
props : NamedPaginationServiceSignals < E , Collection > ;
126
- methods : NamedPaginationServiceMethods < Collection > ;
96
+ methods : MethodsDictionary ;
127
97
}
128
98
> ;
129
99
130
100
export function withPagination < E > ( ) : SignalStoreFeature <
131
- EmptyFeatureResult & {
132
- state : EntityState < E > ;
133
- props : EntityComputed < E > ;
134
- } ,
101
+ EmptyFeatureResult ,
135
102
{
136
103
state : PaginationServiceState < E > ;
137
104
props : PaginationServiceSignals < E > ;
138
- methods : PaginationServiceMethods ;
105
+ methods : MethodsDictionary ;
139
106
}
140
107
> ;
141
108
142
109
export function withPagination < E , Collection extends string > ( options ?: {
143
110
entity : E ;
144
111
collection : Collection ;
145
- } ) : /* eslint-disable @typescript-eslint/no-explicit-any */
146
- SignalStoreFeature < any , any > {
112
+ } ) : SignalStoreFeature {
147
113
const {
148
114
pageKey,
149
115
pageSizeKey,
@@ -153,12 +119,6 @@ SignalStoreFeature<any, any> {
153
119
pageCountKey,
154
120
pageNavigationArrayMaxKey,
155
121
pageNavigationArrayKey,
156
- setPageSizeKey,
157
- nextPageKey,
158
- previousPageKey,
159
- lastPageKey,
160
- firstPageKey,
161
- gotoPageKey,
162
122
hasNextPageKey,
163
123
hasPreviousPageKey,
164
124
} = createPaginationKeys < Collection > ( options ) ;
@@ -216,37 +176,7 @@ SignalStoreFeature<any, any> {
216
176
return page ( ) > 1 ;
217
177
} ) ,
218
178
} ;
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
+ } )
250
180
) ;
251
181
}
252
182
@@ -330,49 +260,31 @@ function createPaginationKeys<Collection extends string>(
330
260
const selectedPageEntitiesKey = options ?. collection
331
261
? `selectedPage${ capitalize ( options ?. collection ) } Entities`
332
262
: 'selectedPageEntities' ;
263
+
333
264
const pageKey = options ?. collection
334
265
? `${ options . collection } CurrentPage`
335
266
: 'currentPage' ;
267
+
336
268
const pageSizeKey = options ?. collection
337
269
? `${ options . collection } PageSize`
338
270
: 'pageSize' ;
271
+
339
272
const totalCountKey = options ?. collection
340
273
? `${ options . collection } TotalCount`
341
274
: 'totalCount' ;
275
+
342
276
const pageCountKey = options ?. collection
343
277
? `${ options . collection } PageCount`
344
278
: 'pageCount' ;
279
+
345
280
const pageNavigationArrayMaxKey = options ?. collection
346
281
? `${ options . collection } PageNavigationArrayMax`
347
282
: 'pageNavigationArrayMax' ;
283
+
348
284
const pageNavigationArrayKey = options ?. collection
349
285
? `${ options . collection } PageNavigationArray`
350
286
: 'pageNavigationArray' ;
351
287
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
-
376
288
const hasNextPageKey = options ?. collection
377
289
? `hasNext${ capitalize ( options . collection ) } Page`
378
290
: 'hasNextPage' ;
@@ -390,12 +302,6 @@ function createPaginationKeys<Collection extends string>(
390
302
pageCountKey,
391
303
pageNavigationArrayKey,
392
304
pageNavigationArrayMaxKey,
393
- setPageSizeKey,
394
- nextPageKey,
395
- previousPageKey,
396
- lastPageKey,
397
- firstPageKey,
398
- gotoPageKey,
399
305
hasNextPageKey,
400
306
hasPreviousPageKey,
401
307
} ;
0 commit comments