import { Component, Inject } from '@angular/core'; import { UntypedFormGroup } from '@angular/forms'; import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from '@angular/material/dialog'; import { DmpStatus } from '@app/core/common/enum/dmp-status'; import { DataTableRequest } from '@app/core/model/data-table/data-table-request'; import { DmpListingModel } from '@app/core/model/dmp/dmp-listing'; import { DmpCriteria } from '@app/core/query/dmp/dmp-criteria'; import { DmpService } from '@app/core/services/dmp/dmp.service'; import { SingleAutoCompleteConfiguration } from '@app/library/auto-complete/single/single-auto-complete-configuration'; import { BaseComponent } from '@common/base/base.component'; import { TranslateService } from '@ngx-translate/core'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; @Component({ selector: 'app-start-new-description-dialog', templateUrl: './start-new-description-dialog.component.html', styleUrls: ['./start-new-description-dialog.component.scss'] }) export class StartNewDescriptionDialogComponent extends BaseComponent { public isDialog: boolean = false; public formGroup: UntypedFormGroup; dmpAutoCompleteConfiguration: SingleAutoCompleteConfiguration = { filterFn: this.searchDmp.bind(this), initialItems: (extraData) => this.searchDmp(''), displayFn: (item) => this.getDatasetDisplay(item), titleFn: (item) => item['label'], subtitleFn: (item) => { try { return this.language.instant('DATASET-WIZARD.FIRST-STEP.SUB-TITLE') + new Date(item['creationTime']).toISOString(); } catch { return '-'; } } }; constructor( public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: any, public dialog: MatDialog, private language: TranslateService, private dmpService: DmpService ) { super(); this.formGroup = data.formGroup; } cancel() { this.dialogRef.close(); } send() { this.dialogRef.close(this.data); } close() { this.dialogRef.close(false); } next() { this.dialogRef.close(this.data); } startNewDmp() { this.data.startNewDmp = true; this.dialogRef.close(this.data); } searchDmp(query: string): Observable { const fields: Array = new Array(); fields.push('-created'); const dmpDataTableRequest: DataTableRequest = new DataTableRequest(0, null, { fields: fields }); dmpDataTableRequest.criteria = new DmpCriteria(); dmpDataTableRequest.criteria.like = query; dmpDataTableRequest.criteria.status = DmpStatus.Draft; return this.dmpService.getPaged(dmpDataTableRequest, "autocomplete").pipe(map(x => x.data)); } getDatasetDisplay(item: any): string { // if (!this.isPublic) { // return (item['status'] ? this.language.instant('TYPES.DATASET-STATUS.FINALISED').toUpperCase() : this.language.instant('TYPES.DATASET-STATUS.DRAFT').toUpperCase()) + ': ' + item['label']; // } // else { return item['label']; } return item['label'] ? item['label'] : null; } }