Skip to content

Commit ff2fbf1

Browse files
committed
115491: Restructure bitstream update code
1 parent 6195b8d commit ff2fbf1

1 file changed

Lines changed: 56 additions & 36 deletions

File tree

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

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ interface DataObjects {
5151
item: Item,
5252
}
5353

54+
/**
55+
* The results after updating all the fields on submission.
56+
*/
57+
interface UpdateResult {
58+
metadataUpdateRD: RemoteData<Bitstream>,
59+
primaryUpdateRD: RemoteData<Bundle>,
60+
formatUpdateRD: RemoteData<Bitstream>,
61+
}
62+
5463
/**
5564
* Key prefix used to generate form messages
5665
*/
@@ -645,46 +654,21 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
645654
onSubmit() {
646655
const updatedValues = this.formGroup.getRawValue();
647656

648-
const metadataUpdateRD$ = this.updateBitstreamMetadataRD$(updatedValues);
649-
const primaryUpdateRD$ = this.updatePrimaryBitstreamRD$(updatedValues);
650-
const formatUpdateRD$ = this.updateBitstreamFormatRD$(updatedValues);
651-
652-
this.subs.push(combineLatest([metadataUpdateRD$, primaryUpdateRD$, formatUpdateRD$])
653-
.subscribe(([metadataUpdateRD, primaryUpdateRD, formatUpdateRD]) => {
654-
let errorWhileSaving = false;
655-
656-
// Check for errors during the primary bitstream update
657-
if (hasValue(primaryUpdateRD) && primaryUpdateRD.hasFailed) {
658-
this.notificationsService.error(
659-
this.translate.instant(NOTIFICATIONS_PREFIX + 'error.primaryBitstream.title'),
660-
primaryUpdateRD.errorMessage
661-
);
662-
663-
errorWhileSaving = true;
664-
}
665-
666-
// Check for errors during the bitstream format update
667-
if (hasValue(formatUpdateRD) && formatUpdateRD.hasFailed) {
668-
this.notificationsService.error(
669-
this.translate.instant(NOTIFICATIONS_PREFIX + 'error.format.title'),
670-
formatUpdateRD.errorMessage
671-
);
672-
673-
errorWhileSaving = true;
674-
}
675-
676-
this.bitstreamService.commitUpdates();
677-
this.notificationsService.success(
678-
this.translate.instant(NOTIFICATIONS_PREFIX + 'saved.title'),
679-
this.translate.instant(NOTIFICATIONS_PREFIX + 'saved.content')
680-
);
681-
if (!errorWhileSaving) {
682-
this.navigateToItemEditBitstreams();
683-
}
657+
this.subs.push(combineLatest(this.getUpdateObservables(updatedValues))
658+
.subscribe((updateResult: UpdateResult) => {
659+
this.handleUpdateResult(updateResult);
684660
})
685661
);
686662
}
687663

664+
getUpdateObservables(updatedValues: any): ObservablesDictionary<UpdateResult> {
665+
return {
666+
metadataUpdateRD: this.updateBitstreamMetadataRD$(updatedValues),
667+
primaryUpdateRD: this.updatePrimaryBitstreamRD$(updatedValues),
668+
formatUpdateRD: this.updateBitstreamFormatRD$(updatedValues),
669+
};
670+
}
671+
688672
updateBitstreamMetadataRD$(updatedValues: any): Observable<RemoteData<Bitstream>> {
689673
const updatedBitstream = this.formToBitstream(updatedValues);
690674

@@ -792,6 +776,42 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
792776
return updatedBitstream;
793777
}
794778

779+
handleUpdateResult(updateResult: UpdateResult) {
780+
let errorWhileSaving = false;
781+
782+
// Check for errors during the primary bitstream update
783+
const primaryUpdateRD = updateResult.primaryUpdateRD;
784+
if (hasValue(primaryUpdateRD) && primaryUpdateRD.hasFailed) {
785+
this.notificationsService.error(
786+
this.translate.instant(NOTIFICATIONS_PREFIX + 'error.primaryBitstream.title'),
787+
primaryUpdateRD.errorMessage
788+
);
789+
790+
errorWhileSaving = true;
791+
}
792+
793+
// Check for errors during the bitstream format update
794+
const formatUpdateRD = updateResult.formatUpdateRD;
795+
if (hasValue(formatUpdateRD) && formatUpdateRD.hasFailed) {
796+
this.notificationsService.error(
797+
this.translate.instant(NOTIFICATIONS_PREFIX + 'error.format.title'),
798+
formatUpdateRD.errorMessage
799+
);
800+
801+
errorWhileSaving = true;
802+
}
803+
804+
this.bitstreamService.commitUpdates();
805+
this.notificationsService.success(
806+
this.translate.instant(NOTIFICATIONS_PREFIX + 'saved.title'),
807+
this.translate.instant(NOTIFICATIONS_PREFIX + 'saved.content')
808+
);
809+
810+
if (!errorWhileSaving) {
811+
this.navigateToItemEditBitstreams();
812+
}
813+
}
814+
795815
/**
796816
* Cancel the form and return to the previous page
797817
*/

0 commit comments

Comments
 (0)