diff --git a/src/app/submission/sections/form/section-form.component.ts b/src/app/submission/sections/form/section-form.component.ts index 77ca64ad3b..46eea15fac 100644 --- a/src/app/submission/sections/form/section-form.component.ts +++ b/src/app/submission/sections/form/section-form.component.ts @@ -35,7 +35,7 @@ import { difference } from '../../../shared/object.util'; templateUrl: './section-form.component.html', }) @renderSectionFor(SectionsType.SubmissionForm) -export class FormSectionComponent extends SectionModelComponent implements OnDestroy { +export class FormSectionComponent extends SectionModelComponent { public formId; public formModel: DynamicFormControlModel[]; @@ -90,7 +90,7 @@ export class FormSectionComponent extends SectionModelComponent implements OnDes }); } - ngOnDestroy() { + onSectionDestroy() { this.subs .filter((subscription) => hasValue(subscription)) .forEach((subscription) => subscription.unsubscribe()); diff --git a/src/app/submission/sections/license/section-license.component.ts b/src/app/submission/sections/license/section-license.component.ts index 3d29ea3c59..bf962d685c 100644 --- a/src/app/submission/sections/license/section-license.component.ts +++ b/src/app/submission/sections/license/section-license.component.ts @@ -30,7 +30,7 @@ import { FormComponent } from '../../../shared/form/form.component'; templateUrl: './section-license.component.html', }) @renderSectionFor(SectionsType.License) -export class LicenseSectionComponent extends SectionModelComponent implements OnDestroy { +export class LicenseSectionComponent extends SectionModelComponent { public formId; public formModel: DynamicFormControlModel[]; @@ -140,7 +140,7 @@ export class LicenseSectionComponent extends SectionModelComponent implements On } } - ngOnDestroy() { + onSectionDestroy() { this.subs .filter((subscription) => hasValue(subscription)) .forEach((subscription) => subscription.unsubscribe()); diff --git a/src/app/submission/sections/models/section.model.ts b/src/app/submission/sections/models/section.model.ts index 32f37ffcd9..72273e9f47 100644 --- a/src/app/submission/sections/models/section.model.ts +++ b/src/app/submission/sections/models/section.model.ts @@ -1,8 +1,9 @@ -import { Inject, OnInit } from '@angular/core'; +import { Inject, OnDestroy, OnInit } from '@angular/core'; import { SectionDataObject } from './section-data.model'; import { Observable } from 'rxjs/Observable'; import { SectionsService } from '../sections.service'; -import { isNotUndefined } from '../../../shared/empty.util'; +import { hasValue, isNotUndefined } from '../../../shared/empty.util'; +import { Subscription } from 'rxjs/Subscription'; export interface SectionDataModel { sectionData: SectionDataObject @@ -11,12 +12,13 @@ export interface SectionDataModel { /** * An abstract model class for a submission edit form section. */ -export abstract class SectionModelComponent implements OnInit, SectionDataModel { +export abstract class SectionModelComponent implements OnDestroy, OnInit, SectionDataModel { protected abstract sectionService: SectionsService; collectionId: string; sectionData: SectionDataObject; submissionId: string; protected valid: boolean; + private sectionStatusSub: Subscription; public constructor(@Inject('collectionIdProvider') public injectedCollectionId: string, @Inject('sectionDataProvider') public injectedSectionData: SectionDataObject, @@ -33,13 +35,21 @@ export abstract class SectionModelComponent implements OnInit, SectionDataModel protected abstract getSectionStatus(): Observable; protected abstract onSectionInit(): void; + protected abstract onSectionDestroy(): void; protected updateSectionStatus(): void { - this.getSectionStatus() + this.sectionStatusSub = this.getSectionStatus() .filter((sectionStatus: boolean) => isNotUndefined(sectionStatus)) .startWith(true) .subscribe((sectionStatus: boolean) => { this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, sectionStatus); }); } + + ngOnDestroy(): void { + this.onSectionDestroy(); + if (hasValue(this.sectionStatusSub)) { + this.sectionStatusSub.unsubscribe(); + } + } } diff --git a/src/app/submission/sections/upload/section-upload.component.ts b/src/app/submission/sections/upload/section-upload.component.ts index 797e4a1148..dd127054d5 100644 --- a/src/app/submission/sections/upload/section-upload.component.ts +++ b/src/app/submission/sections/upload/section-upload.component.ts @@ -33,7 +33,7 @@ export const POLICY_DEFAULT_WITH_LIST = 2; // Banner2 templateUrl: './section-upload.component.html', }) @renderSectionFor(SectionsType.Upload) -export class UploadSectionComponent extends SectionModelComponent implements OnDestroy { +export class UploadSectionComponent extends SectionModelComponent { public AlertTypeEnum = AlertType; public fileIndexes = []; @@ -205,7 +205,7 @@ export class UploadSectionComponent extends SectionModelComponent implements OnD /** * Method provided by Angular. Invoked when the instance is destroyed. */ - ngOnDestroy() { + onSectionDestroy() { this.subs .filter((subscription) => hasValue(subscription)) .forEach((subscription) => subscription.unsubscribe());