Resouce creation- save resouce to backend
[ccsdk/cds.git] / cds-ui / client / src / app / feature-modules / resource-definition / resource-edit / resource-edit.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 } from '@angular/core';
22 import { IResources } from 'src/app/common/core/store/models/resources.model';
23 import { IResourcesState } from 'src/app/common/core/store/models/resourcesState.model';
24 import { LoadResourcesSuccess,UpdateResources,SetResourcesState } from 'src/app/common/core/store/actions/resources.action';
25 import { Store } from '@ngrx/store';
26 import { IAppState } from '../../../common/core/store/state/app.state';
27 import { JsonEditorComponent, JsonEditorOptions } from 'ang-jsoneditor';
28 import { Observable } from 'rxjs';
29 import { A11yModule } from '@angular/cdk/a11y';
30 import { ResourceEditService } from './resource-edit.service';
31
32 @Component({
33   selector: 'app-resource-edit',
34   templateUrl: './resource-edit.component.html',
35   styleUrls: ['./resource-edit.component.scss']
36 })
37 export class ResourceEditComponent implements OnInit {
38
39     resources:IResources;
40     data:IResources;
41     rdState: Observable<IResourcesState>;
42     designerMode: boolean = false;
43     editorMode: boolean = true;
44     viewText: string = "Open in Editor Mode";
45     @ViewChild(JsonEditorComponent) editor: JsonEditorComponent;
46     options = new JsonEditorOptions();
47   
48   constructor(private store: Store<IAppState>, private resourceEditService: ResourceEditService) {
49         this.rdState = this.store.select('resources');
50     this.options.mode = 'text';
51     this.options.modes = [ 'text', 'tree', 'view'];
52     this.options.statusBar = false;    
53   }
54
55   ngOnInit() {
56     this.rdState.subscribe(
57       resourcesdata => {
58         var resourcesState: IResourcesState = { resources: resourcesdata.resources, isLoadSuccess: resourcesdata.isLoadSuccess, isSaveSuccess: resourcesdata.isSaveSuccess, isUpdateSuccess: resourcesdata.isUpdateSuccess };
59           this.resources=resourcesState.resources;
60     })     
61   }
62
63  metaDataDetail(data: IResources) {
64     this.data=data;       
65   }
66     
67  sourcesDetails(data: IResources) {
68     this.data=data; 
69  }
70     
71  onChange($event) {
72       this.data=JSON.parse($event.srcElement.value);
73   };
74   
75  updateResourcesState(){
76       console.log(this.data);
77       let resourcesState = {
78       resources: this.data,
79       isLoadSuccess: true,
80       isUpdateSuccess:true,
81       isSaveSuccess:true
82     }  
83    this.store.dispatch(new SetResourcesState(resourcesState));   
84   }
85     
86  changeView() {
87     if(this.viewText == 'Open in Editor Mode') {
88       this.editorMode =  true;
89       this.designerMode = false;
90       this.viewText = 'Open in Form Mode'
91     } else {
92       this.editorMode =  false;
93       this.designerMode = true;
94       this.viewText = 'Open in Editor Mode'
95     }
96   } 
97
98   saveToBackend() {
99     this.resourceEditService.saveResource(this.data)
100     .subscribe(response=>{
101       window.alert("save success");
102     },
103     error=>{
104       window.alert('Error saving resources');
105     })
106   }
107 }