Skip to content

Commit 1c93e4f

Browse files
committed
115491: Simplify update logic
Moved the creation of the update observables to their own methods and unified the handling of their results in a single subscription
1 parent 23a29ba commit 1c93e4f

1 file changed

Lines changed: 93 additions & 71 deletions

File tree

src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.ts

Lines changed: 93 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
22
import { Bitstream } from '../../core/shared/bitstream.model';
33
import { ActivatedRoute, Router } from '@angular/router';
4-
import { map, switchMap, tap } from 'rxjs/operators';
4+
import { map, switchMap } from 'rxjs/operators';
55
import {
66
BehaviorSubject, combineLatest, combineLatest as observableCombineLatest, Observable, of as observableOf, Subscription
77
} from 'rxjs';
@@ -661,84 +661,106 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
661661
*/
662662
onSubmit() {
663663
const updatedValues = this.formGroup.getRawValue();
664+
665+
const metadataUpdateRD$ = this.updateBitstreamMetadataRD$(updatedValues);
666+
const primaryUpdateRD$ = this.updatePrimaryBitstreamRD$(updatedValues);
667+
const formatUpdateRD$ = this.updateBitstreamFormatRD$(updatedValues);
668+
669+
this.subs.push(combineLatest([metadataUpdateRD$, primaryUpdateRD$, formatUpdateRD$])
670+
.subscribe(([metadataUpdateRD, primaryUpdateRD, formatUpdateRD]) => {
671+
let errorWhileSaving = false;
672+
673+
// Check for errors during the primary bitstream update
674+
if (hasValue(primaryUpdateRD) && primaryUpdateRD.hasFailed) {
675+
this.notificationsService.error(
676+
this.translate.instant(NOTIFICATIONS_PREFIX + 'error.primaryBitstream.title'),
677+
primaryUpdateRD.errorMessage
678+
);
679+
680+
errorWhileSaving = true;
681+
}
682+
683+
// Check for errors during the bitstream format update
684+
if (hasValue(formatUpdateRD) && formatUpdateRD.hasFailed) {
685+
this.notificationsService.error(
686+
this.translate.instant(NOTIFICATIONS_PREFIX + 'error.format.title'),
687+
formatUpdateRD.errorMessage
688+
);
689+
690+
errorWhileSaving = true;
691+
}
692+
693+
this.bitstreamService.commitUpdates();
694+
this.notificationsService.success(
695+
this.translate.instant(NOTIFICATIONS_PREFIX + 'saved.title'),
696+
this.translate.instant(NOTIFICATIONS_PREFIX + 'saved.content')
697+
);
698+
if (!errorWhileSaving) {
699+
this.navigateToItemEditBitstreams();
700+
}
701+
})
702+
);
703+
}
704+
705+
updateBitstreamMetadataRD$(updatedValues: any): Observable<RemoteData<Bitstream>> {
664706
const updatedBitstream = this.formToBitstream(updatedValues);
665-
const selectedFormat = this.formatOptions.find((f: BitstreamFormat) => f.id === updatedValues.formatContainer.selectedFormat);
666-
const isNewFormat = selectedFormat.id !== this.bitstreamFormat.id;
667-
const isPrimary = updatedValues.fileNamePrimaryContainer.primaryBitstream;
668-
const wasPrimary = this.primaryBitstreamUUID === this.bitstream.uuid;
669-
670-
let bitstream$: Observable<Bitstream>;
671-
let bundle$: Observable<Bundle>;
672-
let errorWhileSaving = false;
673-
674-
if (wasPrimary !== isPrimary) {
675-
let bundleRd$: Observable<RemoteData<Bundle>>;
676-
if (wasPrimary) {
677-
bundleRd$ = this.primaryBitstreamService.delete(this.bundle);
678-
} else if (hasValue(this.primaryBitstreamUUID)) {
679-
bundleRd$ = this.primaryBitstreamService.put(this.bitstream, this.bundle);
680-
} else {
681-
bundleRd$ = this.primaryBitstreamService.create(this.bitstream, this.bundle);
682-
}
683707

684-
bundle$ = bundleRd$.pipe(
685-
getFirstCompletedRemoteData(),
686-
// If the request succeeded, use the new bundle data
687-
// Otherwise send a notification and use the old bundle data
688-
switchMap((bundleRd: RemoteData<Bundle>) => {
689-
if (bundleRd.hasSucceeded) {
690-
return observableOf(bundleRd.payload);
691-
} else {
692-
this.notificationsService.error(
693-
this.translate.instant(NOTIFICATIONS_PREFIX + 'error.primaryBitstream.title'),
694-
bundleRd.errorMessage
695-
);
696-
errorWhileSaving = true;
697-
698-
return observableOf(this.bundle);
699-
}
700-
}),
701-
);
708+
return this.bitstreamService.update(updatedBitstream).pipe(
709+
getFirstCompletedRemoteData()
710+
);
711+
}
712+
713+
/**
714+
* Creates and returns an observable that will update the primary bitstream in the bundle of the
715+
* current bitstream, if necessary according to the provided updated values.
716+
* When an update is necessary, the observable fires once with the completed RemoteData of the bundle update.
717+
* When no update is necessary, the observable fires once with a null value.
718+
* @param updatedValues The raw updated values in the bitstream edit form
719+
*/
720+
updatePrimaryBitstreamRD$(updatedValues: any): Observable<RemoteData<Bundle>> {
721+
// Whether the edited bitstream should be the primary bitstream according to the form
722+
const shouldBePrimary: boolean = updatedValues.fileNamePrimaryContainer.primaryBitstream;
723+
// Whether the edited bitstream currently is the primary bitstream
724+
const isPrimary = this.primaryBitstreamUUID === this.bitstream.uuid;
702725

703-
} else {
704-
bundle$ = observableOf(this.bundle);
726+
// If the primary bitstream status should not be changed, there is nothing to do
727+
if (shouldBePrimary === isPrimary) {
728+
return observableOf(null);
705729
}
706730

707-
if (isNewFormat) {
708-
bitstream$ = this.bitstreamService.updateFormat(this.bitstream, selectedFormat).pipe(
709-
getFirstCompletedRemoteData(),
710-
map((formatResponse: RemoteData<Bitstream>) => {
711-
if (hasValue(formatResponse) && formatResponse.hasFailed) {
712-
this.notificationsService.error(
713-
this.translate.instant(NOTIFICATIONS_PREFIX + 'error.format.title'),
714-
formatResponse.errorMessage
715-
);
716-
} else {
717-
return formatResponse.payload;
718-
}
719-
})
720-
);
731+
let updatedBundleRD$: Observable<RemoteData<Bundle>>;
732+
if (isPrimary) {
733+
updatedBundleRD$ = this.primaryBitstreamService.delete(this.bundle);
734+
} else if (hasValue(this.primaryBitstreamUUID)) {
735+
updatedBundleRD$ = this.primaryBitstreamService.put(this.bitstream, this.bundle);
721736
} else {
722-
bitstream$ = observableOf(this.bitstream);
737+
updatedBundleRD$ = this.primaryBitstreamService.create(this.bitstream, this.bundle);
723738
}
724739

725-
this.subs.push(combineLatest([bundle$, bitstream$]).pipe(
726-
tap(([bundle]) => this.bundle = bundle),
727-
switchMap(() => {
728-
return this.bitstreamService.update(updatedBitstream).pipe(
729-
getFirstSucceededRemoteDataPayload()
730-
);
731-
})
732-
).subscribe(() => {
733-
this.bitstreamService.commitUpdates();
734-
this.notificationsService.success(
735-
this.translate.instant(NOTIFICATIONS_PREFIX + 'saved.title'),
736-
this.translate.instant(NOTIFICATIONS_PREFIX + 'saved.content')
737-
);
738-
if (!errorWhileSaving) {
739-
this.navigateToItemEditBitstreams();
740-
}
741-
}));
740+
return updatedBundleRD$.pipe(
741+
getFirstCompletedRemoteData()
742+
);
743+
}
744+
745+
/**
746+
* Creates and returns an observable that will update the bitstream format
747+
* if necessary according to the provided updated values.
748+
* When an update is necessary, the observable fires once with the completed RemoteData of the bitstream update.
749+
* When no update is necessary, the observable fires once with a null value.
750+
* @param updatedValues The raw updated values in the bitstream edit form
751+
*/
752+
updateBitstreamFormatRD$(updatedValues: any): Observable<RemoteData<Bitstream>> {
753+
const selectedFormat = this.formatOptions.find((f: BitstreamFormat) => f.id === updatedValues.formatContainer.selectedFormat);
754+
const formatChanged = selectedFormat.id !== this.bitstreamFormat.id;
755+
756+
// If the format has not changed, there is nothing to do
757+
if (!formatChanged) {
758+
return observableOf(null);
759+
}
760+
761+
return this.bitstreamService.updateFormat(this.bitstream, selectedFormat).pipe(
762+
getFirstCompletedRemoteData(),
763+
);
742764
}
743765

744766
/**

0 commit comments

Comments
 (0)