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
|
|
13
|
@Component({
|
14
|
selector: 'curator',
|
15
|
templateUrl: './curator.component.html',
|
16
|
})
|
17
|
|
18
|
export class CuratorComponent implements OnInit {
|
19
|
|
20
|
public showLoading = true;
|
21
|
public updateErrorMessage = '';
|
22
|
public successfulSaveMessage = '';
|
23
|
|
24
|
public affiliationsChanged = false;
|
25
|
public hasChanged = false;
|
26
|
public curatorId = null;
|
27
|
public curator: Curator = null;
|
28
|
public photo: any = null;
|
29
|
public properties: EnvProperties = null;
|
30
|
|
31
|
private file: File = null;
|
32
|
private maxsize: number = 200*1024;
|
33
|
private enabled = true;
|
34
|
private deletePhoto =false;
|
35
|
|
36
|
constructor(private element: ElementRef,
|
37
|
private route: ActivatedRoute,
|
38
|
private _router: Router,
|
39
|
private curatorService: CuratorService,
|
40
|
private utilitiesService: UtilitiesService) {
|
41
|
}
|
42
|
|
43
|
|
44
|
ngOnInit() {
|
45
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
46
|
this.properties = data.envSpecific;
|
47
|
if (!Session.isLoggedIn()) {
|
48
|
this._router.navigate(['/user-info'], {
|
49
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
50
|
});
|
51
|
} else {
|
52
|
this.showLoading = true;
|
53
|
this.updateErrorMessage = '';
|
54
|
this.curatorId = Session.getUser().id;
|
55
|
this.curatorService.getCurator(this.properties,
|
56
|
this.properties.adminToolsAPIURL + 'curator/' + this.curatorId).subscribe(
|
57
|
curator => {
|
58
|
if (curator && Object.keys(curator).length > 0) {
|
59
|
this.curator = curator;
|
60
|
this.curator.email = Session.getUserEmail();
|
61
|
if (this.curator.photo && this.curator.photo !== '') {
|
62
|
this.photo = this.properties.utilsService + '/download/' + this.curator.photo;
|
63
|
} else {
|
64
|
this.photo = '../../../assets/common-assets/curator-default.png';
|
65
|
}
|
66
|
this.showLoading = false;
|
67
|
HelperFunctions.scroll();
|
68
|
} else {
|
69
|
this.curator = new Curator();
|
70
|
this.curator._id = this.curatorId;
|
71
|
this.curator.email = Session.getUserEmail();
|
72
|
this.curator.name = Session.getUserFullName();
|
73
|
this.curator.affiliations = [];
|
74
|
this.curator.bio = '';
|
75
|
this.curator.photo = null;
|
76
|
this.photo = '../../../assets/common-assets/curator-default.png';
|
77
|
this.showLoading = false;
|
78
|
HelperFunctions.scroll();
|
79
|
}
|
80
|
},
|
81
|
error => {
|
82
|
}
|
83
|
);
|
84
|
}
|
85
|
});
|
86
|
}
|
87
|
|
88
|
public resetForm() {
|
89
|
if (!Session.isLoggedIn()) {
|
90
|
this._router.navigate(['/user-info'], {
|
91
|
queryParams:
|
92
|
{'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
93
|
});
|
94
|
} else {
|
95
|
if (this.curatorId != null && this.curatorId !== '') {
|
96
|
this.showLoading = true;
|
97
|
this.updateErrorMessage = '';
|
98
|
this.curatorService.getCurator(this.properties,
|
99
|
this.properties.adminToolsAPIURL + 'curator/' + this.curatorId).subscribe(
|
100
|
curator => {
|
101
|
if (curator) {
|
102
|
this.curator = curator;
|
103
|
this.curator.email = Session.getUserEmail();
|
104
|
if (this.curator.photo && this.curator.photo !== '') {
|
105
|
this.photo = this.properties.utilsService + '/download/' + this.curator.photo;
|
106
|
} else {
|
107
|
this.photo = '../../../assets/common-assets/curator-default.png';
|
108
|
}
|
109
|
this.showLoading = false;
|
110
|
HelperFunctions.scroll();
|
111
|
} else {
|
112
|
this.curator = new Curator();
|
113
|
this.curator._id = this.curatorId;
|
114
|
this.curator.email = Session.getUserEmail();
|
115
|
this.curator.name = Session.getUserFullName();
|
116
|
this.curator.affiliations = [];
|
117
|
this.curator.bio = '';
|
118
|
this.curator.photo = null;
|
119
|
this.photo = '../../../assets/common-assets/curator-default.png';
|
120
|
this.showLoading = false;
|
121
|
HelperFunctions.scroll();
|
122
|
}
|
123
|
},
|
124
|
error => {
|
125
|
}
|
126
|
);
|
127
|
}
|
128
|
this.resetChange();
|
129
|
}
|
130
|
}
|
131
|
|
132
|
private change() {
|
133
|
this.hasChanged = true;
|
134
|
this.affiliationsChanged = true;
|
135
|
}
|
136
|
|
137
|
private resetChange() {
|
138
|
this.hasChanged = false;
|
139
|
this.affiliationsChanged = false;
|
140
|
}
|
141
|
|
142
|
private resetMessages() {
|
143
|
this.successfulSaveMessage = '';
|
144
|
this.updateErrorMessage = '';
|
145
|
}
|
146
|
|
147
|
handleUpdateError(message: string, error) {
|
148
|
this.resetMessages();
|
149
|
this.updateErrorMessage = message;
|
150
|
console.log('Server responded: ' + error);
|
151
|
|
152
|
this.showLoading = false;
|
153
|
}
|
154
|
|
155
|
handleSuccessfulSave(message) {
|
156
|
this.resetMessages();
|
157
|
this.showLoading = false;
|
158
|
HelperFunctions.scroll();
|
159
|
this.successfulSaveMessage = message;
|
160
|
}
|
161
|
|
162
|
fileChangeEvent(event) {
|
163
|
this.showLoading = true;
|
164
|
if (event.target.files && event.target.files[0]) {
|
165
|
this.file = event.target.files[0];
|
166
|
if (this.file.type !== 'image/png' && this.file.type !== 'image/jpeg') {
|
167
|
this.handleUpdateError('You must choose a file with type: image/png or image/jpeg!', null);
|
168
|
this.file = null;
|
169
|
} else if (this.file.size > this.maxsize) {
|
170
|
this.handleUpdateError("File exceeds size's limit! Maximum resolution is 256x256 pixels.", null);
|
171
|
this.file = null;
|
172
|
} else {
|
173
|
this.updateErrorMessage = '';
|
174
|
const reader = new FileReader();
|
175
|
reader.readAsDataURL(this.file);
|
176
|
reader.onload = () => {
|
177
|
this.photo = reader.result;
|
178
|
this.showLoading = false;
|
179
|
HelperFunctions.scroll();
|
180
|
};
|
181
|
}
|
182
|
}
|
183
|
}
|
184
|
|
185
|
|
186
|
updateCurator() {
|
187
|
if (!Session.isLoggedIn()) {
|
188
|
this._router.navigate(['/user-info'], {
|
189
|
queryParams:
|
190
|
{'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
191
|
});
|
192
|
} else {
|
193
|
if ((this.hasChanged || this.affiliationsChanged) && this.curator && this.curator.name && this.curator.name !== '') {
|
194
|
this.showLoading = true;
|
195
|
if (this.file) {
|
196
|
this.utilitiesService.uploadPhoto(this.properties.utilsService + '/upload/' + this.curator._id, this.file).subscribe((res) => {
|
197
|
if (this.curator.photo && this.curator.photo !== '') {
|
198
|
this.utilitiesService.deletePhoto(this.properties.utilsService + '/delete/' + this.curator.photo).subscribe();
|
199
|
}
|
200
|
this.curator.photo = res.filename;
|
201
|
this.curatorService.updateCurator(this.properties.adminToolsAPIURL + 'curator',
|
202
|
this.curator).subscribe((curator) => {
|
203
|
if (curator) {
|
204
|
this.handleSuccessfulSave('Your data has been saved successfully!');
|
205
|
this.resetChange();
|
206
|
}
|
207
|
},
|
208
|
error => {
|
209
|
this.handleUpdateError('An error has occurred. Try again later!', error);
|
210
|
this.resetChange();
|
211
|
});
|
212
|
}, error => {
|
213
|
this.handleUpdateError('An error has occurred during photo uploading.', error);
|
214
|
}
|
215
|
);
|
216
|
} else {
|
217
|
if(this.deletePhoto) {
|
218
|
this.utilitiesService.deletePhoto(this.properties.utilsService + '/delete/' + this.curator.photo).subscribe();
|
219
|
this.curator.photo = '';
|
220
|
}
|
221
|
this.curatorService.updateCurator(this.properties.adminToolsAPIURL + 'curator',
|
222
|
this.curator).subscribe((curator) => {
|
223
|
if (curator) {
|
224
|
this.handleSuccessfulSave('Your data has been saved successfully!');
|
225
|
this.resetChange();
|
226
|
}
|
227
|
},
|
228
|
error => {
|
229
|
this.handleUpdateError('An error has occurred. Try again later!', error);
|
230
|
this.resetChange();
|
231
|
});
|
232
|
}
|
233
|
}
|
234
|
}
|
235
|
}
|
236
|
|
237
|
onNameChange() {
|
238
|
this.hasChanged = true;
|
239
|
if (!this.curator.name || this.curator.name === '') {
|
240
|
this.enabled = false;
|
241
|
} else {
|
242
|
this.enabled = true;
|
243
|
}
|
244
|
}
|
245
|
|
246
|
removePhoto() {
|
247
|
this.deletePhoto = true;
|
248
|
this.hasChanged = true;
|
249
|
this.photo = '../../../assets/common-assets/curator-default.png';
|
250
|
}
|
251
|
}
|