64961: Edit bitstream observable fixes

This commit is contained in:
Kristof De Langhe
2019-09-25 16:12:44 +02:00
parent f152cad1fa
commit 16223dfedc

View File

@@ -1,7 +1,7 @@
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core'; import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
import { Bitstream } from '../../core/shared/bitstream.model'; import { Bitstream } from '../../core/shared/bitstream.model';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { map, switchMap, take, tap } from 'rxjs/operators'; import { filter, map, switchMap, take, tap } from 'rxjs/operators';
import { combineLatest as observableCombineLatest, of as observableOf } from 'rxjs'; import { combineLatest as observableCombineLatest, of as observableOf } from 'rxjs';
import { Subscription } from 'rxjs/internal/Subscription'; import { Subscription } from 'rxjs/internal/Subscription';
import { import {
@@ -19,6 +19,7 @@ import { DynamicCustomSwitchModel } from '../../shared/form/builder/ds-dynamic-f
import { cloneDeep } from 'lodash'; import { cloneDeep } from 'lodash';
import { BitstreamDataService } from '../../core/data/bitstream-data.service'; import { BitstreamDataService } from '../../core/data/bitstream-data.service';
import { import {
getAllSucceededRemoteData,
getFirstSucceededRemoteDataPayload, getFirstSucceededRemoteDataPayload,
getRemoteDataPayload, getRemoteDataPayload,
getSucceededRemoteData getSucceededRemoteData
@@ -28,7 +29,7 @@ import { BitstreamFormatDataService } from '../../core/data/bitstream-format-dat
import { BitstreamFormat } from '../../core/shared/bitstream-format.model'; import { BitstreamFormat } from '../../core/shared/bitstream-format.model';
import { BitstreamFormatSupportLevel } from '../../core/shared/bitstream-format-support-level'; import { BitstreamFormatSupportLevel } from '../../core/shared/bitstream-format-support-level';
import { RestResponse } from '../../core/cache/response.models'; import { RestResponse } from '../../core/cache/response.models';
import { hasValue, isNotEmpty } from '../../shared/empty.util'; import { hasValue, isNotEmpty, isNotEmptyOperator } from '../../shared/empty.util';
import { Metadata } from '../../core/shared/metadata.utils'; import { Metadata } from '../../core/shared/metadata.utils';
import { Location } from '@angular/common'; import { Location } from '@angular/common';
@@ -245,9 +246,15 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
formGroup: FormGroup; formGroup: FormGroup;
/** /**
* The subscription on the bitstream * Array to track all subscriptions and unsubscribe them onDestroy
* @type {Array}
*/ */
sub: Subscription; protected subs: Subscription[] = [];
/**
* Denotes whether or not we're awaiting the server's response after a save
*/
private _saveResponsePending: boolean;
constructor(private route: ActivatedRoute, constructor(private route: ActivatedRoute,
private location: Location, private location: Location,
@@ -255,8 +262,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
private translate: TranslateService, private translate: TranslateService,
private bitstreamService: BitstreamDataService, private bitstreamService: BitstreamDataService,
private notificationsService: NotificationsService, private notificationsService: NotificationsService,
private bitstreamFormatService: BitstreamFormatDataService, private bitstreamFormatService: BitstreamFormatDataService) {
private changeDetectorRef: ChangeDetectorRef) {
} }
/** /**
@@ -271,31 +277,47 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
const bitstream$ = this.route.data.pipe( const bitstream$ = this.route.data.pipe(
map((data) => data.bitstream), map((data) => data.bitstream),
getSucceededRemoteData(), getSucceededRemoteData(),
getRemoteDataPayload() getRemoteDataPayload(),
switchMap((bitstream: Bitstream) => this.bitstreamService.findById(bitstream.id).pipe(
getAllSucceededRemoteData(),
getRemoteDataPayload(),
filter((bs: Bitstream) => hasValue(bs)))
)
); );
const allFormats$ = this.bitstreamFormatService.findAll(this.findAllOptions).pipe( const allFormats$ = this.bitstreamFormatService.findAll(this.findAllOptions).pipe(
getSucceededRemoteData(), getSucceededRemoteData(),
getRemoteDataPayload() getRemoteDataPayload()
); );
this.sub = observableCombineLatest( this.subs.push(
bitstream$, observableCombineLatest(
allFormats$ bitstream$,
).subscribe(([bitstream, allFormats]) => { allFormats$
this.bitstream = bitstream as Bitstream; ).subscribe(([bitstream, allFormats]) => {
this.formats = allFormats.page; this.bitstream = bitstream as Bitstream;
this.updateFormatModel(); this.formats = allFormats.page;
this.updateForm(this.bitstream); this.updateFormatModel();
this.changeDetectorRef.detectChanges(); this.updateForm(this.bitstream);
}); if (this._saveResponsePending) {
this.notificationsService.success(
this.translate.instant(this.NOTIFICATIONS_PREFIX + 'saved.title'),
this.translate.instant(this.NOTIFICATIONS_PREFIX + 'saved.content')
);
this._saveResponsePending = false;
}
})
);
this.updateFieldTranslations(); this.updateFieldTranslations();
this.translate.onLangChange
.subscribe(() => { this.subs.push(
this.translate.onLangChange
.subscribe(() => {
this.updateFieldTranslations(); this.updateFieldTranslations();
}); })
this.changeDetectorRef.detectChanges(); );
} };
/** /**
* Update the current form values with bitstream properties * Update the current form values with bitstream properties
@@ -417,7 +439,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
); );
} }
}) })
) );
} else { } else {
bitstream$ = observableOf(this.bitstream); bitstream$ = observableOf(this.bitstream);
} }
@@ -430,32 +452,13 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
} }
return this.bitstreamService.update(this.bitstream).pipe( return this.bitstreamService.update(this.bitstream).pipe(
getFirstSucceededRemoteDataPayload(), getFirstSucceededRemoteDataPayload()
switchMap(() => {
this.bitstreamService.commitUpdates();
return this.bitstreamService.findById(this.bitstream.id).pipe(
getFirstSucceededRemoteDataPayload()
);
})
); );
}) })
).subscribe((bitstream: Bitstream) => { ).subscribe(() => {
this.onSuccess(bitstream); this.bitstreamService.commitUpdates();
this._saveResponsePending = true;
}); });
}
/**
* Display notifications and update the form upon success
* @param bitstream
*/
onSuccess(bitstream: Bitstream) {
this.bitstream = bitstream;
this.updateForm(this.bitstream);
this.notificationsService.success(
this.translate.instant(this.NOTIFICATIONS_PREFIX + 'saved.title'),
this.translate.instant(this.NOTIFICATIONS_PREFIX + 'saved.content')
);
} }
/** /**
@@ -478,9 +481,9 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
* Unsubscribe from open subscriptions * Unsubscribe from open subscriptions
*/ */
ngOnDestroy(): void { ngOnDestroy(): void {
if (this.sub) { this.subs
this.sub.unsubscribe(); .filter((subscription) => hasValue(subscription))
} .forEach((subscription) => subscription.unsubscribe());
} }
} }