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

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

    
19
export class CuratorComponent implements OnInit {
20

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

    
25
  public curatorsEnabled;
26
  public newCurator;
27

    
28
  public communityId = null;
29

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

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

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

    
50

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

    
99
  }
100

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

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

    
161
  private change() {
162
    this.hasChanged = true;
163
    this.affiliationsChanged = true;
164
  }
165

    
166
  private resetChange() {
167
    this.hasChanged = false;
168
    this.affiliationsChanged = false;
169
  }
170

    
171
  private resetMessages() {
172
    this.successfulSaveMessage = '';
173
    this.updateErrorMessage = '';
174
  }
175

    
176
  handleUpdateError(message: string, error) {
177
    this.resetMessages();
178
    this.updateErrorMessage = message;
179
    console.log('Server responded: ' + error);
180

    
181
    this.showLoading = false;
182
  }
183

    
184
  handleSuccessfulSave(message) {
185
    this.resetMessages();
186
    this.showLoading = false;
187
    HelperFunctions.scroll();
188
    this.successfulSaveMessage = message;
189
  }
190

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

    
214

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

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

    
276
  removePhoto() {
277
    this.deletePhoto = true;
278
    this.hasChanged = true;
279
    this.photo = 'assets/common-assets/curator-default.png';
280
  }
281
}
(3-3/4)