diff --git a/src/lib/listing/services/paginated-entities.service.ts b/src/lib/listing/services/paginated-entities.service.ts index cf8b16c..43da78d 100644 --- a/src/lib/listing/services/paginated-entities.service.ts +++ b/src/lib/listing/services/paginated-entities.service.ts @@ -25,7 +25,7 @@ const DEFAULT_PAGE_SIZE = 100; /** * By default, if no implementation (class) is provided, Class = Interface & IListable */ -export class PaginatedEntitiesService< +export abstract class PaginatedEntitiesService< Interface, Class extends Interface & IListable, SearchOptions, @@ -38,50 +38,41 @@ export class PaginatedEntitiesService< totalHits: 0, }; - searchQuery = ''; + protected abstract readonly _searchKey: keyof SearchOptions; + + get searchQuery(): string { + return this._currentConfig.options[this._searchKey] as string; + } get config(): PaginatedConfig { return this._currentConfig; } - updateSearchOptionsAndReload(options: SearchOptions): Observable { - this._currentConfig = { ...this._currentConfig, options }; - return this.loadPage(0); + updateSearchQueryAndReload(value: string): Observable { + return this.loadPage(0, this._currentConfig.pageSize, { [this._searchKey]: value } as SearchOptions); } 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)), - ); - } + loadPage(page = 0, pageSize?: number, searchOptions?: SearchOptions): Observable { + const options = searchOptions ?? this._currentConfig.options; + const size = pageSize ?? this._currentConfig.pageSize; - loadNextPage(): Observable { - return this.loadPage(this._currentConfig.page + 1); - } - - loadPreviousPage(): Observable { - return this.loadPage(this._currentConfig.page - 1); + 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)), + ); } }