55693: Notifications on success/failure and small refactoring

This commit is contained in:
Kristof De Langhe
2018-09-27 17:08:52 +02:00
parent 6f64b943f5
commit 7b74d9c077
2 changed files with 27 additions and 19 deletions

View File

@@ -9,8 +9,8 @@ import { SearchConfigurationService } from '../../+search-page/search-service/se
import { PaginatedSearchOptions } from '../../+search-page/paginated-search-options.model'; import { PaginatedSearchOptions } from '../../+search-page/paginated-search-options.model';
import { PaginatedList } from '../../core/data/paginated-list'; import { PaginatedList } from '../../core/data/paginated-list';
import { Item } from '../../core/shared/item.model'; import { Item } from '../../core/shared/item.model';
import { combineLatest, flatMap, map, tap } from 'rxjs/operators'; import { combineLatest, filter, flatMap, map, switchMap, take, tap } from 'rxjs/operators';
import { getSucceededRemoteData, toDSpaceObjectListRD } from '../../core/shared/operators'; import { filterSuccessfulResponses, getSucceededRemoteData, toDSpaceObjectListRD } from '../../core/shared/operators';
import { SearchService } from '../../+search-page/search-service/search.service'; import { SearchService } from '../../+search-page/search-service/search.service';
import { DSpaceObject } from '../../core/shared/dspace-object.model'; import { DSpaceObject } from '../../core/shared/dspace-object.model';
import { DSpaceObjectType } from '../../core/shared/dspace-object-type.model'; import { DSpaceObjectType } from '../../core/shared/dspace-object-type.model';
@@ -18,6 +18,7 @@ import { SortDirection, SortOptions } from '../../core/cache/models/sort-options
import { NotificationsService } from '../../shared/notifications/notifications.service'; import { NotificationsService } from '../../shared/notifications/notifications.service';
import { ItemDataService } from '../../core/data/item-data.service'; import { ItemDataService } from '../../core/data/item-data.service';
import { forkJoin } from 'rxjs/observable/forkJoin'; import { forkJoin } from 'rxjs/observable/forkJoin';
import { RestResponse } from '../../core/cache/response-cache.models';
@Component({ @Component({
selector: 'ds-collection-item-mapper', selector: 'ds-collection-item-mapper',
@@ -47,12 +48,16 @@ export class CollectionItemMapperComponent implements OnInit {
} }
ngOnInit(): void { ngOnInit(): void {
this.collectionRD$ = this.route.data.map((data) => data.collection); this.collectionRD$ = this.route.data.map((data) => data.collection).pipe(getSucceededRemoteData()) as Observable<RemoteData<Collection>>;
this.searchOptions$ = this.searchConfigService.paginatedSearchOptions; this.searchOptions$ = this.searchConfigService.paginatedSearchOptions;
this.collectionItemsRD$ = this.collectionRD$.pipe(
getSucceededRemoteData(), const collectionAndOptions$ = Observable.combineLatest(
combineLatest(this.searchOptions$), this.collectionRD$,
flatMap(([collectionRD, options]) => { this.searchOptions$
);
this.collectionItemsRD$ = collectionAndOptions$.pipe(
switchMap(([collectionRD, options]) => {
return this.searchService.search(Object.assign(options, { return this.searchService.search(Object.assign(options, {
scope: collectionRD.payload.id, scope: collectionRD.payload.id,
dsoType: DSpaceObjectType.ITEM, dsoType: DSpaceObjectType.ITEM,
@@ -75,15 +80,21 @@ export class CollectionItemMapperComponent implements OnInit {
} }
mapItems(ids: string[]) { mapItems(ids: string[]) {
const responses = this.collectionRD$.pipe( const responses$ = this.collectionRD$.pipe(
map((collectionRD: RemoteData<Collection>) => collectionRD.payload), getSucceededRemoteData(),
flatMap((collection: Collection) => forkJoin(ids.map((id: string) => this.itemDataService.mapToCollection(id, collection.id)))) map((collectionRD: RemoteData<Collection>) => collectionRD.payload.id),
switchMap((collectionId: string) => Observable.combineLatest(ids.map((id: string) => this.itemDataService.mapToCollection(id, collectionId))))
); );
responses.subscribe((value) => console.log(value)); responses$.subscribe((responses: RestResponse[]) => {
const successful = responses.filter((response: RestResponse) => response.isSuccessful);
this.collectionRD$.subscribe((collectionRD: RemoteData<Collection>) => { const unsuccessful = responses.filter((response: RestResponse) => !response.isSuccessful);
this.notificationsService.success('Mapping completed', `Successfully mapped ${ids.length} items to collection "${collectionRD.payload.name}".`); if (successful.length > 0) {
this.notificationsService.success('Mapping completed', `Successfully mapped ${successful.length} items.`);
}
if (unsuccessful.length > 0) {
this.notificationsService.error('Mapping errors', `Errors occurred for mapping of ${unsuccessful.length} items.`);
}
}); });
} }

View File

@@ -60,14 +60,11 @@ export class ItemDataService extends DataService<NormalizedItem, Item> {
} }
public mapToCollection(itemId: string, collectionId: string): Observable<RestResponse> { public mapToCollection(itemId: string, collectionId: string): Observable<RestResponse> {
const request$ = this.getMappingCollectionsEndpoint(itemId, collectionId).pipe( return this.getMappingCollectionsEndpoint(itemId, collectionId).pipe(
isNotEmptyOperator(), isNotEmptyOperator(),
distinctUntilChanged(), distinctUntilChanged(),
map((endpointURL: string) => new PostRequest(this.requestService.generateRequestId(), endpointURL)), map((endpointURL: string) => new PostRequest(this.requestService.generateRequestId(), endpointURL)),
configureRequest(this.requestService) configureRequest(this.requestService),
);
return request$.pipe(
map((request: RestRequest) => request.href), map((request: RestRequest) => request.href),
getResponseFromSelflink(this.responseCache), getResponseFromSelflink(this.responseCache),
map((responseCacheEntry: ResponseCacheEntry) => responseCacheEntry.response) map((responseCacheEntry: ResponseCacheEntry) => responseCacheEntry.response)