diff --git a/src/app/core/data/data.service.ts b/src/app/core/data/data.service.ts index e91143a8cf..fc4da69a5c 100644 --- a/src/app/core/data/data.service.ts +++ b/src/app/core/data/data.service.ts @@ -161,7 +161,6 @@ export abstract class DataService { } findByHref(href: string, options?: HttpOptions): Observable> { - console.log('perform request for:', href) this.requestService.configure(new GetRequest(this.requestService.generateRequestId(), href, null, options), this.forceBypassCache); return this.rdbService.buildSingle(href); } diff --git a/src/app/core/data/relationship.service.ts b/src/app/core/data/relationship.service.ts index b93f4f6ea4..a8e8874dfe 100644 --- a/src/app/core/data/relationship.service.ts +++ b/src/app/core/data/relationship.service.ts @@ -68,7 +68,7 @@ export class RelationshipService extends DataService { deleteRelationship(id: string): Observable { return this.getRelationshipEndpoint(id).pipe( isNotEmptyOperator(), - distinctUntilChanged(), + take(1), map((endpointURL: string) => new DeleteRequest(this.requestService.generateRequestId(), endpointURL)), configureRequest(this.requestService), switchMap((restRequest: RestRequest) => this.requestService.getByUUID(restRequest.uuid)), @@ -84,7 +84,7 @@ export class RelationshipService extends DataService { options.headers = headers; return this.halService.getEndpoint(this.linkPath).pipe( isNotEmptyOperator(), - distinctUntilChanged(), + take(1), map((endpointUrl: string) => `${endpointUrl}?relationshipType=${typeId}`), map((endpointURL: string) => new PostRequest(this.requestService.generateRequestId(), endpointURL, `${item1.self} \n ${item2.self}`, options)), configureRequest(this.requestService), @@ -267,7 +267,7 @@ export class RelationshipService extends DataService { ); })) }), - map((relationships: Relationship[]) => relationships.find((relationship => hasValue(relationship)))), + map((relationships: Relationship[]) => relationships.find((relationship => hasValue(relationship)))) ) } diff --git a/src/app/core/data/request.service.ts b/src/app/core/data/request.service.ts index 775118dbc0..381fbd58c5 100644 --- a/src/app/core/data/request.service.ts +++ b/src/app/core/data/request.service.ts @@ -308,6 +308,7 @@ export class RequestService { */ hasByHref(href: string): boolean { let result = false; + /* NB: that this is only a solution because the select method is synchronous, see: https://github.com/ngrx/store/issues/296#issuecomment-269032571*/ this.getByHref(href).pipe( take(1) ).subscribe((requestEntry: RequestEntry) => result = this.isValid(requestEntry)); diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.html b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.html index 2039a86d0f..a38f557219 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.html +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.html @@ -44,15 +44,16 @@
-
diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.ts index e22a9a442d..22343f67b3 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/dynamic-lookup-relation-modal.component.ts @@ -2,13 +2,13 @@ import { Component, OnDestroy, OnInit } from '@angular/core'; import { PaginatedList } from '../../../../../core/data/paginated-list'; import { SearchResult } from '../../../../search/search-result.model'; import { RemoteData } from '../../../../../core/data/remote-data'; -import { Observable, ReplaySubject } from 'rxjs'; +import { from, Observable, ReplaySubject } from 'rxjs'; import { SearchService } from '../../../../../core/shared/search/search.service'; import { PaginatedSearchOptions } from '../../../../search/paginated-search-options.model'; import { DSpaceObject } from '../../../../../core/shared/dspace-object.model'; import { PaginationComponentOptions } from '../../../../pagination/pagination-component-options.model'; import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; -import { hasValue, isNotEmpty } from '../../../../empty.util'; +import { hasValue, hasValueOperator, isNotEmpty } from '../../../../empty.util'; import { concat, map, mergeMap, multicast, switchMap, take, takeWhile, tap } from 'rxjs/operators'; import { Router } from '@angular/router'; import { SEARCH_CONFIG_SERVICE } from '../../../../../+my-dspace-page/my-dspace-page.component'; @@ -117,12 +117,24 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy }); } - selectPage(page: SearchResult[]) { + selectPage(page: SearchResult[]) { + this.selection$ + .pipe(take(1)) + .subscribe((selection: SearchResult[]) => { + const filteredPage = page.filter((pageItem) => selection.findIndex((selected) => selected.equals(pageItem)) < 0) + this.select(...filteredPage); + }); this.selectableListService.select(this.listId, page); } - deselectPage(page: SearchResult[]) { + deselectPage(page: SearchResult[]) { this.allSelected = false; + this.selection$ + .pipe(take(1)) + .subscribe((selection: SearchResult[]) => { + const filteredPage = page.filter((pageItem) => selection.findIndex((selected) => selected.equals(pageItem)) >= 0) + this.deselect(...filteredPage); + }); this.selectableListService.deselect(this.listId, page); } @@ -132,62 +144,84 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy const fullPagination = Object.assign(new PaginationComponentOptions(), { query: this.searchQuery, currentPage: 1, - pageSize: Number.POSITIVE_INFINITY + pageSize: 9999 }); const fullSearchConfig = Object.assign(this.searchConfig, { pagination: fullPagination }); - const results = this.searchService.search(fullSearchConfig); - results.pipe( + const results$ = this.searchService.search(fullSearchConfig) as Observable>>>; + results$.pipe( getSucceededRemoteData(), map((resultsRD) => resultsRD.payload.page), tap(() => this.selectAllLoading = false), - ).subscribe((results) => - this.selectableListService.select(this.listId, results) + ).subscribe((results) => { + this.selection$ + .pipe(take(1)) + .subscribe((selection: SearchResult[]) => { + const filteredResults = results.filter((pageItem) => selection.findIndex((selected) => selected.equals(pageItem)) < 0); + this.select(...filteredResults); + }); + this.selectableListService.select(this.listId, results); + } ); } deselectAll() { this.allSelected = false; + this.selection$ + .pipe(take(1)) + .subscribe((selection: SearchResult[]) => this.deselect(...selection)); this.selectableListService.deselectAll(this.listId); } - select(selectableObject: SearchResult) { + select(...selectableObjects: SearchResult[]) { setTimeout(() => this.itemRD$ .pipe( getSucceededRemoteData(), - mergeMap((itemRD: RemoteData) => { + switchMap((itemRD: RemoteData) => { const type1: string = itemRD.payload.firstMetadataValue('relationship.type'); - const type2: string = selectableObject.indexableObject.firstMetadataValue('relationship.type'); - return this.relationshipTypeService.getRelationshipTypeByLabelAndTypes(this.relationship.relationshipType, type1, type2) - .pipe( - mergeMap((type: RelationshipType) => { - const isSwitched = type.rightLabel === this.relationship.relationshipType; - if (isSwitched) { - return this.relationshipService.addRelationship(type.id, selectableObject.indexableObject, itemRD.payload); - } else { - return this.relationshipService.addRelationship(type.id, itemRD.payload, selectableObject.indexableObject); - } - } - ) - ); + return from(selectableObjects).pipe( + mergeMap((object) => { + const type2: string = object.indexableObject.firstMetadataValue('relationship.type'); + return this.relationshipTypeService.getRelationshipTypeByLabelAndTypes(this.relationship.relationshipType, type1, type2) + .pipe( + mergeMap((type: RelationshipType) => { + const isSwitched = type.rightLabel === this.relationship.relationshipType; + if (isSwitched) { + return this.relationshipService.addRelationship(type.id, object.indexableObject, itemRD.payload); + } else { + return this.relationshipService.addRelationship(type.id, itemRD.payload, object.indexableObject); + } + } + ) + ).pipe(take(1)); + })); }), - take(1) - ) - .subscribe(), 0); + ).subscribe(), 0 + ); } - deselect(selectableObject: SearchResult) { + deselect(...selectableObjects: SearchResult[]) { setTimeout(() => this.itemRD$.pipe( getSucceededRemoteData(), - switchMap((itemRD: RemoteData) => this.relationshipService.getRelationshipByItemsAndLabel(itemRD.payload, selectableObject.indexableObject, this.relationship.relationshipType)), - switchMap((relationship: Relationship) => this.relationshipService.deleteRelationship(relationship.id)), + switchMap((itemRD: RemoteData) => { + return from(selectableObjects).pipe( + tap(t => console.log(itemRD)), + mergeMap((object) => this.relationshipService.getRelationshipByItemsAndLabel(itemRD.payload, object.indexableObject, this.relationship.relationshipType)), + take(1), + hasValueOperator(), + mergeMap((relationship: Relationship) => this.relationshipService.deleteRelationship(relationship.id)) + ) + }), take(1), ).subscribe(), 0); } - ngOnDestroy(): void { - if (hasValue(this.subscription)) { + ngOnDestroy() + : + void { + if (hasValue(this.subscription) + ) { this.subscription.unsubscribe(); } }