Angular upgrade - Dynamic widget,widget catalog
[portal.git] / portal-FE-common / src / app / pages / widget-catalog / widget-catalog.component.ts
1 import { Component, OnInit } from '@angular/core';
2 import { GridsterConfig, GridsterItem } from 'angular-gridster2';
3 import { WidgetCatalogService } from '../../shared/services/widget-catalog/widget-catalog.service';
4 import { IWidgetCatalog } from '../../shared/model/widget-catalog.model';
5 import { Observable } from 'rxjs';
6 import { inflateRaw } from 'zlib';
7 import { UsersService } from 'src/app/shared/services/users/users.service';
8
9
10 @Component({
11   selector: 'app-widget-catalog',
12   templateUrl: './widget-catalog.component.html',
13   styleUrls: ['./widget-catalog.component.scss']
14 })
15 export class WidgetCatalogComponent implements OnInit {
16   widgetCatalogData: IWidgetCatalog[];
17   isCommonError: boolean = false;
18   isApiRunning: boolean = true;
19   userName: string;
20
21
22   get options(): GridsterConfig {
23     return this.widgetCatalogService.options;
24   } get layout(): GridsterItem[] {
25     return this.widgetCatalogService.layout;
26   } constructor(private widgetCatalogService: WidgetCatalogService, private userService: UsersService) {
27
28   }
29
30   ngOnInit() {
31     const widgetCatalogObservable = this.widgetCatalogService.getWidgetCatalog();
32     this.widgetCatalogService.layout = [];
33     this.getUserWidgets(this.userName);
34   }
35
36
37   getUserWidgets(loginName: string) {
38     const widgetCatalogUserObservable = this.userService.getUserProfile();
39     widgetCatalogUserObservable.subscribe((userProfile: any) => {
40       //console.log('UserProfile is ' + userProfile);
41       if (userProfile) {
42         const widgetCatalogObservable = this.widgetCatalogService.getUserWidgets(userProfile.orgUserId);
43         widgetCatalogObservable.subscribe(data => {
44           //console.log("What is coming from backend" + JSON.parse(data));
45           this.widgetCatalogData = data;
46           console.log(this.widgetCatalogData);
47           for (let entry of this.widgetCatalogData) {
48             if (entry[1] == 'Events' || entry[1] == 'News' || entry[1] == 'Resources') {
49               var appCatalog = {
50                 id: entry[0],
51                 name: entry[1],
52                 headerName: entry[2],
53                 select: (entry[4] == 'S' || entry[4] === null) ? true : false
54               };
55               this.widgetCatalogService.addItem(appCatalog);
56             }
57           }
58         });
59       }
60     });
61
62   }
63
64   getUserProfile(): Observable<any> {
65     const widgetCatalogObservable = this.userService.getUserProfile();
66     return widgetCatalogObservable;
67   }
68
69   storeSelection(widgetCatalogData: any) {
70     console.log("Store selection called " + widgetCatalogData.select);
71     if (widgetCatalogData) {
72       var appData = {
73         widgetId: widgetCatalogData.id,
74         select: widgetCatalogData.select,
75         pending:false
76       };
77       this.widgetCatalogService.updateWidgetCatalog(appData).subscribe(data => {
78         //console.log("Update App sort data" + data);
79       }, error => {
80         console.log('updateWidgetCatalog error' + error);
81       });
82     }
83
84   }
85 }