Skip to content
Snippets Groups Projects
Commit 2053078a authored by Kristof De Langhe's avatar Kristof De Langhe
Browse files

55946: Dynamic reloading of item mapper on item-level

parent 55e37a63
Branches
Tags
No related merge requests found
......@@ -68,6 +68,10 @@ export class CollectionItemMapperComponent implements OnInit {
*/
defaultSortOptions: SortOptions = new SortOptions('dc.title', SortDirection.ASC);
/**
* Firing this observable (shouldUpdate$.next(true)) forces the two lists to reload themselves
* Usually fired after the lists their cache is cleared (to force a new request to the REST API)
*/
shouldUpdate$: BehaviorSubject<boolean>;
constructor(private route: ActivatedRoute,
......@@ -138,6 +142,16 @@ export class CollectionItemMapperComponent implements OnInit {
)
);
this.showNotifications(responses$, remove);
this.clearRequestCache();
}
/**
* Display notifications
* @param {Observable<RestResponse[]>} responses$ The responses after adding/removing a mapping
* @param {boolean} remove Whether or not the goal was to remove mappings
*/
private showNotifications(responses$: Observable<RestResponse[]>, remove?: boolean) {
const messageInsertion = remove ? 'unmap' : 'map';
responses$.subscribe((responses: RestResponse[]) => {
......@@ -163,9 +177,15 @@ export class CollectionItemMapperComponent implements OnInit {
this.notificationsService.error(head, content);
});
}
// Force an update on all lists
this.shouldUpdate$.next(true);
});
}
/**
* Clear all previous requests from cache in preparation of refreshing all lists
*/
private clearRequestCache() {
this.collectionRD$.pipe(take(1)).subscribe((collectionRD: RemoteData<Collection>) => {
this.collectionDataService.clearMappingItemsRequests(collectionRD.payload.id);
this.searchService.clearDiscoveryRequests();
......
......@@ -11,13 +11,14 @@ import { getSucceededRemoteData, toDSpaceObjectListRD } from '../../../core/shar
import { ActivatedRoute, Router } from '@angular/router';
import { SearchService } from '../../../+search-page/search-service/search.service';
import { SearchConfigurationService } from '../../../+search-page/search-service/search-configuration.service';
import { map, switchMap } from 'rxjs/operators';
import { map, switchMap, take } from 'rxjs/operators';
import { ItemDataService } from '../../../core/data/item-data.service';
import { TranslateService } from '@ngx-translate/core';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { DSpaceObjectType } from '../../../core/shared/dspace-object-type.model';
import { isNotEmpty } from '../../../shared/empty.util';
import { RestResponse } from '../../../core/cache/response.models';
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
@Component({
selector: 'ds-item-collection-mapper',
......@@ -55,6 +56,12 @@ export class ItemCollectionMapperComponent implements OnInit {
*/
mappingCollectionsRD$: Observable<RemoteData<PaginatedList<Collection>>>;
/**
* Firing this observable (shouldUpdate$.next(true)) forces the two lists to reload themselves
* Usually fired after the lists their cache is cleared (to force a new request to the REST API)
*/
shouldUpdate$: BehaviorSubject<boolean>;
constructor(private route: ActivatedRoute,
private router: Router,
private searchConfigService: SearchConfigurationService,
......@@ -76,8 +83,13 @@ export class ItemCollectionMapperComponent implements OnInit {
* TODO: When the API support it, fetch collections excluding the item's scope (currently fetches all collections)
*/
loadCollectionLists() {
this.itemCollectionsRD$ = this.itemRD$.pipe(
map((itemRD: RemoteData<Item>) => itemRD.payload),
this.shouldUpdate$ = new BehaviorSubject<boolean>(true);
this.itemCollectionsRD$ = observableCombineLatest(this.itemRD$, this.shouldUpdate$).pipe(
map(([itemRD, shouldUpdate]) => {
if (shouldUpdate) {
return itemRD.payload
}
}),
switchMap((item: Item) => this.itemDataService.getMappedCollections(item.id))
);
......@@ -120,6 +132,7 @@ export class ItemCollectionMapperComponent implements OnInit {
);
this.showNotifications(responses$, 'item.edit.item-mapper.notifications.add');
this.clearRequestCache();
}
/**
......@@ -135,6 +148,7 @@ export class ItemCollectionMapperComponent implements OnInit {
);
this.showNotifications(responses$, 'item.edit.item-mapper.notifications.remove');
this.clearRequestCache();
}
/**
......@@ -176,6 +190,18 @@ export class ItemCollectionMapperComponent implements OnInit {
this.notificationsService.error(head, content);
});
}
// Force an update on all lists
this.shouldUpdate$.next(true);
});
}
/**
* Clear all previous requests from cache in preparation of refreshing all lists
*/
private clearRequestCache() {
this.itemRD$.pipe(take(1)).subscribe((itemRD: RemoteData<Item>) => {
this.itemDataService.clearMappedCollectionsRequests(itemRD.payload.id);
this.searchService.clearDiscoveryRequests();
});
}
......
......@@ -322,6 +322,9 @@ export class SearchService implements OnDestroy {
return '/' + g.toString();
}
/**
* Clear all request cache related to discovery objects
*/
clearDiscoveryRequests() {
this.halService.getEndpoint(this.searchLinkPath).pipe(take(1)).subscribe((href: string) => {
this.requestService.removeByHrefSubstring(href);
......
import { Injectable } from '@angular/core';
import { Action, Store } from '@ngrx/store';
import { Store } from '@ngrx/store';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { NormalizedCollection } from '../cache/models/normalized-collection.model';
import { ObjectCacheService } from '../cache/object-cache.service';
......@@ -21,9 +21,6 @@ import { GenericConstructor } from '../shared/generic-constructor';
import { ResponseParsingService } from './parsing.service';
import { DSpaceObject } from '../shared/dspace-object.model';
import { DSOResponseParsingService } from './dso-response-parsing.service';
import { IndexName, IndexState } from '../index/index.reducer';
import { IndexActionTypes, RemoveFromIndexBySubstringAction } from '../index/index.actions';
import { Actions, ofType } from '@ngrx/effects';
@Injectable()
export class CollectionDataService extends ComColDataService<NormalizedCollection, Collection> {
......@@ -35,8 +32,7 @@ export class CollectionDataService extends ComColDataService<NormalizedCollectio
protected store: Store<CoreState>,
protected cds: CommunityDataService,
protected halService: HALEndpointService,
protected objectCache: ObjectCacheService,
protected indexStore: Store<IndexState>
protected objectCache: ObjectCacheService
) {
super();
}
......
import { distinctUntilChanged, map, filter, switchMap, tap } from 'rxjs/operators';
import { distinctUntilChanged, map, filter, switchMap, tap, take } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
......@@ -102,4 +102,10 @@ export class ItemDataService extends DataService<NormalizedItem, Item> {
return this.rdbService.toRemoteDataObservable(requestEntry$, payload$);
}
public clearMappedCollectionsRequests(itemId: string) {
this.getMappingCollectionsEndpoint(itemId).pipe(take(1)).subscribe((href: string) => {
this.requestService.removeByHrefSubstring(href);
});
}
}
......@@ -139,6 +139,11 @@ export class RequestService {
}
}
/**
* Remove all request cache providing (part of) the href
* This also includes href-to-uuid index cache
* @param href A substring of the request(s) href
*/
removeByHrefSubstring(href: string) {
this.store.pipe(
select(this.uuidsFromHrefSubstringSelector(pathSelector<CoreState, IndexState>(coreSelector, 'index'), IndexName.REQUEST, href)),
......@@ -152,6 +157,10 @@ export class RequestService {
this.indexStore.dispatch(new RemoveFromIndexBySubstringAction(IndexName.REQUEST, href));
}
/**
* Remove request cache using the request's UUID
* @param uuid
*/
removeByUuid(uuid: string) {
this.store.dispatch(new RequestRemoveAction(uuid));
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment