From 0d90209ac9d9249133a8106af30b5240a171db35 Mon Sep 17 00:00:00 2001 From: apapachristou Date: Mon, 20 Jul 2020 18:24:29 +0300 Subject: [PATCH] Fixes bugs on discard changes on DMP editor and renames field authors to field researchers --- .../dataset-info/dataset-info.component.ts | 24 +++++++---- .../app/ui/dmp/editor/dmp-editor.component.ts | 41 +++++++++++++++---- .../funding-info/funding-info.component.ts | 39 ++++++++++-------- .../editor/main-info/main-info.component.html | 6 +-- dmp-frontend/src/assets/i18n/en.json | 3 +- 5 files changed, 75 insertions(+), 38 deletions(-) diff --git a/dmp-frontend/src/app/ui/dmp/editor/dataset-info/dataset-info.component.ts b/dmp-frontend/src/app/ui/dmp/editor/dataset-info/dataset-info.component.ts index 5dfe19777..43dda3552 100644 --- a/dmp-frontend/src/app/ui/dmp/editor/dataset-info/dataset-info.component.ts +++ b/dmp-frontend/src/app/ui/dmp/editor/dataset-info/dataset-info.component.ts @@ -35,15 +35,8 @@ export class DatasetInfoComponent extends BaseComponent implements OnInit { @Input() isClone: boolean; @Output() onFormChanged: EventEmitter = new EventEmitter(); - profilesAutoCompleteConfiguration: MultipleAutoCompleteConfiguration = { - filterFn: this.filterProfiles.bind(this), - initialItems: (excludedItems: any[]) => this.filterProfiles('').pipe(map(result => result.filter(resultItem => (excludedItems || []).map(x => x.id).indexOf(resultItem.id) === -1))), - displayFn: (item) => item['label'], - titleFn: (item) => item['label'], - subtitleFn: (item) => item['description'] - }; - selectedDmpProfileDefinition: DmpProfileDefinition; + profilesAutoCompleteConfiguration: MultipleAutoCompleteConfiguration; constructor( private language: TranslateService, @@ -58,6 +51,14 @@ export class DatasetInfoComponent extends BaseComponent implements OnInit { } ngOnInit() { + this.profilesAutoCompleteConfiguration = { + filterFn: this.filterProfiles.bind(this), + initialItems: (excludedItems: any[]) => this.filterProfiles('').pipe(map(result => result.filter(resultItem => (excludedItems || []).map(x => x.id).indexOf(resultItem.id) === -1))), + displayFn: (item) => item['label'], + titleFn: (item) => item['label'], + subtitleFn: (item) => item['description'] + }; + if (this.formGroup.get('definition')) { this.selectedDmpProfileDefinition = this.formGroup.get('definition').value; } this.registerFormEventsForDmpProfile(); @@ -74,6 +75,13 @@ export class DatasetInfoComponent extends BaseComponent implements OnInit { this.formGroup.valueChanges.pipe(takeUntil(this._destroyed)) .subscribe(x => { + this.profilesAutoCompleteConfiguration = { + filterFn: this.filterProfiles.bind(this), + initialItems: (excludedItems: any[]) => this.filterProfiles('').pipe(map(result => result.filter(resultItem => (excludedItems || []).map(x => x.id).indexOf(resultItem.id) === -1))), + displayFn: (item) => item['label'], + titleFn: (item) => item['label'], + subtitleFn: (item) => item['description'] + }; this.onFormChanged.emit(); }); } diff --git a/dmp-frontend/src/app/ui/dmp/editor/dmp-editor.component.ts b/dmp-frontend/src/app/ui/dmp/editor/dmp-editor.component.ts index 7a5fe5525..8704484a1 100644 --- a/dmp-frontend/src/app/ui/dmp/editor/dmp-editor.component.ts +++ b/dmp-frontend/src/app/ui/dmp/editor/dmp-editor.component.ts @@ -1,11 +1,11 @@ import { Component, OnInit, SimpleChanges } from '@angular/core'; -import { FormGroup } from '@angular/forms'; +import { FormGroup, AbstractControl, FormControl, FormArray } from '@angular/forms'; import { MatDialog } from '@angular/material/dialog'; import { ActivatedRoute, Params, Router } from '@angular/router'; import { DmpStatus } from '@app/core/common/enum/dmp-status'; import { DataTableRequest } from '@app/core/model/data-table/data-table-request'; -import { DmpProfileDefinition } from '@app/core/model/dmp-profile/dmp-profile'; +import { DmpProfileDefinition, DmpProfile } from '@app/core/model/dmp-profile/dmp-profile'; import { DmpProfileListing } from '@app/core/model/dmp-profile/dmp-profile-listing'; import { DmpModel } from '@app/core/model/dmp/dmp'; import { UserModel } from '@app/core/model/user/user'; @@ -37,11 +37,8 @@ import { map, takeUntil } from 'rxjs/operators'; import { Principal } from "@app/core/model/auth/principal"; import { Role } from "@app/core/common/enum/role"; import { LockService } from '@app/core/services/lock/lock.service'; -import { Location } from '@angular/common'; import { LockModel } from '@app/core/model/lock/lock.model'; import { Guid } from '@common/types/guid'; -import { isNullOrUndefined } from 'util'; -import { environment } from 'environments/environment'; import { ConfigurationService } from '@app/core/services/configuration/configuration.service'; import { ExtraPropertiesFormModel } from './general-tab/extra-properties-form.model'; @@ -142,7 +139,9 @@ export class DmpEditorComponent extends BaseComponent implements OnInit, IBreadC this.dmp.extraProperties = new ExtraPropertiesFormModel(); this.dmp.fromModel(data); this.formGroup = this.dmp.buildForm(); - this.formGroupRawValue = this.formGroup.getRawValue(); + + this.formGroupRawValue = JSON.parse(JSON.stringify(this.formGroup.getRawValue())); + this.setIsUserOwner(); if (!this.isUserOwner) { this.isFinalized = true; @@ -200,7 +199,9 @@ export class DmpEditorComponent extends BaseComponent implements OnInit, IBreadC this.dmp.extraProperties = new ExtraPropertiesFormModel(); this.dmp.fromModel(data); this.formGroup = this.dmp.buildForm(); - this.formGroupRawValue = this.formGroup.getRawValue(); + + this.formGroupRawValue = JSON.parse(JSON.stringify(this.formGroup.getRawValue())); + //this.registerFormEventsForDmpProfile(this.dmp.definition); this.formGroup.disable(); // if (!this.isAuthenticated) { @@ -237,7 +238,9 @@ export class DmpEditorComponent extends BaseComponent implements OnInit, IBreadC this.dmp.extraProperties.visible = false; this.dmp.extraProperties.contact = this.authService.current().id; this.formGroup = this.dmp.buildForm(); - this.formGroupRawValue = this.formGroup.getRawValue(); + + this.formGroupRawValue = JSON.parse(JSON.stringify(this.formGroup.getRawValue())); + this.registerFormEventsForNewItem(); if (this.isAuthenticated) { this.language.get('NAV-BAR.MY-DMPS').pipe(takeUntil(this._destroyed)).subscribe(x => { @@ -266,6 +269,24 @@ export class DmpEditorComponent extends BaseComponent implements OnInit, IBreadC }); } + copyFormControl(control: AbstractControl) { + if (control instanceof FormControl) { + return new FormControl(control.value); + } else if (control instanceof FormGroup) { + const copy = new FormGroup({}); + Object.keys(control.getRawValue()).forEach(key => { + copy.addControl(key, this.copyFormControl(control.controls[key])); + }); + return copy; + } else if (control instanceof FormArray) { + const copy = new FormArray([]); + control.controls.forEach(control => { + copy.push(this.copyFormControl(control)); + }) + return copy; + } + } + public formChanged() { this.hasChanges = true; } @@ -604,7 +625,9 @@ export class DmpEditorComponent extends BaseComponent implements OnInit, IBreadC } public discardChanges() { - this.formGroup.patchValue(this.formGroupRawValue); + // this.formGroup.reset(); + this.formGroup.patchValue(JSON.parse(JSON.stringify(this.formGroupRawValue))); + this.hasChanges = false; } diff --git a/dmp-frontend/src/app/ui/dmp/editor/funding-info/funding-info.component.ts b/dmp-frontend/src/app/ui/dmp/editor/funding-info/funding-info.component.ts index 461536df7..11a74f5a2 100644 --- a/dmp-frontend/src/app/ui/dmp/editor/funding-info/funding-info.component.ts +++ b/dmp-frontend/src/app/ui/dmp/editor/funding-info/funding-info.component.ts @@ -64,6 +64,28 @@ export class FundingInfoComponent extends BaseComponent implements OnInit { const grantRequestItem: RequestItem = new RequestItem(); grantRequestItem.criteria = new GrantCriteria(); + this.configureAutoCompletes(); + + this.isCreateNew = (this.grantformGroup.get('label').value != null && this.grantformGroup.get('label').value.length > 0); + this.isCreateNewProject = (this.projectFormGroup.get('label').value != null && this.projectFormGroup.get('label').value.length > 0); + this.isCreateNewFunder = (this.funderFormGroup.get('label').value != null && this.funderFormGroup.get('label').value.length > 0); + this.setGrantValidators(); + this.setProjectValidators(); + this.setFunderValidators(); + this.registerFormListeners(); + if (this.isNew && !this.isClone) { + this.grantformGroup.reset(); + this.grantformGroup.disable(); + } + + this.formGroup.valueChanges.pipe(takeUntil(this._destroyed)) + .subscribe(x => { + this.configureAutoCompletes(); + this.onFormChanged.emit(); + }); + } + + configureAutoCompletes(): void { this.funderAutoCompleteConfiguration = { filterFn: this.searchFunder.bind(this), initialItems: () => this.searchFunder(''), @@ -87,23 +109,6 @@ export class FundingInfoComponent extends BaseComponent implements OnInit { titleFn: (item) => item['label'], subtitleFn: (item) => item['source'] ? this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.SOURCE:') + item['source'] : (item['key'] ? this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.SOURCE:') + item['key'] : this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.NO-SOURCE')) } - - this.isCreateNew = (this.grantformGroup.get('label').value != null && this.grantformGroup.get('label').value.length > 0); - this.isCreateNewProject = (this.projectFormGroup.get('label').value != null && this.projectFormGroup.get('label').value.length > 0); - this.isCreateNewFunder = (this.funderFormGroup.get('label').value != null && this.funderFormGroup.get('label').value.length > 0); - this.setGrantValidators(); - this.setProjectValidators(); - this.setFunderValidators(); - this.registerFormListeners(); - if (this.isNew && !this.isClone) { - this.grantformGroup.reset(); - this.grantformGroup.disable(); - } - - this.formGroup.valueChanges.pipe(takeUntil(this._destroyed)) - .subscribe(x => { - this.onFormChanged.emit(); - }); } searchGrant(query: any) { diff --git a/dmp-frontend/src/app/ui/dmp/editor/main-info/main-info.component.html b/dmp-frontend/src/app/ui/dmp/editor/main-info/main-info.component.html index 748e68d8e..664c6057b 100644 --- a/dmp-frontend/src/app/ui/dmp/editor/main-info/main-info.component.html +++ b/dmp-frontend/src/app/ui/dmp/editor/main-info/main-info.component.html @@ -31,17 +31,17 @@ - +
-
1.3 {{'DMP-EDITOR.FIELDS.AUTHORS' | translate}}
+
1.3 {{'DMP-EDITOR.FIELDS.RESEARCHERS' | translate}}
{{'DMP-EDITOR.MAIN-INFO.HINT' | translate}}
info {{'DMP-EDITOR.MAIN-INFO.TYPING' | translate}}
- + {{formGroup.get('researchers').getError('backendError').message}} diff --git a/dmp-frontend/src/assets/i18n/en.json b/dmp-frontend/src/assets/i18n/en.json index 50e94b9d0..ac7b3e17b 100644 --- a/dmp-frontend/src/assets/i18n/en.json +++ b/dmp-frontend/src/assets/i18n/en.json @@ -795,7 +795,8 @@ "PLACEHOLDER": { "DESCRIPTION": "Fill with description", "ORGANIZATION": "Select organization", - "AUTHORS": "Select authors" + "AUTHORS": "Select authors", + "RESEARCHERS": "Select researchers" }, "SNACK-BAR": { "UNSUCCESSFUL-DOI": "Unsuccessful DOI creation",