import { Component , OnDestroy , OnInit } from '@angular/core' ; import { FormBuilder , FormGroup , Validators } from '@angular/forms' ; import { MatSnackBar } from '@angular/material' ; import { ActivatedRoute , Router } from '@angular/router' ; import { Subscription } from 'rxjs' ; import { v4 as uuid } from 'uuid' ; import { MonitorService } from '@core/services/index.service' ; import { BreadCrumbModel , MonitorModel , ProjectModel } from '@shared/models/index.model' ; /**
 * Monitor create edit component
 */ @ Component ( { selector : 'dashboard-monitor-create-edit' , templateUrl : './monitor-create-edit.component.html' , styleUrls : [ './monitor-create-edit.component.scss' ] , } ) export class MonitorCreateEditComponent implements OnInit , OnDestroy { private monitorUid : string ; private monitorsList : MonitorModel [ ] = [ ] ; private projectSubscription : Subscription ; private saveMonitorSubscription : Subscription ; public isEdit : Boolean = false ; public monitorForm : FormGroup ; public projectUid : string ; public statusCodeList : Number [ ] = [ 200 , 201 , 204 , 400 , 401 , 404 , 500 ] ; public project : ProjectModel ; public breadCrumb : BreadCrumbModel [ ] ; param form param monitorService param route param snackBar param router constructor ( private form , private monitorService , private route , private snackBar , private router ) { } /**
     * Lifecycle init method
     */ ngOnInit ( ) { this . projectUid = this . route . snapshot . paramMap . get ( 'projectUid' ) ; this . initializeMonitorForm ( ) ; this . projectSubscription = this . route . data . subscribe ( ( data ) => { this . project = data . project ; this . breadCrumb = [ { link : `/projects/${ this . project . uid }` , title : this . project . title } ] ; this . monitorsList = this . project && this . project . monitors ? this . project . monitors : [ ] ; this . monitorUid = this . route . snapshot . paramMap . get ( 'monitorUid' ) ; if ( this . monitorUid ) { this . isEdit = true ; const filteredMonitor = this . monitorsList . find ( ( monitor ) => monitor . uid === this . monitorUid ) ; this . monitorForm . reset ( filteredMonitor ) ; } } ) ; } /**
     * To initialize monitor form
     */ initializeMonitorForm ( ) { this . monitorForm = this . form . group ( { uid : uuid ( ) , name : [ undefined , [ Validators . required , Validators . minLength ( 3 ) ] ] , method : [ undefined , Validators . required ] , path : [ undefined , Validators . required ] , expectedCode : [ undefined , Validators . required ] , expectedText : [ undefined ] , } ) ; } param uid param monitors save ( ) { this . saveMonitorSubscription = this . monitorService . save ( this . projectUid , new MonitorModel ( { ... this . monitorForm . value } ) ) . subscribe ( ( ) => this . router . navigate ( [ `/projects/${ this . projectUid }/monitors` ] ) , ( error ) : any => this . snackBar . open ( error . message , undefined , { duration : 5000 } ) ) ; } /**
     * Lifecycle destroy method
     */ ngOnDestroy ( ) { this . projectSubscription . unsubscribe ( ) ; if ( this . saveMonitorSubscription ) { this . saveMonitorSubscription . unsubscribe ( ) ; } } }