import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties'; import {Session} from '../../openaireLibrary/login/utils/helper.class'; import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class'; import {CuratorService} from '../../openaireLibrary/connect/curators/curator.service'; import {Affiliation, Curator} from '../../openaireLibrary/utils/entities/CuratorInfo'; import {HelperFunctions} from '../../openaireLibrary/utils/HelperFunctions.class'; import {UtilitiesService} from '../../openaireLibrary/services/utilities.service'; @Component({ selector: 'curator', templateUrl: './curator.component.html', }) export class CuratorComponent implements OnInit { public showLoading = true; public updateErrorMessage = ''; public successfulSaveMessage = ''; public affiliationsChanged = false; public hasChanged = false; public curatorId = null; public curator: Curator = null; public photo: any = null; public properties: EnvProperties = null; private file: File = null; private enabled = true; private deletePhoto =false; constructor(private element: ElementRef, private route: ActivatedRoute, private _router: Router, private curatorService: CuratorService, private utilitiesService: UtilitiesService) { } 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 { this.showLoading = true; this.updateErrorMessage = ''; this.curatorId = Session.getUser().id; this.curatorService.getCurator(this.properties, this.properties.adminToolsAPIURL + 'curator/' + this.curatorId).subscribe( curator => { if (curator) { this.curator = curator; this.curator.email = Session.getUserEmail(); if (this.curator.photo && this.curator.photo !== '') { this.photo = this.properties.downloadUrl + '/' + this.curator.photo; } else { this.photo = '../../../assets/common-assets/curator-default.png'; } this.showLoading = false; HelperFunctions.scroll(); } else { this.curator = new Curator(); this.curator._id = this.curatorId; this.curator.email = Session.getUserEmail(); this.curator.name = Session.getUserFullName(); this.curator.affiliations = []; this.curator.bio = ''; this.curator.photo = null; this.photo = '../../../assets/common-assets/curator-default.png'; this.showLoading = false; HelperFunctions.scroll(); } }, error => { } ); } }); } 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 = ''; this.curatorService.getCurator(this.properties, this.properties.adminToolsAPIURL + 'curator/' + this.curatorId).subscribe( curator => { if (curator) { this.curator = curator; this.curator.email = Session.getUserEmail(); if (this.curator.photo && this.curator.photo !== '') { this.photo = this.properties.downloadUrl + '/' + this.curator.photo; } else { this.photo = '../../../assets/common-assets/curator-default.png'; } this.showLoading = false; HelperFunctions.scroll(); } else { this.curator = new Curator(); this.curator._id = this.curatorId; this.curator.email = Session.getUserEmail(); this.curator.name = Session.getUserFullName(); this.curator.affiliations = []; this.curator.bio = ''; this.curator.photo = null; this.photo = '../../../assets/common-assets/curator-default.png'; this.showLoading = false; HelperFunctions.scroll(); } }, error => { } ); } this.resetChange(); } } private change() { this.hasChanged = true; this.affiliationsChanged = true; } private resetChange() { this.hasChanged = false; this.affiliationsChanged = false; } private resetMessages() { 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') { this.handleUpdateError('You must choose a photo!', null); this.file = null; } else { this.updateErrorMessage = ''; const reader = new FileReader(); reader.readAsDataURL(this.file); reader.onload = () => { this.photo = reader.result; this.showLoading = false; HelperFunctions.scroll(); }; } } } updateCurator() { if (!Session.isLoggedIn()) { this._router.navigate(['/user-info'], { queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} }); } else { if ((this.hasChanged || this.affiliationsChanged) && this.curator && this.curator.name && this.curator.name !== '') { this.showLoading = true; if (this.file) { this.utilitiesService.uploadPhoto(this.properties.uploadService + '/' + this.curator._id, this.file).subscribe((res) => { if (this.curator.photo && this.curator.photo !== '') { this.utilitiesService.deletePhoto(this.properties.deleteUrl + '/' + this.curator.photo).subscribe(); } this.curator.photo = res.filename; this.curatorService.updateCurator(this.properties.adminToolsAPIURL + 'curator', this.curator).subscribe((curator) => { if (curator) { this.handleSuccessfulSave('Your data has been saved successfully!'); this.resetChange(); } }, error => { this.handleUpdateError('An error has occurred. Try again later!', error); this.resetChange(); }); }, error => { this.handleUpdateError('An error has occurred during photo uploading.', error); } ); } else { if(this.deletePhoto) { this.utilitiesService.deletePhoto(this.properties.deleteUrl + '/' + this.curator.photo).subscribe(); this.curator.photo = ''; } this.curatorService.updateCurator(this.properties.adminToolsAPIURL + 'curator', this.curator).subscribe((curator) => { if (curator) { this.handleSuccessfulSave('Your data has been saved successfully!'); this.resetChange(); } }, error => { this.handleUpdateError('An error has occurred. Try again later!', error); this.resetChange(); }); } } } } onNameChange() { this.hasChanged = true; if (!this.curator.name || this.curator.name === '') { this.enabled = false; } else { this.enabled = true; } } removePhoto() { this.deletePhoto = true; this.hasChanged = true; this.photo = '../../../assets/common-assets/curator-default.png'; } }