From 24450f63a99ccdf860f1a5561e3ee52a29e0906a Mon Sep 17 00:00:00 2001 From: Giuseppe Date: Fri, 12 Oct 2018 15:33:01 +0200 Subject: [PATCH] Created abstract methods to section model --- .../sections/form/section-form.component.ts | 23 ++++++------- .../license/section-license.component.ts | 32 +++++++++++++------ .../sections/models/section.model.ts | 25 +++++++++++++-- .../recycle/section-recycle.component.ts | 9 ++++-- .../upload/section-upload.component.ts | 14 ++++---- 5 files changed, 70 insertions(+), 33 deletions(-) diff --git a/src/app/submission/sections/form/section-form.component.ts b/src/app/submission/sections/form/section-form.component.ts index 38e59244ea..611d294983 100644 --- a/src/app/submission/sections/form/section-form.component.ts +++ b/src/app/submission/sections/form/section-form.component.ts @@ -1,6 +1,7 @@ import { ChangeDetectorRef, Component, Inject, OnDestroy, ViewChild } from '@angular/core'; import { DynamicFormControlEvent, DynamicFormControlModel } from '@ng-dynamic-forms/core'; +import { Observable } from 'rxjs/Observable'; import { Subscription } from 'rxjs/Subscription'; import { TranslateService } from '@ngx-translate/core'; import { isEqual } from 'lodash'; @@ -10,7 +11,7 @@ import { FormComponent } from '../../../shared/form/form.component'; import { FormService } from '../../../shared/form/form.service'; import { SectionModelComponent } from '../models/section.model'; import { SubmissionFormsConfigService } from '../../../core/config/submission-forms-config.service'; -import { hasValue, isNotEmpty, isNotUndefined, isUndefined } from '../../../shared/empty.util'; +import { hasValue, isNotEmpty, isUndefined } from '../../../shared/empty.util'; import { ConfigData } from '../../../core/config/config-data'; import { JsonPatchOperationPathCombiner } from '../../../core/json-patch/builder/json-patch-operation-path-combiner'; import { SubmissionFormsModel } from '../../../core/shared/config/config-submission-forms.model'; @@ -65,13 +66,14 @@ export class FormSectionComponent extends SectionModelComponent implements OnDes super(injectedCollectionId, injectedSectionData, injectedSubmissionId); } - ngOnInit() { + onSectionInit() { this.pathCombiner = new JsonPatchOperationPathCombiner('sections', this.sectionData.id); + this.formId = this.formService.getUniqueId(this.sectionData.id); + this.formConfigService.getConfigByHref(this.sectionData.config) .flatMap((config: ConfigData) => config.payload) .subscribe((config: SubmissionFormsModel) => { this.formConfig = config; - this.formId = this.formService.getUniqueId(this.sectionData.id); this.sectionService.getSectionData(this.submissionId, this.sectionData.id) .take(1) .subscribe((sectionData: WorkspaceitemSectionDataType) => { @@ -94,6 +96,10 @@ export class FormSectionComponent extends SectionModelComponent implements OnDes .forEach((subscription) => subscription.unsubscribe()); } + protected getSectionStatus(): Observable { + return this.formService.isValid(this.formId); + } + hasMetadataEnrichment(sectionData): boolean { const diffResult = []; @@ -167,16 +173,7 @@ export class FormSectionComponent extends SectionModelComponent implements OnDes subscriptions() { this.subs.push( - /** - * Subscribe to form status - */ - this.formService.isValid(this.formId) - .filter((formValid) => isNotUndefined(formValid)) - .filter((formValid) => formValid !== this.valid) - .subscribe((formState) => { - this.valid = formState; - this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, this.valid); - }), + /** * Subscribe to form's data */ diff --git a/src/app/submission/sections/license/section-license.component.ts b/src/app/submission/sections/license/section-license.component.ts index 52f77c1f16..3d29ea3c59 100644 --- a/src/app/submission/sections/license/section-license.component.ts +++ b/src/app/submission/sections/license/section-license.component.ts @@ -1,6 +1,7 @@ -import { ChangeDetectorRef, Component, Inject, OnDestroy, OnInit } from '@angular/core'; +import { ChangeDetectorRef, Component, Inject, OnDestroy, OnInit, ViewChild } from '@angular/core'; import { DynamicCheckboxModel, DynamicFormControlEvent, DynamicFormControlModel } from '@ng-dynamic-forms/core'; +import { Observable } from 'rxjs/Observable'; import { Subscription } from 'rxjs/Subscription'; import { SectionModelComponent } from '../models/section.model'; @@ -21,6 +22,7 @@ import { WorkspaceitemSectionLicenseObject } from '../../../core/submission/mode import { SubmissionService } from '../../submission.service'; import { SectionsService } from '../sections.service'; import { FormOperationsService } from '../form/form-operations.service'; +import { FormComponent } from '../../../shared/form/form.component'; @Component({ selector: 'ds-submission-section-license', @@ -28,7 +30,7 @@ import { FormOperationsService } from '../form/form-operations.service'; templateUrl: './section-license.component.html', }) @renderSectionFor(SectionsType.License) -export class LicenseSectionComponent extends SectionModelComponent implements OnDestroy, OnInit { +export class LicenseSectionComponent extends SectionModelComponent implements OnDestroy { public formId; public formModel: DynamicFormControlModel[]; @@ -38,6 +40,8 @@ export class LicenseSectionComponent extends SectionModelComponent implements On protected pathCombiner: JsonPatchOperationPathCombiner; protected subs: Subscription[] = []; + @ViewChild('formRef') private formRef: FormComponent; + constructor(protected changeDetectorRef: ChangeDetectorRef, protected collectionDataService: CollectionDataService, protected formBuilderService: FormBuilderService, @@ -52,8 +56,11 @@ export class LicenseSectionComponent extends SectionModelComponent implements On super(injectedCollectionId, injectedSectionData, injectedSubmissionId); } - ngOnInit() { + onSectionInit() { this.pathCombiner = new JsonPatchOperationPathCombiner('sections', this.sectionData.id); + this.formId = this.formService.getUniqueId(this.sectionData.id); + this.formModel = this.formBuilderService.fromJSON(SECTION_LICENSE_FORM_MODEL); + const model = this.formBuilderService.findById('granted', this.formModel); this.subs.push( this.collectionDataService.findById(this.collectionId) @@ -63,13 +70,13 @@ export class LicenseSectionComponent extends SectionModelComponent implements On .take(1) .subscribe((licenseData: RemoteData) => { this.licenseText = licenseData.payload.text; - this.formId = this.formService.getUniqueId(this.sectionData.id); - this.formModel = this.formBuilderService.fromJSON(SECTION_LICENSE_FORM_MODEL); - const model = this.formBuilderService.findById('granted', this.formModel); + // Retrieve license accepted status if ((this.sectionData.data as WorkspaceitemSectionLicenseObject).granted) { - (model as DynamicCheckboxModel).checked = true; - this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, true); + (model as DynamicCheckboxModel).valueUpdates.next(true); + // this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, true); + } else { + (model as DynamicCheckboxModel).valueUpdates.next(false); } // Disable checkbox whether it's in workflow or item scope @@ -91,7 +98,6 @@ export class LicenseSectionComponent extends SectionModelComponent implements On // When the error path is only on the section, // replace it with the path to the form field to display error also on the form if (error.path === '/sections/license') { - const model = this.formBuilderService.findById('granted', this.formModel); // check whether license is not accepted if (!(model as DynamicCheckboxModel).checked) { return Object.assign({}, error, {path: '/sections/license/granted'}); @@ -115,10 +121,16 @@ export class LicenseSectionComponent extends SectionModelComponent implements On ); } + protected getSectionStatus(): Observable { + const model = this.formBuilderService.findById('granted', this.formModel); + return (model as DynamicCheckboxModel).valueUpdates + .map((value) => value === true); + } + onChange(event: DynamicFormControlEvent) { const path = this.formOperationsService.getFieldPathSegmentedFromChangeEvent(event); const value = this.formOperationsService.getFieldValueFromChangeEvent(event); - this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, value.value); + // this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, value.value); if (value) { this.operationsBuilder.add(this.pathCombiner.getPath(path), value.value.toString(), false, true); // Remove any section's errors diff --git a/src/app/submission/sections/models/section.model.ts b/src/app/submission/sections/models/section.model.ts index 7925cc54ec..32f37ffcd9 100644 --- a/src/app/submission/sections/models/section.model.ts +++ b/src/app/submission/sections/models/section.model.ts @@ -1,5 +1,8 @@ -import { Inject } from '@angular/core'; +import { Inject, 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'; export interface SectionDataModel { sectionData: SectionDataObject @@ -8,7 +11,8 @@ export interface SectionDataModel { /** * An abstract model class for a submission edit form section. */ -export abstract class SectionModelComponent implements SectionDataModel { +export abstract class SectionModelComponent implements OnInit, SectionDataModel { + protected abstract sectionService: SectionsService; collectionId: string; sectionData: SectionDataObject; submissionId: string; @@ -21,4 +25,21 @@ export abstract class SectionModelComponent implements SectionDataModel { this.sectionData = injectedSectionData; this.submissionId = injectedSubmissionId; } + + ngOnInit(): void { + this.onSectionInit(); + this.updateSectionStatus(); + } + + protected abstract getSectionStatus(): Observable; + protected abstract onSectionInit(): void; + + protected updateSectionStatus(): void { + this.getSectionStatus() + .filter((sectionStatus: boolean) => isNotUndefined(sectionStatus)) + .startWith(true) + .subscribe((sectionStatus: boolean) => { + this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, sectionStatus); + }); + } } diff --git a/src/app/submission/sections/recycle/section-recycle.component.ts b/src/app/submission/sections/recycle/section-recycle.component.ts index 9890a59d69..3415d2ffed 100644 --- a/src/app/submission/sections/recycle/section-recycle.component.ts +++ b/src/app/submission/sections/recycle/section-recycle.component.ts @@ -5,10 +5,10 @@ import { renderSectionFor } from '../sections-decorator'; import { SectionDataObject } from '../models/section-data.model'; import { SubmissionState } from '../../submission.reducers'; import { Store } from '@ngrx/store'; -import { WorkspaceitemSectionRecycleObject } from '../../../core/submission/models/workspaceitem-section-recycle.model'; import { submissionSectionDataFromIdSelector } from '../../selectors'; import { isNotEmpty } from '../../../shared/empty.util'; import { Observable } from 'rxjs/Observable'; +import { SectionsService } from '../sections.service'; @Component({ selector: 'ds-recycle-section', @@ -26,13 +26,14 @@ export class RecycleSectionComponent extends SectionModelComponent { public files: any[]; // WorkspaceitemSectionUploadFileObject[]; constructor(protected store: Store, + protected sectionService: SectionsService, @Inject('collectionIdProvider') public injectedCollectionId: string, @Inject('sectionDataProvider') public injectedSectionData: SectionDataObject, @Inject('submissionIdProvider') public injectedSubmissionId: string) { super(injectedCollectionId, injectedSectionData, injectedSubmissionId); } - ngOnInit() { + onSectionInit() { this.sectionDataObs = this.store.select(submissionSectionDataFromIdSelector(this.submissionId, this.sectionData.id)) .filter((sd) => isNotEmpty(sd)) @@ -52,4 +53,8 @@ export class RecycleSectionComponent extends SectionModelComponent { this.isLoading = false; } + protected getSectionStatus(): Observable { + return Observable.of(true); + } + } diff --git a/src/app/submission/sections/upload/section-upload.component.ts b/src/app/submission/sections/upload/section-upload.component.ts index 9a837789cf..a464a73a2e 100644 --- a/src/app/submission/sections/upload/section-upload.component.ts +++ b/src/app/submission/sections/upload/section-upload.component.ts @@ -1,4 +1,4 @@ -import { ChangeDetectorRef, Component, Inject, OnInit } from '@angular/core'; +import { ChangeDetectorRef, Component, Inject, OnDestroy, OnInit } from '@angular/core'; import { Observable } from 'rxjs/Observable'; @@ -29,7 +29,7 @@ export const POLICY_DEFAULT_WITH_LIST = 2; // Banner2 templateUrl: './section-upload.component.html', }) @renderSectionFor(SectionsType.Upload) -export class UploadSectionComponent extends SectionModelComponent implements OnInit { +export class UploadSectionComponent extends SectionModelComponent implements OnDestroy { public AlertTypeEnum = AlertType; public fileIndexes = []; @@ -74,7 +74,7 @@ export class UploadSectionComponent extends SectionModelComponent implements OnI super(undefined, injectedSectionData, injectedSubmissionId); } - ngOnInit() { + onSectionInit() { const config$ = this.uploadsConfigService.getConfigByHref(this.sectionData.config) .flatMap((config) => config.payload); @@ -162,7 +162,6 @@ export class UploadSectionComponent extends SectionModelComponent implements OnI }) .distinctUntilChanged() .subscribe(([configMetadataForm, fileList]:[SubmissionFormsModel, any[]]) => { - let sectionStatus = false; this.fileList = []; this.fileIndexes = []; this.fileNames = []; @@ -173,9 +172,7 @@ export class UploadSectionComponent extends SectionModelComponent implements OnI this.fileIndexes.push(file.uuid); this.fileNames.push(this.getFileName(configMetadataForm, file)); }); - sectionStatus = true; } - this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, sectionStatus); this.changeDetectorRef.detectChanges(); } ) @@ -194,6 +191,11 @@ export class UploadSectionComponent extends SectionModelComponent implements OnI return title; } + protected getSectionStatus(): Observable { + return this.bitstreamService.getUploadedFileList(this.submissionId, this.sectionData.id) + .map((fileList: any[]) => (isNotUndefined(fileList) && fileList.length > 0)); + } + /** * Method provided by Angular. Invoked when the instance is destroyed. */