Project

General

Profile

1
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3

    
4
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
5

    
6
import {Session, User} from '../../openaireLibrary/login/utils/helper.class';
7
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
8
import {CuratorService} from '../../openaireLibrary/connect/curators/curator.service';
9
import {Curator} from '../../openaireLibrary/utils/entities/CuratorInfo';
10
import {HelperFunctions} from '../../openaireLibrary/utils/HelperFunctions.class';
11
import {UtilitiesService} from '../../openaireLibrary/services/utilities.service';
12
import {HelpContentService} from '../../services/help-content.service';
13
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
14
import {UserManagementService} from '../../openaireLibrary/services/user-management.service';
15
import {Title} from '@angular/platform-browser';
16

    
17
@Component({
18
  selector: 'curator',
19
  templateUrl: './curator.component.html',
20
})
21

    
22
export class CuratorComponent implements OnInit {
23

    
24
  public showLoading = true;
25
  public updateErrorMessage = '';
26
  public successfulSaveMessage = '';
27

    
28
  public curatorsEnabled;
29
  public newCurator;
30

    
31
  public communityId = null;
32

    
33
  public affiliationsChanged = false;
34
  public hasChanged = false;
35
  public curatorId = null;
36
  public curator: Curator = null;
37
  public photo: any = null;
38
  public properties: EnvProperties = null;
39
  public user: User;
40

    
41
  private file: File = null;
42
  private maxsize: number = 200 * 1024;
43
  public enabled = true;
44
  private deletePhoto = false;
45

    
46
  @ViewChild('privacyStatement') privacyStatement: AlertModal;
47

    
48
  constructor(private element: ElementRef,
49
              private route: ActivatedRoute,
50
              private _router: Router,
51
              private title: Title,
52
              private curatorService: CuratorService,
53
              private utilitiesService: UtilitiesService,
54
              private helpContentService: HelpContentService,
55
              private userManagementService: UserManagementService) {
56
  }
57

    
58

    
59
  ngOnInit() {
60
    this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
61
      this.properties = data.envSpecific;
62
      if (!Session.isLoggedIn()) {
63
        this._router.navigate(['/user-info'], {
64
          queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
65
        });
66
      } else {
67
        this.route.queryParams.subscribe((params) => {
68
          this.communityId = params['communityId'];
69
          this.title.setTitle('Administration Dashboard | Personal Info');
70
          this.showLoading = true;
71
          this.updateErrorMessage = '';
72
          this.userManagementService.getUserInfo().subscribe(user => {
73
            this.user = user;
74
            this.curatorId = this.user.id;
75
            this.curatorService.getCurator(this.properties, this.curatorId).subscribe(
76
              curator => {
77
                if (curator && Object.keys(curator).length > 0) {
78
                  this.curator = curator;
79
                  this.curator.email = this.user.email;
80
                  if (this.curator.photo && this.curator.photo !== '') {
81
                    this.photo = this.properties.utilsService + '/download/' + this.curator.photo;
82
                  } else {
83
                    this.photo = 'assets/common-assets/curator-default.png';
84
                  }
85
                  this.curatorsPageStatus();
86
                  this.showLoading = false;
87
                  HelperFunctions.scroll();
88
                } else {
89
                  this.newCurator = true;
90
                  this.curator = new Curator();
91
                  this.curator._id = this.curatorId;
92
                  this.curator.email = this.user.email;
93
                  this.curator.name = this.user.fullname;
94
                  this.curator.affiliations = [];
95
                  this.curator.bio = '';
96
                  this.curator.photo = null;
97
                  this.photo = 'assets/common-assets/curator-default.png';
98
                  this.showLoading = false;
99
                  HelperFunctions.scroll();
100
                }
101
              },
102
              error => {
103
              }
104
            );
105
          });
106
        });
107
      }
108
    });
109

    
110
  }
111

    
112
  public resetForm() {
113
    if (!Session.isLoggedIn()) {
114
      this._router.navigate(['/user-info'], {
115
        queryParams:
116
          {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
117
      });
118
    } else {
119
      if (this.curatorId != null && this.curatorId !== '') {
120
        this.showLoading = true;
121
        this.updateErrorMessage = '';
122
        this.curatorService.getCurator(this.properties, this.curatorId).subscribe(
123
          curator => {
124
            if (curator) {
125
              this.curator = curator;
126
              this.curator.email = this.user.email;
127
              if (this.curator.photo && this.curator.photo !== '') {
128
                this.photo = this.properties.utilsService + '/download/' + this.curator.photo;
129
              } else {
130
                this.photo = 'assets/common-assets/curator-default.png';
131
              }
132
              this.showLoading = false;
133
              this.curatorsPageStatus();
134
              HelperFunctions.scroll();
135
            } else {
136
              this.newCurator = true;
137
              this.curator = new Curator();
138
              this.curator._id = this.curatorId;
139
              this.curator.email = this.user.email
140
              this.curator.name = this.user.fullname;
141
              this.curator.affiliations = [];
142
              this.curator.bio = '';
143
              this.curator.photo = null;
144
              this.photo = 'assets/common-assets/curator-default.png';
145
              this.showLoading = false;
146
              HelperFunctions.scroll();
147
            }
148
          },
149
          error => {
150
          }
151
        );
152
      }
153
      this.resetChange();
154
    }
155
  }
156

    
157
  private curatorsPageStatus() {
158
    this.helpContentService.getCommunityFull(this.communityId, this.properties.adminToolsAPIURL).subscribe((community) => {
159
      for (let page of community.pages) {
160
        if (page['route'] === '/curators') {
161
          this.curatorsEnabled = page['isEnabled'];
162
          return;
163
        }
164
      }
165
      this.curatorsEnabled = false;
166
      console.log(this.curatorsEnabled);
167
    });
168
  }
169

    
170
  private change() {
171
    this.hasChanged = true;
172
    this.affiliationsChanged = true;
173
  }
174

    
175
  private resetChange() {
176
    this.hasChanged = false;
177
    this.affiliationsChanged = false;
178
  }
179

    
180
  public resetMessages() {
181
    this.successfulSaveMessage = '';
182
    this.updateErrorMessage = '';
183
  }
184

    
185
  handleUpdateError(message: string, error) {
186
    this.resetMessages();
187
    this.updateErrorMessage = message;
188
    console.log('Server responded: ' + error);
189

    
190
    this.showLoading = false;
191
  }
192

    
193
  handleSuccessfulSave(message) {
194
    this.resetMessages();
195
    this.showLoading = false;
196
    HelperFunctions.scroll();
197
    this.successfulSaveMessage = message;
198
  }
199

    
200
  fileChangeEvent(event) {
201
    this.showLoading = true;
202
    if (event.target.files && event.target.files[0]) {
203
      this.file = event.target.files[0];
204
      if (this.file.type !== 'image/png' && this.file.type !== 'image/jpeg') {
205
        this.handleUpdateError('You must choose a file with type: image/png or image/jpeg!', null);
206
        this.file = null;
207
      } else if (this.file.size > this.maxsize) {
208
        this.handleUpdateError('File exceeds size\'s limit! Maximum resolution is 256x256 pixels.', null);
209
        this.file = null;
210
      } else {
211
        this.updateErrorMessage = '';
212
        const reader = new FileReader();
213
        reader.readAsDataURL(this.file);
214
        reader.onload = () => {
215
          this.photo = reader.result;
216
          this.showLoading = false;
217
          HelperFunctions.scroll();
218
        };
219
      }
220
    }
221
  }
222

    
223

    
224
  updateCurator() {
225
    if (!Session.isLoggedIn()) {
226
      this._router.navigate(['/user-info'], {
227
        queryParams:
228
          {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
229
      });
230
    } else {
231
      if ((this.hasChanged || this.affiliationsChanged) && this.curator && this.curator.name && this.curator.name !== '') {
232
        this.showLoading = true;
233
        if (this.file) {
234
          this.utilitiesService.uploadPhoto(this.properties.utilsService + '/upload/' + this.curator._id, this.file).subscribe((res) => {
235
              if (this.curator.photo && this.curator.photo !== '') {
236
                this.utilitiesService.deletePhoto(this.properties.utilsService + '/delete/' + this.curator.photo).subscribe();
237
              }
238
              this.curator.photo = res.filename;
239
              this.curatorService.updateCurator(this.properties, this.curator).subscribe((curator) => {
240
                  if (curator) {
241
                    this.handleSuccessfulSave('Your data has been saved successfully!');
242
                    this.newCurator = false;
243
                    this.resetChange();
244
                  }
245
                },
246
                error => {
247
                  this.handleUpdateError('An error has occurred. Try again later!', error);
248
                  this.resetChange();
249
                });
250
            }, error => {
251
              this.handleUpdateError('An error has occurred during photo uploading.', error);
252
            }
253
          );
254
        } else {
255
          if (this.deletePhoto) {
256
            this.utilitiesService.deletePhoto(this.properties.utilsService + '/delete/' + this.curator.photo).subscribe();
257
            this.curator.photo = '';
258
          }
259
          this.curatorService.updateCurator(this.properties, this.curator).subscribe((curator) => {
260
              if (curator) {
261
                this.handleSuccessfulSave('Your data has been saved successfully!');
262
                this.resetChange();
263
              }
264
            },
265
            error => {
266
              this.handleUpdateError('An error has occurred. Try again later!', error);
267
              this.resetChange();
268
            });
269
        }
270
      }
271
    }
272
  }
273

    
274
  onNameChange() {
275
    this.hasChanged = true;
276
    if (!this.curator.name || this.curator.name === '') {
277
      this.enabled = false;
278
    } else {
279
      this.enabled = true;
280
    }
281
  }
282

    
283
  removePhoto() {
284
    this.deletePhoto = true;
285
    this.hasChanged = true;
286
    this.photo = 'assets/common-assets/curator-default.png';
287
  }
288

    
289
  privacy() {
290
    this.privacyStatement.cancelButton = false;
291
    this.privacyStatement.okButtonText = 'Close';
292
    this.privacyStatement.alertTitle = 'Privacy policy statement';
293
    this.privacyStatement.open();
294
  }
295
}
(3-3/4)