diff --git a/src/lib/listing/services/paginated-entities.service.ts b/src/lib/listing/services/paginated-entities.service.ts index 53c898b..13a37b5 100644 --- a/src/lib/listing/services/paginated-entities.service.ts +++ b/src/lib/listing/services/paginated-entities.service.ts @@ -19,6 +19,8 @@ interface PaginatedConfig { readonly totalHits: number; } +const DEFAULT_PAGE_SIZE = 100; + @Injectable() /** * By default, if no implementation (class) is provided, Class = Interface & IListable @@ -26,41 +28,65 @@ interface PaginatedConfig { export class PaginatedEntitiesService< Interface, Class extends Interface & IListable, - Options, + SearchOptions, PrimaryKey extends Id = Class['id'], > extends EntitiesService { - protected _currentConfig: PaginatedConfig = { options: {} as Options, page: 0, pageSize: 0, totalHits: 0 }; + protected _currentConfig: PaginatedConfig = { + options: {} as SearchOptions, + page: 0, + pageSize: DEFAULT_PAGE_SIZE, + totalHits: 0, + }; - get config(): PaginatedConfig { + searchQuery = ''; + + get config(): PaginatedConfig { return this._currentConfig; } - reloadPage(): Observable { - return this.loadPage(this._currentConfig.options, this._currentConfig.page, this._currentConfig.pageSize); + constructor() { + super(); + console.log('PaginatedEntitiesService'); } - loadPage(options: Options = {} as Options, page = 0, size = 100): Observable { - return super._post>({ page, size, options }).pipe( - tap( - response => - (this._currentConfig = { - options, - page: response.page, - pageSize: response.pageSize, - totalHits: response.totalHits, - }), - ), - map(response => response.data), - mapEach(entity => (this._entityClass ? new this._entityClass(entity) : (entity as unknown as Class))), - tap((entities: Class[]) => this.setEntities(entities)), - ); + updateSearchOptionsAndReloadPage(options: SearchOptions): Observable { + this._currentConfig = { ...this._currentConfig, options }; + return this.loadPage(); + } + + reloadPage(): Observable { + return this.loadPage(this._currentConfig.page); + } + + loadPage(page = 0, pageSize?: number): Observable { + const options = this._currentConfig.options; + return super + ._post>({ + page, + size: pageSize ?? this._currentConfig.pageSize, + options, + }) + .pipe( + tap( + response => + (this._currentConfig = { + options, + page: response.page, + pageSize: response.pageSize, + totalHits: response.totalHits, + }), + ), + map(response => response.data), + mapEach(entity => (this._entityClass ? new this._entityClass(entity) : (entity as unknown as Class))), + tap((entities: Class[]) => this.setEntities(entities)), + ); } loadNextPage(): Observable { - return this.loadPage(this._currentConfig.options, this._currentConfig.page + 1, this._currentConfig.pageSize); + return this.loadPage(this._currentConfig.page + 1); } loadPreviousPage(): Observable { - return this.loadPage(this._currentConfig.options, this._currentConfig.page - 1, this._currentConfig.pageSize); + return this.loadPage(this._currentConfig.page - 1); } }