Adding changes for catalog edit and create
[ccsdk/cds.git] / cds-ui / client / src / app / feature-modules / controller-catalog / create-catalog / create-catalog.component.ts
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : CDS
4 * ================================================================================
5 * Copyright (C) 2019 TechMahindra
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21 import { Component, OnInit, ViewChild, ɵConsole } from '@angular/core';
22 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
23 import { JsonEditorComponent, JsonEditorOptions } from 'ang-jsoneditor';
24 import { Observable } from 'rxjs';
25 import { ICatalogState } from 'src/app/common/core/store/models/catalogState.model';
26 import { ICatalog } from 'src/app/common/core/store/models/catalog.model';
27 import { Store } from '@ngrx/store';
28 import { IAppState } from 'src/app/common/core/store/state/app.state';
29 import { SetCatalogState } from 'src/app/common/core/store/actions/catalog.action';
30 import { CreateCatalogService } from './create-catalog.service';
31 import { NotificationHandlerService } from 'src/app/common/core/services/notification-handler.service';
32
33 @Component({
34   selector: 'app-create-catalog',
35   templateUrl: './create-catalog.component.html',
36   styleUrls: ['./create-catalog.component.scss']
37 })
38 export class CreateCatalogComponent implements OnInit {
39   
40   CatalogFormData: FormGroup;
41   @ViewChild(JsonEditorComponent) editor: JsonEditorComponent;
42   options = new JsonEditorOptions();
43   data:any;
44   derivedFrom: any[] = ['tosca.nodes.Component','tosca.nodes.VNF','tosca.nodes.Artifact','tosca.nodes.ResourceSource','tosca.nodes.Workflow','tosca.nodes.Root'];
45   definitionType: any[] = ['node_type'];
46   ccState: Observable<ICatalogState>;
47   catalog: ICatalog;
48
49   constructor(private formBuilder: FormBuilder, private store: Store<IAppState>, private catalogCreateService: CreateCatalogService, private alertService: NotificationHandlerService) { 
50     this.ccState = this.store.select('catalog');
51     this.CatalogFormData = this.formBuilder.group({
52         modelName: ['', Validators.required],
53         updatedBy: ['', Validators.required],
54         tags: ['', Validators.required],
55         definitionType: ['', Validators.required],
56         derivedFrom: ['', Validators.required],
57         description : ['', Validators.required]
58       });    
59   }
60   ngOnInit() {
61     this.options.mode = 'text';
62     this.options.modes = [ 'text', 'tree', 'view'];
63     this.options.statusBar = false; 
64
65     this.ccState.subscribe(
66       catalogdata => {
67         var catalogState: ICatalogState = { catalog: catalogdata.catalog, isLoadSuccess: catalogdata.isLoadSuccess, isSaveSuccess: catalogdata.isSaveSuccess, isUpdateSuccess: catalogdata.isUpdateSuccess };
68         this.catalog = catalogState.catalog;
69         console.log( this.catalog );
70       });
71
72 //    this.catalogCreateService.getDefinition()
73 //    .subscribe(data=>{
74 //        console.log(data);
75 //        data.forEach(element => {
76 //          this.definitionType.push(element)
77 //        });          
78 //    }, error=>{
79 //      window.alert('error' + error);
80 //    })
81 //
82 //    this.catalogCreateService.getDerivedFrom()
83 //    .subscribe(data=>{
84 //        console.log(data);
85 //        data.forEach(element => {
86 //          this.derivedFrom.push(element)
87 //        });          
88 //    }, error=>{
89 //      window.alert('error' + error);
90 //    })
91   }
92   CreateCatalog(){
93           this.catalog.modelName=this.CatalogFormData.controls['modelName'].value;
94             this.catalog.updatedBy=this.CatalogFormData.controls['updatedBy'].value
95             this.catalog.tags=this.CatalogFormData.controls['tags'].value
96             this.catalog.definitionType=this.CatalogFormData.controls['definitionType'].value
97             this.catalog.derivedFrom=this.CatalogFormData.controls['derivedFrom'].value
98             this.catalog.description=this.CatalogFormData.controls['description'].value
99             this.catalog.definition=this.data;
100             console.log(this.catalog);
101     let catalogState = {
102       catalog: this.catalog
103     }
104     this.store.dispatch(new SetCatalogState(catalogState));
105     
106       this.catalogCreateService.saveCatalog(this.catalog)
107       .subscribe(response=>{
108         this.alertService.success("save success")
109       },
110       error=>{
111         this.alertService.error('Error saving resources');
112       })
113  
114   }
115
116   onChange($event) {
117     this.data=JSON.parse($event.srcElement.value); 
118     console.log(this.data);
119   };
120 }