import { Component, ViewChild, Injector, Output, EventEmitter, OnInit} from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { catchError, finalize } from 'rxjs/operators'; import { DataRetentionsServiceProxy, CreateOrEditDataRetentionDto } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; import { throwError } from 'rxjs'; @Component({ selector: 'createOrEditDataRetentionModal', templateUrl: './create-or-edit-dataRetention-modal.component.html' }) export class CreateOrEditDataRetentionModalComponent extends AppComponentBase implements OnInit { @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @Output() modalSave: EventEmitter = new EventEmitter(); active = false; saving = false; duration = ''; isActive = false; startTime: any; currentDate: Date; today = new Date(); dataRetention: CreateOrEditDataRetentionDto = new CreateOrEditDataRetentionDto(); constructor( injector: Injector, private _dataRetentionsServiceProxy: DataRetentionsServiceProxy ) { super(injector); } ngOnInit() { } show(dataRetentionId?: number): void { if (!dataRetentionId) { this.isActive = false; this.dataRetention = new CreateOrEditDataRetentionDto(); this.dataRetention.id = dataRetentionId; this.active = true; this.startTime = ''; this.modal.show(); } else { this._dataRetentionsServiceProxy.getDataRetentionForEdit(dataRetentionId).subscribe(result => { this.dataRetention = result.dataRetention; //this.showDurationText(); this.active = true; this.isActive = result.dataRetention.isActive; this.startTime = moment(moment(this.currentDate).format('ddd MMM DD YYYY') + ' ' + result.dataRetention.startTime).toDate() this.modal.show(); }); } } save(): void { this.saving = true; this.dataRetention.isActive = this.isActive; this.dataRetention.startTime = new Date(this.startTime).toLocaleTimeString('en-GB', { hour12: false, hour: "numeric", minute: "numeric", second: "numeric" }) as any; if (this.isActive) { this.message.confirm( '', 'Are you sure you want to continue?', (isConfirmed) => { if (isConfirmed) { this._dataRetentionsServiceProxy.createOrEdit(this.dataRetention) .pipe(catchError((err) => { console.log(err); return throwError(err); }), finalize(() => { this.saving = false; })) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } else { this.saving = false; } } ); } else { this._dataRetentionsServiceProxy.createOrEdit(this.dataRetention) .pipe(catchError((err) => { console.log(err); return throwError(err); }), finalize(() => { this.saving = false; })) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } } close(): void { this.duration = ''; this.active = false; this.modal.hide(); } // showDurationText(event?) { // if (this.dataRetention.frequency == 0) { // this.duration = this.dataRetention.duration > 1 ? 'Weeks' : 'Week'; // } else if (this.dataRetention.frequency == 1) { // this.duration = this.dataRetention.duration > 1 ? 'Months' : 'Month'; // } else if (this.dataRetention.frequency == 2) { // this.duration = this.dataRetention.duration > 1 ? 'Years' : 'Year'; // } // } }