From c15481fc2dd07bea0d4d750443e57fdeb064eca7 Mon Sep 17 00:00:00 2001 From: Koen Pauwels Date: Wed, 14 Dec 2022 15:41:40 +0100 Subject: [PATCH] 97257 Reworked ItemDataService.findItemsWithEdit, added pagination parameters --- src/app/core/data/item-data.service.ts | 61 ++++++++++++------- .../editable-item-selector.component.ts | 15 ++++- 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/src/app/core/data/item-data.service.ts b/src/app/core/data/item-data.service.ts index d85941a0ee1..13f74cba9b6 100644 --- a/src/app/core/data/item-data.service.ts +++ b/src/app/core/data/item-data.service.ts @@ -47,16 +47,18 @@ import { CreateData, CreateDataImpl } from './base/create-data'; import { RequestParam } from '../cache/models/request-param.model'; import { dataService } from './base/data-service.decorator'; import { FollowLinkConfig } from 'src/app/shared/utils/follow-link-config.model'; +import { SearchData, SearchDataImpl } from './base/search-data'; /** * An abstract service for CRUD operations on Items * Doesn't specify an endpoint because multiple endpoints support Item-like functionality (e.g. items, itemtemplates) * Extend this class to implement data services for Items */ -export abstract class BaseItemDataService extends IdentifiableDataService implements CreateData, PatchData, DeleteData { +export abstract class BaseItemDataService extends IdentifiableDataService implements CreateData, PatchData, DeleteData, SearchData { private createData: CreateData; private patchData: PatchData; private deleteData: DeleteData; + private searchData: SearchData; protected constructor( protected linkPath, @@ -75,6 +77,7 @@ export abstract class BaseItemDataService extends IdentifiableDataService this.createData = new CreateDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, notificationsService, this.responseMsToLive); this.patchData = new PatchDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, comparator, this.responseMsToLive, this.constructIdEndpoint); this.deleteData = new DeleteDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, notificationsService, this.responseMsToLive, this.constructIdEndpoint); + this.searchData = new SearchDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, this.responseMsToLive); } /** @@ -389,31 +392,43 @@ export abstract class BaseItemDataService extends IdentifiableDataService return this.createData.create(object, ...params); } - public getSearchEndpoint(topic: string): Observable { - return this.halService.getEndpoint(this.linkPath).pipe( - map((endpoint: string) => { - let result = `${endpoint}/search/${topic}`; - console.log(result); - return result; - }) - // switchMap((url: string) => this.halService.getEndpoint('search', `${topic}`)) - ); - } + // private getSearchEndpoint(topic: string): Observable { + // return this.halService.getEndpoint(this.linkPath).pipe( + // map((endpoint: string) => { + // let result = `${endpoint}/search/${topic}`; + // console.log(result); + // return result; + // }) + // ); + // } - public findItemsWithEdit(...linksToFollow: FollowLinkConfig[]): Observable>> { - const hrefObs = this.getSearchEndpoint("findItemsWithEdit"); - hrefObs.pipe( - isNotEmptyOperator(), - take(1) - ).subscribe((href) => { - const request = new GetRequest(this.requestService.generateRequestId(), href); - this.requestService.send(request); - }); - return this.rdbService.buildList(hrefObs, ...linksToFollow).pipe( - filter((items: RemoteData>) => !items.isResponsePending) - ); + /** + * Make a new FindListRequest with given search method + * + * @param searchMethod The search method for the object + * @param options The [[FindListOptions]] object + * @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's + * no valid cached version. Defaults to true + * @param reRequestOnStale Whether or not the request should automatically be re- + * requested after the response becomes stale + * @param linksToFollow List of {@link FollowLinkConfig} that indicate which + * {@link HALLink}s should be automatically resolved + * @return {Observable>} + * Return an observable that emits response from the server + */ + public searchBy(searchMethod: string, options?: FindListOptions, useCachedVersionIfAvailable?: boolean, reRequestOnStale?: boolean, ...linksToFollow: FollowLinkConfig[]): Observable>> { + return this.searchData.searchBy(searchMethod, options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow); } + /** + * Find the list of items for which the current user has editing rights. + * + * @param linksToFollow List of {@link FollowLinkConfig} that indicate which + * {@link HALLink}s should be automatically resolved + */ + public findItemsWithEdit(options: FindListOptions, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig[]): Observable>> { + return this.searchBy('findItemsWithEdit', options, useCachedVersionIfAvailable, reRequestOnStale ); + } } /** diff --git a/src/app/shared/dso-selector/dso-selector/editable-item-selector/editable-item-selector.component.ts b/src/app/shared/dso-selector/dso-selector/editable-item-selector/editable-item-selector.component.ts index 93adbe29245..e8cdea6de4b 100644 --- a/src/app/shared/dso-selector/dso-selector/editable-item-selector/editable-item-selector.component.ts +++ b/src/app/shared/dso-selector/dso-selector/editable-item-selector/editable-item-selector.component.ts @@ -14,12 +14,16 @@ import { followLink } from '../../../utils/follow-link-config.model'; import { getFirstCompletedRemoteData, mapRemoteDataPayload } from 'src/app/core/shared/operators'; import { hasValue } from 'src/app/shared/empty.util'; import { ItemSearchResult } from 'src/app/shared/object-collection/shared/item-search-result.model'; +import { FindListOptions } from 'src/app/core/data/find-list-options.model'; @Component({ selector: 'ds-editable-item-selector', templateUrl: '../dso-selector.component.html', styleUrls: ['../dso-selector.component.scss'] }) +/** + * Component rendering a list of items that are editable by the current user. + */ export class EditableItemSelectorComponent extends DSOSelectorComponent { constructor( @@ -32,12 +36,19 @@ export class EditableItemSelectorComponent extends DSOSelectorComponent { super(searchService, notificationsService, translate, dsoNameService); } + /* + * Find the list of items that can be edited by the current user. + */ search(query: string, page: number): Observable>>> { - return this.itemDataService.findItemsWithEdit(followLink('owningCollection')).pipe( + const findOptions: FindListOptions = { + currentPage: page, + elementsPerPage: this.defaultPagination.pageSize + }; + return this.itemDataService.findItemsWithEdit(findOptions, false, false, followLink('owningCollection')).pipe( getFirstCompletedRemoteData(), - tap((rdata) => console.log('TEST:', rdata)), + tap((rdata) => console.log('TAPPEDITAP:', rdata)), // XXX mapRemoteDataPayload((payload) => hasValue(payload) ? buildPaginatedList(payload.pageInfo, payload.page.map((item) => Object.assign(new ItemSearchResult(), { indexableObject: item })))