Refactor
This commit is contained in:
parent
9faca532c6
commit
47e371a41e
@ -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<PrimaryKey>,
|
||||
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<SearchOptions> {
|
||||
return this._currentConfig;
|
||||
}
|
||||
|
||||
updateSearchOptionsAndReload(options: SearchOptions): Observable<Class[]> {
|
||||
this._currentConfig = { ...this._currentConfig, options };
|
||||
return this.loadPage(0);
|
||||
updateSearchQueryAndReload(value: string): Observable<Class[]> {
|
||||
return this.loadPage(0, this._currentConfig.pageSize, { [this._searchKey]: value } as SearchOptions);
|
||||
}
|
||||
|
||||
reloadPage(): Observable<Class[]> {
|
||||
return this.loadPage(this._currentConfig.page);
|
||||
}
|
||||
|
||||
loadPage(page = 0, pageSize?: number): Observable<Class[]> {
|
||||
const options = this._currentConfig.options;
|
||||
return super
|
||||
._post<PaginatedResponse<Interface>>({
|
||||
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<Class[]> {
|
||||
const options = searchOptions ?? this._currentConfig.options;
|
||||
const size = pageSize ?? this._currentConfig.pageSize;
|
||||
|
||||
loadNextPage(): Observable<Class[]> {
|
||||
return this.loadPage(this._currentConfig.page + 1);
|
||||
}
|
||||
|
||||
loadPreviousPage(): Observable<Class[]> {
|
||||
return this.loadPage(this._currentConfig.page - 1);
|
||||
return super._post<PaginatedResponse<Interface>>({ 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)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user