2019-06-02 20:55:56 +02:00
|
|
|
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
|
|
|
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
|
|
|
|
2019-09-13 11:39:18 +02:00
|
|
|
import {Session, User} from '../../openaireLibrary/login/utils/helper.class';
|
2019-06-02 20:55:56 +02:00
|
|
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
|
|
|
import {CuratorService} from '../../openaireLibrary/connect/curators/curator.service';
|
2019-06-13 10:23:34 +02:00
|
|
|
import {Curator} from '../../openaireLibrary/utils/entities/CuratorInfo';
|
2019-06-02 20:55:56 +02:00
|
|
|
import {HelperFunctions} from '../../openaireLibrary/utils/HelperFunctions.class';
|
2019-06-06 12:53:47 +02:00
|
|
|
import {UtilitiesService} from '../../openaireLibrary/services/utilities.service';
|
2019-07-09 11:43:19 +02:00
|
|
|
import {HelpContentService} from '../../services/help-content.service';
|
2019-08-27 11:23:40 +02:00
|
|
|
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
|
2019-09-13 11:39:18 +02:00
|
|
|
import {UserManagementService} from '../../openaireLibrary/services/user-management.service';
|
2020-04-01 15:40:48 +02:00
|
|
|
import {Title} from '@angular/platform-browser';
|
2019-06-02 20:55:56 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'curator',
|
|
|
|
templateUrl: './curator.component.html',
|
|
|
|
})
|
|
|
|
|
|
|
|
export class CuratorComponent implements OnInit {
|
|
|
|
|
|
|
|
public showLoading = true;
|
|
|
|
public updateErrorMessage = '';
|
|
|
|
public successfulSaveMessage = '';
|
|
|
|
|
2020-09-28 14:12:27 +02:00
|
|
|
public curatorsEnabled = false;
|
|
|
|
public newCurator = false;
|
2019-07-09 11:43:19 +02:00
|
|
|
|
|
|
|
public communityId = null;
|
|
|
|
|
2019-06-11 15:52:12 +02:00
|
|
|
public affiliationsChanged = false;
|
2019-06-02 20:55:56 +02:00
|
|
|
public hasChanged = false;
|
|
|
|
public curatorId = null;
|
|
|
|
public curator: Curator = null;
|
|
|
|
public photo: any = null;
|
|
|
|
public properties: EnvProperties = null;
|
2019-09-13 11:39:18 +02:00
|
|
|
public user: User;
|
2019-06-02 20:55:56 +02:00
|
|
|
|
|
|
|
private file: File = null;
|
2019-09-13 11:39:18 +02:00
|
|
|
private maxsize: number = 200 * 1024;
|
2019-07-10 14:55:16 +02:00
|
|
|
public enabled = true;
|
2019-07-09 11:43:19 +02:00
|
|
|
private deletePhoto = false;
|
2019-06-02 20:55:56 +02:00
|
|
|
|
2019-08-27 11:23:40 +02:00
|
|
|
@ViewChild('privacyStatement') privacyStatement: AlertModal;
|
|
|
|
|
2019-06-02 20:55:56 +02:00
|
|
|
constructor(private element: ElementRef,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private _router: Router,
|
2020-04-01 15:40:48 +02:00
|
|
|
private title: Title,
|
2019-06-02 20:55:56 +02:00
|
|
|
private curatorService: CuratorService,
|
2019-07-09 11:43:19 +02:00
|
|
|
private utilitiesService: UtilitiesService,
|
2019-09-13 11:39:18 +02:00
|
|
|
private helpContentService: HelpContentService,
|
|
|
|
private userManagementService: UserManagementService) {
|
2019-06-02 20:55:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
|
|
|
});
|
|
|
|
} else {
|
2019-07-09 11:43:19 +02:00
|
|
|
this.route.queryParams.subscribe((params) => {
|
|
|
|
this.communityId = params['communityId'];
|
2020-04-01 15:40:48 +02:00
|
|
|
this.title.setTitle('Administration Dashboard | Personal Info');
|
2019-07-09 11:43:19 +02:00
|
|
|
this.showLoading = true;
|
|
|
|
this.updateErrorMessage = '';
|
2019-12-23 16:57:56 +01:00
|
|
|
this.userManagementService.getUserInfo().subscribe(user => {
|
2019-09-13 11:39:18 +02:00
|
|
|
this.user = user;
|
2020-09-28 14:12:27 +02:00
|
|
|
if(this.user) {
|
|
|
|
this.curatorId = this.user.id;
|
|
|
|
this.getCurator();
|
|
|
|
}
|
2019-09-13 11:39:18 +02:00
|
|
|
});
|
|
|
|
});
|
2019-06-02 20:55:56 +02:00
|
|
|
}
|
|
|
|
});
|
2019-07-09 11:43:19 +02:00
|
|
|
|
2019-06-02 20:55:56 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 14:12:27 +02:00
|
|
|
public getCurator() {
|
|
|
|
this.curatorService.getCurator(this.properties, this.curatorId).subscribe(
|
|
|
|
curator => {
|
|
|
|
if (curator && Object.keys(curator).length > 0) {
|
|
|
|
this.curator = curator;
|
|
|
|
this.curator.email = this.user.email;
|
|
|
|
if (this.curator.photo && this.curator.photo !== '') {
|
|
|
|
this.photo = this.properties.utilsService + '/download/' + this.curator.photo;
|
|
|
|
} else {
|
|
|
|
this.photo = 'assets/common-assets/curator-default.png';
|
|
|
|
}
|
|
|
|
this.curatorsPageStatus();
|
|
|
|
this.showLoading = false;
|
|
|
|
HelperFunctions.scroll();
|
|
|
|
} else {
|
|
|
|
this.newCurator = true;
|
|
|
|
this.curator = new Curator();
|
|
|
|
this.curator._id = this.curatorId;
|
|
|
|
this.curator.email = this.user.email;
|
|
|
|
this.curator.name = this.user.fullname;
|
|
|
|
this.curator.affiliations = [];
|
|
|
|
this.curator.bio = '';
|
|
|
|
this.curator.photo = null;
|
|
|
|
this.photo = 'assets/common-assets/curator-default.png';
|
|
|
|
this.showLoading = false;
|
|
|
|
HelperFunctions.scroll();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-02 20:55:56 +02:00
|
|
|
public resetForm() {
|
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams:
|
|
|
|
{'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (this.curatorId != null && this.curatorId !== '') {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.updateErrorMessage = '';
|
2020-09-28 14:12:27 +02:00
|
|
|
this.getCurator();
|
2019-06-02 20:55:56 +02:00
|
|
|
}
|
|
|
|
this.resetChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 11:43:19 +02:00
|
|
|
private curatorsPageStatus() {
|
2020-09-28 14:12:27 +02:00
|
|
|
this.curatorsEnabled = false;
|
2019-07-09 11:43:19 +02:00
|
|
|
this.helpContentService.getCommunityFull(this.communityId, this.properties.adminToolsAPIURL).subscribe((community) => {
|
2019-09-13 11:39:18 +02:00
|
|
|
for (let page of community.pages) {
|
|
|
|
if (page['route'] === '/curators') {
|
2019-07-09 11:43:19 +02:00
|
|
|
this.curatorsEnabled = page['isEnabled'];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-09-13 11:39:18 +02:00
|
|
|
});
|
2019-07-09 11:43:19 +02:00
|
|
|
}
|
|
|
|
|
2019-06-02 20:55:56 +02:00
|
|
|
private change() {
|
|
|
|
this.hasChanged = true;
|
2019-06-11 15:52:12 +02:00
|
|
|
this.affiliationsChanged = true;
|
2019-06-02 20:55:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private resetChange() {
|
|
|
|
this.hasChanged = false;
|
2019-06-11 15:52:12 +02:00
|
|
|
this.affiliationsChanged = false;
|
2019-06-02 20:55:56 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 14:55:16 +02:00
|
|
|
public resetMessages() {
|
2019-06-02 20:55:56 +02:00
|
|
|
this.successfulSaveMessage = '';
|
|
|
|
this.updateErrorMessage = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUpdateError(message: string, error) {
|
|
|
|
this.resetMessages();
|
|
|
|
this.updateErrorMessage = message;
|
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSuccessfulSave(message) {
|
|
|
|
this.resetMessages();
|
|
|
|
this.showLoading = false;
|
|
|
|
HelperFunctions.scroll();
|
|
|
|
this.successfulSaveMessage = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileChangeEvent(event) {
|
|
|
|
this.showLoading = true;
|
|
|
|
if (event.target.files && event.target.files[0]) {
|
|
|
|
this.file = event.target.files[0];
|
|
|
|
if (this.file.type !== 'image/png' && this.file.type !== 'image/jpeg') {
|
2019-06-27 14:34:31 +02:00
|
|
|
this.handleUpdateError('You must choose a file with type: image/png or image/jpeg!', null);
|
2019-06-02 20:55:56 +02:00
|
|
|
this.file = null;
|
2019-06-28 11:23:01 +02:00
|
|
|
} else if (this.file.size > this.maxsize) {
|
2019-09-13 11:39:18 +02:00
|
|
|
this.handleUpdateError('File exceeds size\'s limit! Maximum resolution is 256x256 pixels.', null);
|
2019-06-28 11:23:01 +02:00
|
|
|
this.file = null;
|
2019-06-02 20:55:56 +02:00
|
|
|
} else {
|
|
|
|
this.updateErrorMessage = '';
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.readAsDataURL(this.file);
|
|
|
|
reader.onload = () => {
|
|
|
|
this.photo = reader.result;
|
|
|
|
this.showLoading = false;
|
|
|
|
HelperFunctions.scroll();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 14:00:20 +02:00
|
|
|
saveCurator() {
|
|
|
|
this.curatorService.updateCurator(this.properties, this.curator).subscribe((curator) => {
|
|
|
|
if (curator) {
|
|
|
|
this.handleSuccessfulSave('Your data has been saved successfully!');
|
|
|
|
this.newCurator = false;
|
|
|
|
this.file = null;
|
|
|
|
this.deletePhoto = false;
|
|
|
|
this.resetChange();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.handleUpdateError('An error has occurred. Try again later!', error);
|
|
|
|
this.resetChange();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-02 20:55:56 +02:00
|
|
|
|
|
|
|
updateCurator() {
|
2019-06-04 16:20:16 +02:00
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams:
|
|
|
|
{'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
|
|
|
});
|
|
|
|
} else {
|
2019-06-11 15:52:12 +02:00
|
|
|
if ((this.hasChanged || this.affiliationsChanged) && this.curator && this.curator.name && this.curator.name !== '') {
|
2019-06-04 16:20:16 +02:00
|
|
|
this.showLoading = true;
|
|
|
|
if (this.file) {
|
2019-06-28 11:49:21 +02:00
|
|
|
this.utilitiesService.uploadPhoto(this.properties.utilsService + '/upload/' + this.curator._id, this.file).subscribe((res) => {
|
2019-06-04 16:20:16 +02:00
|
|
|
if (this.curator.photo && this.curator.photo !== '') {
|
2019-06-28 11:49:21 +02:00
|
|
|
this.utilitiesService.deletePhoto(this.properties.utilsService + '/delete/' + this.curator.photo).subscribe();
|
2019-06-04 16:20:16 +02:00
|
|
|
}
|
|
|
|
this.curator.photo = res.filename;
|
2020-09-28 14:00:20 +02:00
|
|
|
this.saveCurator();
|
2019-06-04 16:20:16 +02:00
|
|
|
}, error => {
|
|
|
|
this.handleUpdateError('An error has occurred during photo uploading.', error);
|
2019-06-02 20:55:56 +02:00
|
|
|
}
|
2019-06-04 16:20:16 +02:00
|
|
|
);
|
|
|
|
} else {
|
2019-09-13 11:39:18 +02:00
|
|
|
if (this.deletePhoto) {
|
2020-09-28 14:00:20 +02:00
|
|
|
if(this.curator.photo && this.curator.photo != '') {
|
|
|
|
this.utilitiesService.deletePhoto(this.properties.utilsService + '/delete/' + this.curator.photo).subscribe();
|
|
|
|
this.curator.photo = '';
|
|
|
|
}
|
2019-06-07 12:27:16 +02:00
|
|
|
}
|
2020-09-28 14:00:20 +02:00
|
|
|
this.saveCurator();
|
2019-06-04 16:20:16 +02:00
|
|
|
}
|
2019-06-02 20:55:56 +02:00
|
|
|
}
|
2019-09-13 11:39:18 +02:00
|
|
|
}
|
2019-06-02 20:55:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onNameChange() {
|
|
|
|
this.hasChanged = true;
|
2020-09-28 14:00:20 +02:00
|
|
|
this.enabled = !(!this.curator.name || this.curator.name === '');
|
2019-06-02 20:55:56 +02:00
|
|
|
}
|
2019-06-03 10:25:11 +02:00
|
|
|
|
2019-06-07 12:27:16 +02:00
|
|
|
removePhoto() {
|
|
|
|
this.deletePhoto = true;
|
|
|
|
this.hasChanged = true;
|
2020-09-28 14:00:20 +02:00
|
|
|
this.file = null;
|
2019-07-03 12:50:15 +02:00
|
|
|
this.photo = 'assets/common-assets/curator-default.png';
|
2019-06-07 12:27:16 +02:00
|
|
|
}
|
2019-08-27 11:23:40 +02:00
|
|
|
|
|
|
|
privacy() {
|
|
|
|
this.privacyStatement.cancelButton = false;
|
|
|
|
this.privacyStatement.okButtonText = 'Close';
|
2019-09-13 11:39:18 +02:00
|
|
|
this.privacyStatement.alertTitle = 'Privacy policy statement';
|
2019-08-27 11:23:40 +02:00
|
|
|
this.privacyStatement.open();
|
|
|
|
}
|
2019-06-02 20:55:56 +02:00
|
|
|
}
|