Project

General

Profile

1
import {Component, OnDestroy, OnInit, ViewChild} from "@angular/core";
2
import {StakeholderService} from "../openaireLibrary/monitor/services/stakeholder.service";
3
import {EnvProperties} from "../openaireLibrary/utils/properties/env-properties";
4
import {EnvironmentSpecificService} from "../openaireLibrary/utils/properties/environment-specific.service";
5
import {Stakeholder, Visibility} from "../openaireLibrary/monitor/entities/stakeholder";
6
import {Subscriber, zip} from "rxjs";
7
import {StakeholderUtils} from "../utils/indicator-utils";
8
import {FormBuilder, FormGroup} from "@angular/forms";
9
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
10
import {Option} from "../openaireLibrary/sharedComponents/input/input.component";
11
import {Title} from "@angular/platform-browser";
12
import {UserManagementService} from "../openaireLibrary/services/user-management.service";
13
import {Session} from "../openaireLibrary/login/utils/helper.class";
14
import {EditStakeholderComponent} from "../general/edit-stakeholder/edit-stakeholder.component";
15
import {properties} from "../../environments/environment";
16

    
17
declare var UIkit;
18

    
19
@Component({
20
  selector: 'home',
21
  templateUrl: "./manageStakeholders.component.html",
22
  styleUrls: ['manageStakeholders.component.css']
23
})
24
export class ManageStakeholdersComponent implements OnInit, OnDestroy {
25
  
26
  public properties: EnvProperties;
27
  public loading: boolean = true;
28
  public stakeholderUtils: StakeholderUtils = new StakeholderUtils();
29
  public defaultStakeholders: Stakeholder[];
30
  public stakeholders: Stakeholder[];
31
  public alias: string[];
32
  public stakeholder: Stakeholder;
33
  public index: number;
34
  public user = null;
35
  /**
36
   *  Filtered Stakeholders
37
   */
38
  public displayDefaultStakeholders: Stakeholder[];
39
  public displayStakeholders: Stakeholder[];
40
  /**
41
   * Top filters
42
   */
43
  public filters: FormGroup;
44
  public all: Option = {
45
    value: 'all',
46
    label: 'All'
47
  };
48
  
49
  public callback: Function;
50
  
51
  /**
52
   * Grid or List View
53
   */
54
  private subscriptions: any[] = [];
55
  @ViewChild('editStakeholderModal') editStakeholderModal: AlertModal;
56
  @ViewChild('deleteStakeholderModal') deleteStakeholderModal: AlertModal;
57
  @ViewChild('editStakeholderComponent') editStakeholderComponent: EditStakeholderComponent;
58
  
59
  tab: "all" | "templates" | "profiles" = "all";
60
  
61
  constructor(private stakeholderService: StakeholderService,
62
              private userManagementService: UserManagementService,
63
              private propertiesService: EnvironmentSpecificService,
64
              private title: Title,
65
              private fb: FormBuilder) {
66
  }
67
  
68
  ngOnInit(): void {
69
    this.buildFilters();
70
    this.properties = properties;
71
    this.title.setTitle('Manage profiles');
72
    this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
73
      this.user = user;
74
    }));
75
    let data = zip(
76
      this.stakeholderService.getDefaultStakeholders(this.properties.monitorServiceAPIURL),
77
      this.stakeholderService.getMyStakeholders(this.properties.monitorServiceAPIURL),
78
      this.stakeholderService.getAlias(this.properties.monitorServiceAPIURL)
79
    );
80
    this.subscriptions.push(data.subscribe(res => {
81
      this.defaultStakeholders = res[0];
82
      this.stakeholders = res[1];
83
      this.displayDefaultStakeholders = res[0];
84
      this.displayStakeholders = res[1];
85
      this.alias = res[2];
86
      this.loading = false;
87
    }));
88

    
89
    this.subscriptions.push(UIkit.util.on(document, 'hidden', '#edit_modal', (): void => {
90
      this.editStakeholderComponent.removePhoto();
91
    }));
92
  }
93
  
94
  ngOnDestroy(): void {
95
    this.subscriptions.forEach(value => {
96
      if (value instanceof Subscriber) {
97
        value.unsubscribe();
98
      } else if (value instanceof Function) {
99
        value();
100
      }
101
    });
102
  }
103
  
104
  hide(element: any) {
105
    UIkit.dropdown(element).hide();
106
  }
107
  
108
  
109
  private buildFilters() {
110
    this.filters = this.fb.group({
111
      status: this.fb.control('all'),
112
      keyword: this.fb.control('')
113
    });
114
    this.subscriptions.push(this.filters.get('status').valueChanges.subscribe(value => {
115
      console.debug(value);
116
      this.onStatusChange(value);
117
    }));
118
    this.subscriptions.push(this.filters.get('keyword').valueChanges.subscribe(value => {
119
      this.onKeywordChange(value);
120
    }));
121
  }
122
  
123
  onStatusChange(value) {
124
    this.displayDefaultStakeholders = this.filterStatus(this.defaultStakeholders, value);
125
    this.displayStakeholders = this.filterStatus(this.stakeholders, value);
126
  }
127
  
128
  onKeywordChange(value) {
129
    this.displayDefaultStakeholders = this.filterByKeyword(this.defaultStakeholders, value);
130
    this.displayStakeholders = this.filterByKeyword(this.stakeholders, value);
131
  }
132
  
133
  private filterStatus(stakeholders: Stakeholder[], value): Stakeholder[] {
134
    if (value === 'all') {
135
      return stakeholders;
136
    } else {
137
      return stakeholders.filter(stakeholder => stakeholder.visibility == value);
138
    }
139
  }
140
  
141
  private filterByKeyword(stakeholders: Stakeholder[], value): Stakeholder[] {
142
    if (value === null || value === '') {
143
      return stakeholders;
144
    } else {
145
      return stakeholders.filter(stakeholder =>
146
        stakeholder.name && stakeholder.name.toLowerCase().includes(value.toLowerCase()) ||
147
        stakeholder.type && stakeholder.type.toLowerCase().includes(value.toLowerCase()) ||
148
        stakeholder.index_id && stakeholder.index_id.toLowerCase().includes(value.toLowerCase()) ||
149
        stakeholder.index_shortName && stakeholder.index_shortName.toLowerCase().includes(value.toLowerCase()) ||
150
        stakeholder.index_name && stakeholder.index_name.toLowerCase().includes(value.toLowerCase())
151
      );
152
    }
153
  }
154
  
155
  public editStakeholder(stakeholder: Stakeholder = null, isDefault: boolean = false) {
156
    if (isDefault) {
157
      this.index = (stakeholder) ? this.defaultStakeholders.findIndex(value => value._id === stakeholder._id) : -1;
158
    } else {
159
      this.index = (stakeholder) ? this.stakeholders.findIndex(value => value._id === stakeholder._id) : -1;
160
    }
161
    if (!stakeholder) {
162
      this.stakeholder = new Stakeholder(null, null, null,
163
        null, null, null, null, null);
164
    } else {
165
      this.stakeholder = stakeholder;
166
    }
167
    this.editStakeholderComponent.init(this.stakeholder, this.alias, this.defaultStakeholders, isDefault, this.index === -1);
168
    if (this.index !== -1) {
169
      this.callback = (stakeholder: Stakeholder) => {
170
        if (stakeholder.defaultId == null) {
171
          this.defaultStakeholders[this.index] = stakeholder;
172
        } else {
173
          this.stakeholders[this.index] = stakeholder;
174
        }
175
      };
176
      this.editStakeholderModal.okButtonText = 'Save Changes';
177
    } else {
178
      this.callback = (stakeholder: Stakeholder) => {
179
        if (stakeholder.defaultId === null) {
180
          this.defaultStakeholders.push(stakeholder);
181
        } else {
182
          this.stakeholders.push(stakeholder);
183
        }
184
      };
185
      this.editStakeholderModal.okButtonText = 'Create';
186
    }
187
    this.editStakeholderModal.cancelButtonText = 'Cancel';
188
    this.editStakeholderModal.okButtonLeft = false;
189
    this.editStakeholderModal.alertMessage = false;
190
    this.editStakeholderModal.open();
191
  }
192
  
193
  public deleteStakeholderOpen(stakeholder: Stakeholder) {
194
    this.stakeholder = stakeholder;
195
    this.deleteStakeholderModal.alertTitle = 'Delete ' + this.stakeholder.index_name;
196
    this.deleteStakeholderModal.cancelButtonText = 'No';
197
    this.deleteStakeholderModal.okButtonText = 'Yes';
198
    this.deleteStakeholderModal.message = 'This stakeholder will permanently be deleted. Are you sure you want to proceed?';
199
    this.deleteStakeholderModal.open();
200
  }
201
  
202
  public deleteStakeholder() {
203
    if (!this.stakeholder.defaultId) {
204
      this.index = (this.stakeholder) ? this.defaultStakeholders.findIndex(value => value._id === this.stakeholder._id) : -1;
205
    } else {
206
      this.index = (this.stakeholder) ? this.stakeholders.findIndex(value => value._id === this.stakeholder._id) : -1;
207
    }
208
    this.subscriptions.push(this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, [this.stakeholder._id]).subscribe(() => {
209
      UIkit.notification(this.stakeholder.name+ ' has been <b>successfully deleted</b>', {
210
        status: 'success',
211
        timeout: 6000,
212
        pos: 'bottom-right'
213
      });
214
      if (!this.stakeholder.defaultId) {
215
        this.defaultStakeholders.splice(this.index, 1);
216
      } else {
217
        this.stakeholders.splice(this.index, 1);
218
      }
219
    }, error => {
220
      UIkit.notification('An error has occurred. Please try again later', {
221
        status: 'danger',
222
        timeout: 6000,
223
        pos: 'bottom-right'
224
      });
225
    }));
226
  }
227
  
228
  changeStakeholderStatus(stakeholder: Stakeholder, visibility: Visibility) {
229
    let path = [
230
      stakeholder._id
231
    ];
232
    this.subscriptions.push(this.stakeholderService.changeVisibility(this.properties.monitorServiceAPIURL, path, visibility).subscribe(visibility => {
233
      stakeholder.visibility = visibility;
234
      UIkit.notification(stakeholder.name+ '\'s status has been <b>successfully changed</b> to ' + stakeholder.visibility.toLowerCase(), {
235
        status: 'success',
236
        timeout: 6000,
237
        pos: 'bottom-right'
238
      });
239
    }, error => {
240
      UIkit.notification('An error has occurred. Please try again later', {
241
        status: 'danger',
242
        timeout: 6000,
243
        pos: 'bottom-right'
244
      });
245
    }));
246
  }
247
  
248
  public isManager(): boolean {
249
    return this.isCurator() || (Session.isKindOfMonitorManager(this.user));
250
  }
251
  
252
  public isProfileManager(stakeholder: Stakeholder): boolean {
253
    return this.isCurator() || (Session.isManager(stakeholder.type, stakeholder.alias, this.user));
254
  }
255
  
256
  public isCurator(): boolean {
257
    return this.isAdmin() || Session.isMonitorCurator(this.user);
258
  }
259
  
260
  public isAdmin(): boolean {
261
    return Session.isPortalAdministrator(this.user);
262
  }
263
  
264
  changeTab(tab: "all" | "templates" | "profiles") {
265
    this.tab = tab;
266
  }
267
}
(4-4/5)