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} 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

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

    
20
export class CuratorComponent implements OnInit {
21

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

    
26
  public curatorsEnabled;
27
  public newCurator;
28

    
29
  public communityId = null;
30

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

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

    
43
  @ViewChild('privacyStatement') privacyStatement: AlertModal;
44

    
45
  constructor(private element: ElementRef,
46
              private route: ActivatedRoute,
47
              private _router: Router,
48
              private curatorService: CuratorService,
49
              private utilitiesService: UtilitiesService,
50
              private helpContentService: HelpContentService) {
51
  }
52

    
53

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

    
102
  }
103

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

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

    
164
  private change() {
165
    this.hasChanged = true;
166
    this.affiliationsChanged = true;
167
  }
168

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

    
174
  public resetMessages() {
175
    this.successfulSaveMessage = '';
176
    this.updateErrorMessage = '';
177
  }
178

    
179
  handleUpdateError(message: string, error) {
180
    this.resetMessages();
181
    this.updateErrorMessage = message;
182
    console.log('Server responded: ' + error);
183

    
184
    this.showLoading = false;
185
  }
186

    
187
  handleSuccessfulSave(message) {
188
    this.resetMessages();
189
    this.showLoading = false;
190
    HelperFunctions.scroll();
191
    this.successfulSaveMessage = message;
192
  }
193

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

    
217

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

    
270
  onNameChange() {
271
    this.hasChanged = true;
272
    if (!this.curator.name || this.curator.name === '') {
273
      this.enabled = false;
274
    } else {
275
      this.enabled = true;
276
    }
277
  }
278

    
279
  removePhoto() {
280
    this.deletePhoto = true;
281
    this.hasChanged = true;
282
    this.photo = 'assets/common-assets/curator-default.png';
283
  }
284

    
285
  privacy() {
286
    this.privacyStatement.cancelButton = false;
287
    this.privacyStatement.okButtonText = 'Close';
288
    this.privacyStatement.alertTitle = 'Privacy policy statement'
289
    this.privacyStatement.open();
290
  }
291
}
(3-3/4)