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 { ProjectService } from '@core/services/index.service' ; import { ProjectModel } from '@shared/models/index.model' ; /**
 * Project create/edit component
 */ @ Component ( { selector : 'dashboard-projects-create' , templateUrl : './create-edit.component.html' , styleUrls : [ './create-edit.component.scss' ] , } ) export class CreateEditProjectComponent implements OnInit , OnDestroy { private createEditSubscription : Subscription ; private projectSubscription : Subscription ; public uid : string ; public isEdit : Boolean = false ; public projectForm : FormGroup ; param router param form param snackBar param projectService param route constructor ( private router , private form , private snackBar , private projectService , private route ) { } /**
     * Life cycle init method
     */ ngOnInit ( ) { this . uid = this . route . snapshot . paramMap . get ( 'projectUid' ) ; this . projectForm = this . form . group ( { type : [ 'public' , [ Validators . required ] ] , title : [ undefined , [ Validators . required , Validators . minLength ( 3 ) , Validators . maxLength ( 32 ) ] ] , description : [ undefined , [ Validators . minLength ( 3 ) , Validators . maxLength ( 1024 ) ] ] , url : [ undefined ] , logoUrl : [ undefined ] , } ) ; if ( this . uid ) { this . isEdit = true ; this . projectSubscription = this . route . data . subscribe ( ( data ) => this . projectForm . reset ( data . project ) ) ; } } /**
     * Save project details based upon if click on edit or add
     */ save ( ) { if ( this . uid ) { this . createEditSubscription = this . projectService . save ( { uid : this . uid , ... this . projectForm . getRawValue ( ) } ) . subscribe ( ( ) => this . router . navigate ( [ '/projects' , this . uid ] ) , ( error ) : any => this . snackBar . open ( error . message , undefined , { duration : 5000 } ) ) ; } else { this . createEditSubscription = this . projectService . create ( this . projectForm . getRawValue ( ) ) . subscribe ( ( project ) => this . router . navigate ( [ '/projects' , project . uid ] ) , ( error ) : any => this . snackBar . open ( error . message , undefined , { duration : 5000 } ) ) ; } } /**
     * Life cycle destroy method
     */ ngOnDestroy ( ) { if ( this . createEditSubscription ) { this . createEditSubscription . unsubscribe ( ) ; } if ( this . projectSubscription ) { this . projectSubscription . unsubscribe ( ) ; } } }