added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / layout / settings / settings.component.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 import { Component, OnInit } from '@angular/core';\r
18 import { CookieService } from 'ngx-cookie-service';\r
19 import { UserService } from 'app/shared/services/user.service';\r
20 import { GroupService } from 'app/shared/services/group.service';\r
21 import { routerTransition } from 'app/router.animations';\r
22 import { AlertSnackbarComponent } from 'app/shared/modules/alert-snackbar/alert-snackbar.component';\r
23 import { Group } from 'app/shared/models/group.model';\r
24 import { MatSnackBar } from '@angular/material';\r
25 \r
26 @Component({\r
27   selector: 'app-settings',\r
28   templateUrl: './settings.component.pug',\r
29   styleUrls: ['./settings.component.scss'],\r
30   animations: [routerTransition()]\r
31 })\r
32 \r
33 export class SettingsComponent implements OnInit {\r
34   defaultGroupEnabled = false;\r
35   private defaultGroup;\r
36   private eligibleGroups;\r
37   private currentUser;\r
38   private defaultGroupId;\r
39 \r
40   constructor(private cookie: CookieService,\r
41     private user: UserService,\r
42     private _group: GroupService,\r
43     private snack: MatSnackBar\r
44   ) { }\r
45 \r
46   ngOnInit() {\r
47 \r
48     this.currentUser = JSON.parse(this.cookie.get('currentUser'));\r
49 \r
50     this._group.find({ $limit: -1 }).subscribe((result) => {\r
51       if (result)\r
52         this.eligibleGroups = result;\r
53     });\r
54 \r
55     this.user.get(this.currentUser._id).subscribe((result) => {\r
56       if (result)\r
57         this.defaultGroupId = result['defaultGroup'];\r
58         this.defaultGroupEnabled = result['defaultGroupEnabled'];\r
59 \r
60         this._group.get(this.defaultGroupId).subscribe((result) => {\r
61         this.defaultGroup = result;\r
62       });\r
63     });\r
64   }\r
65 \r
66   changDefaultGroup(group: Group) {\r
67     this.defaultGroup = group;\r
68   }\r
69 \r
70   enableDefaultGroup() {\r
71     this.defaultGroupEnabled = true;\r
72   }\r
73 \r
74   disableDefaultGroup() {\r
75     this.defaultGroupEnabled = false;\r
76     \r
77   }\r
78 \r
79   update() {\r
80 \r
81     this.currentUser.defaultGroupEnabled = this.defaultGroupEnabled;\r
82     this.currentUser.defaultGroup = this.defaultGroup;\r
83     this.cookie.set('currentUser', JSON.stringify(this.currentUser));\r
84     \r
85 \r
86     \r
87     let userPatch = {\r
88       _id: this.currentUser._id,\r
89       defaultGroup: this.defaultGroup._id,\r
90       defaultGroupEnabled: this.defaultGroupEnabled\r
91     };\r
92 \r
93     this.user.patch(userPatch).subscribe((res) => {\r
94       let snackMessage = 'Successfully Updated Settings';\r
95                 this.snack.openFromComponent(AlertSnackbarComponent, {\r
96                     duration: 1500,\r
97                     data: {\r
98                         message: snackMessage\r
99                     }\r
100                 })\r
101     })\r
102   }\r
103 }\r