dbdd2928450c79bfdd4f1e8a54ba1e6766d41343
[portal.git] / portal-FE-common / src / app / pages / dashboard-widget-catalog / dashboard-widget-catalog.component.ts
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 import { Component, OnInit } from '@angular/core';
39 import { GridsterConfig, GridsterItem } from 'angular-gridster2';
40 import { WidgetCatalogService } from '../../shared/services/widget-catalog/widget-catalog.service';
41 import { IWidgetCatalog } from '../../shared/model/widget-catalog.model';
42 import { Observable } from 'rxjs';
43 import { inflateRaw } from 'zlib';
44 import { UsersService } from 'src/app/shared/services/users/users.service';
45
46 @Component({
47   selector: 'app-dashboard-widget-catalog',
48   templateUrl: './dashboard-widget-catalog.component.html',
49   styleUrls: ['./dashboard-widget-catalog.component.scss']
50 })
51 export class DashboardWidgetCatalogComponent implements OnInit {
52
53  
54   widgetCatalogData: IWidgetCatalog[];
55   isCommonError: boolean = false;
56   isApiRunning: boolean = true;
57   userName: string;
58
59
60   get options(): GridsterConfig {
61     return this.widgetCatalogService.options;
62   } get layout(): GridsterItem[] {
63     return this.widgetCatalogService.layout;
64   } constructor(private widgetCatalogService: WidgetCatalogService, private userService: UsersService) {
65
66   }
67
68   ngOnInit() {
69     const widgetCatalogObservable = this.widgetCatalogService.getWidgetCatalog();
70     this.widgetCatalogService.layout = [];
71     this.getUserWidgets(this.userName);
72   }
73
74
75   getUserWidgets(loginName: string) {
76     const widgetCatalogUserObservable = this.userService.getUserProfile();
77     widgetCatalogUserObservable.subscribe((userProfile: any) => {
78       //console.log('UserProfile is ' + userProfile);
79       if (userProfile) {
80         const widgetCatalogObservable = this.widgetCatalogService.getUserWidgets(userProfile.orgUserId);
81         widgetCatalogObservable.subscribe(data => {
82           //console.log("What is coming from backend" + JSON.parse(data));
83           this.widgetCatalogData = data;
84           console.log(this.widgetCatalogData);
85           for (let entry of this.widgetCatalogData) {
86             if (entry[1] == 'Events' || entry[1] == 'News' || entry[1] == 'Resources') {
87               if(entry[4] === 'S' || entry[4] === null)
88               {
89               var appCatalog = {
90                 id: entry[0],
91                 name: entry[1],
92                 headerName: entry[2],
93                 //select: (entry[4] === 'S' || entry[4] === null) ? true : false
94               };
95               this.widgetCatalogService.addItem(appCatalog);
96             }
97             }
98           }
99         });
100       }
101     });
102
103   }
104
105   getUserProfile(): Observable<any> {
106     const widgetCatalogObservable = this.userService.getUserProfile();
107     return widgetCatalogObservable;
108   }
109
110   storeSelection(widgetCatalogData: any) {
111     console.log("Store selection called " + widgetCatalogData.select);
112     if (widgetCatalogData && widgetCatalogData.select) {
113       var appData = {
114         widgetId: widgetCatalogData.id,
115         select: widgetCatalogData.select,
116         pending:false
117       };
118       this.widgetCatalogService.updateWidgetCatalog(appData).subscribe(data => {
119         //console.log("Update App sort data" + data);
120       }, error => {
121         console.log('updateWidgetCatalog error' + error);
122       });
123     }
124
125   }
126 }