import { Component, Input, OnInit, ViewEncapsulation } from '@angular/core'; import { FormGroup } from '@angular/forms'; import { MatSnackBar } from '@angular/material'; import { ActivatedRoute, Router } from '@angular/router'; import { TranslateService } from '@ngx-translate/core'; import { Observable } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; import { BaseComponent } from '../../../core/common/base/base.component'; import { BaseCriteria } from '../../../models/criteria/BaseCriteria'; import { DatasetProfileCriteria } from '../../../models/criteria/dataset-profile/DatasetProfileCriteria'; import { ProjectCriteria } from '../../../models/criteria/project/ProjectCriteria'; import { RequestItem } from '../../../models/criteria/RequestItem'; import { DatasetProfileModel } from '../../../models/datasets/DatasetProfileModel'; import { DmpUsersModel } from '../../../models/dmpUsers/DmpUsersModel'; import { BaseErrorModel } from '../../../models/error/BaseErrorModel'; import { ExternalSourcesItemModel } from '../../../models/external-sources/ExternalSourcesItemModel'; import { DataManagementPlanService } from '../../../services/data-management-plan/data-management-plan.service'; import { ExternalSourcesService } from '../../../services/external-sources/external-sources.service'; import { LanguageResolverService } from '../../../services/language-resolver/language-resolver.service'; import { ProjectService } from '../../../services/project/project.service'; import { MultipleAutoCompleteConfiguration } from '../../../shared/components/autocompletes/multiple/multiple-auto-complete-configuration'; import { SingleAutoCompleteConfiguration } from '../../../shared/components/autocompletes/single/single-auto-complete-configuration'; import { SnackBarNotificationComponent } from '../../../shared/components/notificaiton/snack-bar-notification.component'; @Component({ selector: 'app-dmp-wizard-editor-component', templateUrl: 'dmp-wizard-editor.component.html', styleUrls: ['./dmp-wizard-editor.component.scss'], encapsulation: ViewEncapsulation.None }) export class DataManagementPlanWizardEditorComponent extends BaseComponent implements OnInit { isNew = true; @Input() formGroup: FormGroup = null; filteringOrganisationsAsync = false; filteringResearchersAsync = false; filteredProfilesAsync = false; filteredOrganisations: ExternalSourcesItemModel[]; filteredResearchers: ExternalSourcesItemModel[]; filteredProfiles: DatasetProfileModel[]; projectAutoCompleteConfiguration: SingleAutoCompleteConfiguration; profilesAutoCompleteConfiguration: MultipleAutoCompleteConfiguration; organisationsAutoCompleteConfiguration: MultipleAutoCompleteConfiguration; researchersAutoCompleteConfiguration: MultipleAutoCompleteConfiguration; createNewVersion; associatedUsers: Array; labelDisabled = false; constructor( private dataManagementPlanService: DataManagementPlanService, private projectService: ProjectService, private externalSourcesService: ExternalSourcesService, private route: ActivatedRoute, public snackBar: MatSnackBar, public router: Router, public language: TranslateService, private _service: DataManagementPlanService, private languageResolverService: LanguageResolverService ) { super(); } ngOnInit() { const projectRequestItem: RequestItem = new RequestItem(); projectRequestItem.criteria = new ProjectCriteria(); this.projectAutoCompleteConfiguration = { filterFn: this.searchProject.bind(this.projectService), items: this.searchProject(''), displayFn: (item) => item['label'], titleFn: (item) => item['label'], //mapFn: (item) => new JsonSerializer().fromJSONArray(item, ProjectReference).map(item => item.toDropdownList()), loadDataOnStart: true }; this.profilesAutoCompleteConfiguration = { filterFn: this.filterProfiles.bind(this), initialItems: (excludedItems: any[]) => this.filterProfiles('').map(result => result.filter(resultItem => excludedItems.map(x => x.id).indexOf(resultItem.id) === -1)), displayFn: (item) => item['label'], titleFn: (item) => item['label'], //mapFn: (item) => new JsonSerializer().fromJSONArray(item, ProjectReference).map(item => item.toDropdownList()), loadDataOnStart: true }; this.organisationsAutoCompleteConfiguration = { filterFn: this.filterOrganisations.bind(this), initialItems: (excludedItems: any[]) => this.filterOrganisations('').map(result => result.filter(resultItem => excludedItems.map(x => x.id).indexOf(resultItem.id) === -1)), displayFn: (item) => item['name'], titleFn: (item) => item['name'], loadDataOnStart: true }; this.researchersAutoCompleteConfiguration = { filterFn: this.filterResearchers.bind(this), initialItems: (excludedItems: any[]) => this.filterResearchers('').map(result => result.filter(resultItem => excludedItems.map(x => x.id).indexOf(resultItem.id) === -1)), displayFn: (item) => item['name'], titleFn: (item) => item['name'], loadDataOnStart: true }; const organisationRequestItem: RequestItem = new RequestItem(); organisationRequestItem.criteria = new BaseCriteria(); this.route.data .pipe(takeUntil(this._destroyed)) .subscribe(value => { if (value.clone === false && this.formGroup.get('label').value) { this.labelDisabled = true; } this.formGroup.controls['version'].disable(); }); } searchProject(query: string) { const projectRequestItem: RequestItem = new RequestItem(); projectRequestItem.criteria = new ProjectCriteria(); projectRequestItem.criteria.like = query; return this.projectService.getWithExternal(projectRequestItem); } formSubmit(): void { //this.touchAllFormFields(this.formGroup); if (!this.isFormValid()) { return; } this.onSubmit(); } public isFormValid() { return this.formGroup.valid; } onSubmit(): void { this.dataManagementPlanService.createDataManagementPlan(this.formGroup.getRawValue()) .pipe(takeUntil(this._destroyed)) .subscribe( complete => this.onCallbackSuccess(), error => this.onCallbackError(error) ); } onCallbackSuccess(): void { this.snackBar.openFromComponent(SnackBarNotificationComponent, { data: { message: this.isNew ? 'GENERAL.SNACK-BAR.SUCCESSFUL-CREATION' : 'GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE', language: this.language }, duration: 3000, }); this.router.navigate(['/dmps']); } onCallbackError(error: any) { this.setErrorModel(error.error); //this.validateAllFormFields(this.formGroup); } public setErrorModel(errorModel: BaseErrorModel) { Object.keys(errorModel).forEach(item => { // (this.dataManagementPlan.errorModel)[item] = (errorModel)[item]; }); } public cancel(): void { this.router.navigate(['/dmps']); } filterOrganisations(value: string): Observable { this.filteredOrganisations = undefined; this.filteringOrganisationsAsync = true; return this.externalSourcesService.searchDMPOrganizations(value); } filterResearchers(value: string): Observable { this.filteredResearchers = undefined; this.filteringResearchersAsync = true; return this.externalSourcesService.searchDMPResearchers({ criteria: { name: value, like: null } }); } filterProfiles(value: string): Observable { this.filteredProfiles = undefined; this.filteredProfilesAsync = true; const request = new RequestItem(); const criteria = new DatasetProfileCriteria(); criteria.like = value; request.criteria = criteria; return this._service.searchDMPProfiles(request); } }