connect-admin/src/app/pages/curator/curator.component.ts

314 lines
11 KiB
TypeScript
Raw Normal View History

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 {AlertModal} from '../../openaireLibrary/utils/modal/alert';
import {UtilitiesService} from '../../openaireLibrary/services/utilities.service';
@Component({
selector: 'curator',
templateUrl: './curator.component.html',
})
export class CuratorComponent implements OnInit {
@ViewChild('affiliationModal') affiliationModal: AlertModal;
@ViewChild('removeAffiliationModal') removeAffiliationModal: AlertModal;
public showLoading = true;
public updateErrorMessage = '';
public successfulSaveMessage = '';
public hasChanged = false;
public curatorId = null;
public curator: Curator = null;
public affiliation: Affiliation = new Affiliation();
public photo: any = null;
public properties: EnvProperties = null;
private file: File = null;
private enabled = true;
private index = 0;
private maxCharacters = 70;
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.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 => {
}
);
}
});
}
initAffiliation(affiliation: Affiliation = null) {
this.affiliation = new Affiliation();
if (affiliation) {
this.affiliation.name = affiliation.name;
this.affiliation.logo_url = affiliation.logo_url;
this.affiliation.website_url = affiliation.website_url;
this.affiliationModal.okButtonText = 'Edit Affiliation';
} else {
this.index = -1;
this.affiliation.name = '';
this.affiliation.logo_url = '';
this.affiliation.website_url = '';
this.affiliationModal.okButtonText = 'Add new Affiliation';
}
this.affiliationModal.okButtonLeft = false;
this.affiliationModal.open();
}
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.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;
}
private resetChange() {
this.hasChanged = false;
}
private resetMessages() {
this.successfulSaveMessage = '';
this.updateErrorMessage = '';
}
public chooseAffiliation(index: number, action: string = 'delete') {
this.index = index;
const affiliation: Affiliation = this.curator.affiliations[index];
if (action === 'delete') {
this.removeAffiliationModal.message = 'Do you want to remove ' +
affiliation.name + ' from your Affiliations?';
this.removeAffiliationModal.okButtonText = 'Yes';
this.removeAffiliationModal.cancelButtonText = 'No';
this.removeAffiliationModal.open();
} else if (action === 'edit') {
this.initAffiliation(affiliation);
}
}
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.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 {
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();
});
}
}
}
}
isEmptyAffiliation(): boolean {
return ((!this.affiliation.name || this.affiliation.name === '') ||
(!this.affiliation.logo_url || this.affiliation.logo_url === '') ||
(!this.affiliation.website_url || this.affiliation.website_url === ''));
}
addAffiliation() {
if (!this.isEmptyAffiliation()) {
if(!HelperFunctions.isTiny(this.affiliation.logo_url)) {
this.utilitiesService.getTiny(this.properties.tinyUrl + this.affiliation.logo_url).subscribe((res)=> {
this.affiliation.logo_url = res;
if (this.index === -1) {
this.curator.affiliations.push(this.affiliation);
} else {
this.curator.affiliations[this.index] = this.affiliation;
}
this.change();
})
} else {
if (this.index === -1) {
this.curator.affiliations.push(this.affiliation);
} else {
this.curator.affiliations[this.index] = this.affiliation;
}
this.change();
}
}
}
removeAffiliation() {
this.curator.affiliations.splice(this.index, 1);
this.change();
}
onNameChange() {
this.hasChanged = true;
if (!this.curator.name || this.curator.name === '') {
this.enabled = false;
} else {
this.enabled = true;
}
}
_format(name: string){
if(name) {
return (((name).length > this.maxCharacters) ? (name.substring(0, (this.maxCharacters - ('...').length)) + '...') : name);
} else {
return null;
}
}
}