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

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

    
21
export class CuratorComponent implements OnInit {
22

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

    
27
  public curatorsEnabled;
28
  public newCurator;
29

    
30
  public communityId = null;
31

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

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

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

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

    
56

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

    
108
  }
109

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

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

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

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

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

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

    
189
    this.showLoading = false;
190
  }
191

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

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

    
222

    
223
  updateCurator() {
224
    if (!Session.isLoggedIn()) {
225
      this._router.navigate(['/user-info'], {
226
        queryParams:
227
          {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
228
      });
229
    } else {
230
      if ((this.hasChanged || this.affiliationsChanged) && this.curator && this.curator.name && this.curator.name !== '') {
231
        this.showLoading = true;
232
        if (this.file) {
233
          this.utilitiesService.uploadPhoto(this.properties.utilsService + '/upload/' + this.curator._id, this.file).subscribe((res) => {
234
              if (this.curator.photo && this.curator.photo !== '') {
235
                this.utilitiesService.deletePhoto(this.properties.utilsService + '/delete/' + this.curator.photo).subscribe();
236
              }
237
              this.curator.photo = res.filename;
238
              this.curatorService.updateCurator(this.properties.adminToolsAPIURL + 'curator',
239
                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.adminToolsAPIURL + 'curator',
260
            this.curator).subscribe((curator) => {
261
              if (curator) {
262
                this.handleSuccessfulSave('Your data has been saved successfully!');
263
                this.resetChange();
264
              }
265
            },
266
            error => {
267
              this.handleUpdateError('An error has occurred. Try again later!', error);
268
              this.resetChange();
269
            });
270
        }
271
      }
272
    }
273
  }
274

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

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

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