63945: Bitstream delete notifications, de-caching, reloading and small layout changes

This commit is contained in:
Kristof De Langhe
2019-07-26 13:28:32 +02:00
parent f93a104d68
commit 27ec828142
6 changed files with 182 additions and 41 deletions

View File

@@ -1,8 +1,8 @@
import { Component, Inject, OnDestroy } from '@angular/core';
import { ChangeDetectorRef, Component, Inject, OnDestroy } from '@angular/core';
import { AbstractItemUpdateComponent } from '../abstract-item-update/abstract-item-update.component';
import { map, switchMap, take } from 'rxjs/operators';
import { filter, map, switchMap, take } from 'rxjs/operators';
import { Bitstream } from '../../../core/shared/bitstream.model';
import { getBundleNames, toBitstreamsArray, toBundleMap } from '../../../core/shared/item-bitstreams-utils';
import { toBitstreamsArray, toBundleMap } from '../../../core/shared/item-bitstreams-utils';
import { Observable } from 'rxjs/internal/Observable';
import { FieldUpdate, FieldUpdates } from '../../../core/data/object-updates/object-updates.reducer';
import { Subscription } from 'rxjs/internal/Subscription';
@@ -16,7 +16,14 @@ import { BitstreamDataService } from '../../../core/data/bitstream-data.service'
import { FieldChangeType } from '../../../core/data/object-updates/object-updates.actions';
import { isNotEmptyOperator } from '../../../shared/empty.util';
import { zip as observableZip } from 'rxjs';
import { RestResponse } from '../../../core/cache/response.models';
import { ErrorResponse, RestResponse } from '../../../core/cache/response.models';
import { ObjectCacheService } from '../../../core/cache/object-cache.service';
import { RequestService } from '../../../core/data/request.service';
import { getSucceededRemoteData } from '../../../core/shared/operators';
import { Item } from '../../../core/shared/item.model';
import { RemoteData } from '../../../core/data/remote-data';
export const ORIGINAL_BUNDLE = 'ORIGINAL';
@Component({
selector: 'ds-item-bitstreams',
@@ -28,12 +35,24 @@ import { RestResponse } from '../../../core/cache/response.models';
*/
export class ItemBitstreamsComponent extends AbstractItemUpdateComponent implements OnDestroy {
bundleNames$: Observable<string[]>;
/**
* A Map of the current item's bundles and respective FieldUpdates
* key: Bundle Name
* value: FieldUpdates about the bundle's bitstreams
*/
updatesMap: Map<string, Observable<FieldUpdates>>;
/**
* A subscription keeping the updatesMap up-to-date
*/
updatesMapSub: Subscription;
/**
* A subscription that checks when the item is deleted in cache and reloads the item by sending a new request
* This is used to update the item in cache after bitstreams are deleted
*/
itemUpdateSubscription: Subscription;
constructor(
public itemService: ItemDataService,
public objectUpdatesService: ObjectUpdatesService,
@@ -42,7 +61,10 @@ export class ItemBitstreamsComponent extends AbstractItemUpdateComponent impleme
public translateService: TranslateService,
@Inject(GLOBAL_CONFIG) public EnvConfig: GlobalConfig,
public route: ActivatedRoute,
public bitstreamService: BitstreamDataService
public bitstreamService: BitstreamDataService,
public objectCache: ObjectCacheService,
public requestService: RequestService,
public cdRef: ChangeDetectorRef
) {
super(itemService, objectUpdatesService, router, notificationsService, translateService, EnvConfig, route);
}
@@ -52,13 +74,19 @@ export class ItemBitstreamsComponent extends AbstractItemUpdateComponent impleme
*/
ngOnInit(): void {
super.ngOnInit();
this.bundleNames$ = this.item.bitstreams.pipe(getBundleNames());
this.initializeItemUpdate();
}
/**
* Initialize the notification messages prefix
*/
initializeNotificationsPrefix(): void {
this.notificationsPrefix = 'item.edit.bitstreams.notifications.';
}
/**
* Initialize the original fields for the object-updates-service
*/
initializeOriginalFields(): void {
this.item.bitstreams.pipe(
toBitstreamsArray(),
@@ -68,6 +96,9 @@ export class ItemBitstreamsComponent extends AbstractItemUpdateComponent impleme
});
}
/**
* Initialize field updates
*/
initializeUpdates(): void {
this.updates$ = this.item.bitstreams.pipe(
toBitstreamsArray(),
@@ -80,10 +111,36 @@ export class ItemBitstreamsComponent extends AbstractItemUpdateComponent impleme
bundleMap.forEach((bitstreams: Bitstream[], bundleName: string) => {
updatesMap.set(bundleName, this.objectUpdatesService.getFieldUpdatesExclusive(this.url, bitstreams));
});
if (updatesMap.size === 0) {
// Add an ORIGINAL bundle by default if the item doesn't contain any bitstreams
updatesMap.set(ORIGINAL_BUNDLE, undefined);
}
this.updatesMap = updatesMap;
});
}
/**
* Update the item (and view) when it's removed in the request cache
* Also re-initialize the original fields and updates
*/
initializeItemUpdate(): void {
this.itemUpdateSubscription = this.requestService.hasByHrefObservable(this.item.self).pipe(
filter((exists: boolean) => !exists),
switchMap(() => this.itemService.findById(this.item.uuid)),
getSucceededRemoteData(),
).subscribe((itemRD: RemoteData<Item>) => {
this.item = itemRD.payload;
this.initializeOriginalFields();
this.initializeUpdates();
this.cdRef.detectChanges();
});
}
/**
* Submit the current changes
* Bitstreams marked as deleted send out a delete request to the rest API
* Display notifications and reset the current item/updates
*/
submit() {
const removedBitstreams$ = this.item.bitstreams.pipe(
toBitstreamsArray(),
@@ -94,11 +151,53 @@ export class ItemBitstreamsComponent extends AbstractItemUpdateComponent impleme
);
removedBitstreams$.pipe(
take(1),
switchMap((removedBistreams: Bitstream[]) => observableZip(...removedBistreams.map((bitstream: Bitstream) => this.bitstreamService.delete(bitstream))))
).subscribe();
switchMap((removedBistreams: Bitstream[]) => observableZip(...removedBistreams.map((bitstream: Bitstream) => this.bitstreamService.deleteAndReturnResponse(bitstream))))
).subscribe((responses: RestResponse[]) => {
this.displayNotifications(responses);
this.reset();
});
}
/**
* Display notifications
* - Error notification for each failed response with their message
* - Success notification in case there's at least one successful response
* @param responses
*/
displayNotifications(responses: RestResponse[]) {
const failedResponses = responses.filter((response: RestResponse) => !response.isSuccessful);
const successfulResponses = responses.filter((response: RestResponse) => response.isSuccessful);
failedResponses.forEach((response: ErrorResponse) => {
this.notificationsService.error(this.getNotificationTitle('failed'), response.errorMessage);
});
if (successfulResponses.length > 0) {
this.notificationsService.success(this.getNotificationTitle('saved'), this.getNotificationContent('saved'));
}
}
/**
* De-cache the current item (it should automatically reload due to itemUpdateSubscription)
*/
reset() {
this.updatesMap = undefined;
this.refreshItemCache();
this.initializeItemUpdate();
}
/**
* Remove the current item's cache from object- and request-cache
*/
refreshItemCache() {
this.objectCache.remove(this.item.self);
this.requestService.removeByHrefSubstring(this.item.self);
}
/**
* Unsubscribe from open subscriptions whenever the component gets destroyed
*/
ngOnDestroy(): void {
this.updatesMapSub.unsubscribe();
this.itemUpdateSubscription.unsubscribe();
}
}