2021-05-19 13:40:29 +02:00
|
|
|
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
|
2019-06-11 15:52:12 +02:00
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
|
|
|
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
|
|
|
import {Affiliation} from '../../openaireLibrary/utils/entities/CuratorInfo';
|
|
|
|
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
|
|
|
|
import {AffiliationService} from "../../openaireLibrary/connect/affiliations/affiliation.service";
|
2019-07-08 15:09:45 +02:00
|
|
|
import {HelpContentService} from "../../services/help-content.service";
|
2020-04-01 15:40:48 +02:00
|
|
|
import {Title} from '@angular/platform-browser';
|
2020-09-24 13:48:17 +02:00
|
|
|
import {StringUtils} from "../../openaireLibrary/utils/string-utils.class";
|
2021-05-19 13:40:29 +02:00
|
|
|
import {properties} from "../../../environments/environment";
|
|
|
|
import {Page} from "../../openaireLibrary/utils/entities/adminTool/page";
|
|
|
|
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
|
|
|
|
import {Subscription} from "rxjs";
|
|
|
|
import {CommunityService} from "../../openaireLibrary/connect/community/community.service";
|
2022-06-30 17:23:14 +02:00
|
|
|
import {CommunityInfo} from "../../openaireLibrary/connect/community/communityInfo";
|
|
|
|
import {NotificationHandler} from "../../openaireLibrary/utils/notification-handler";
|
2022-07-04 20:45:20 +02:00
|
|
|
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
|
2019-06-11 15:52:12 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'affiliations',
|
|
|
|
templateUrl: './affiliations.component.html',
|
|
|
|
})
|
2021-05-19 13:40:29 +02:00
|
|
|
export class AffiliationsComponent implements OnInit, OnDestroy {
|
|
|
|
public loading = true;
|
|
|
|
public properties: EnvProperties = properties;
|
|
|
|
public index = 0;
|
|
|
|
public affiliations: Affiliation[];
|
|
|
|
public affiliationFb: FormGroup;
|
2022-06-30 17:23:14 +02:00
|
|
|
public community: CommunityInfo;
|
2021-05-19 13:40:29 +02:00
|
|
|
public organizationsPage: Page;
|
|
|
|
public page: number = 1;
|
|
|
|
public pageSize: number = 10;
|
|
|
|
private subs: any[] = [];
|
2019-06-11 15:52:12 +02:00
|
|
|
@ViewChild('affiliationModal') affiliationModal: AlertModal;
|
|
|
|
@ViewChild('removeAffiliationModal') removeAffiliationModal: AlertModal;
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
constructor(private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
2020-04-01 15:40:48 +02:00
|
|
|
private title: Title,
|
2021-05-19 13:40:29 +02:00
|
|
|
private fb: FormBuilder,
|
|
|
|
private communityService: CommunityService,
|
2019-06-11 15:52:12 +02:00
|
|
|
private affiliationService: AffiliationService,
|
2021-05-19 13:40:29 +02:00
|
|
|
private helpContentService: HelpContentService) {
|
2019-06-11 15:52:12 +02:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
|
2019-06-11 15:52:12 +02:00
|
|
|
ngOnInit() {
|
2021-05-19 13:40:29 +02:00
|
|
|
this.loading = true;
|
2022-06-30 17:23:14 +02:00
|
|
|
this.subs.push(this.communityService.getCommunityAsObservable().subscribe( community => {
|
|
|
|
this.community = community;
|
|
|
|
if (this.community) {
|
|
|
|
this.title.setTitle(this.community.shortTitle.toUpperCase() + ' | Organizations');
|
|
|
|
this.getAffiliations();
|
|
|
|
this.organizationsPageStatus();
|
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.subs.forEach(sub => {
|
|
|
|
if (sub instanceof Subscription) {
|
|
|
|
sub.unsubscribe();
|
2019-06-11 15:52:12 +02:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
})
|
2019-06-11 15:52:12 +02:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
|
2019-06-11 15:52:12 +02:00
|
|
|
getAffiliations() {
|
2021-05-19 13:40:29 +02:00
|
|
|
this.loading = true;
|
2022-07-04 20:45:20 +02:00
|
|
|
this.affiliationService.getAffiliations(this.community.communityId).subscribe(
|
2019-06-11 15:52:12 +02:00
|
|
|
affiliations => {
|
|
|
|
this.affiliations = affiliations;
|
2021-05-19 13:40:29 +02:00
|
|
|
this.loading = false;
|
2019-06-11 15:52:12 +02:00
|
|
|
},
|
|
|
|
error => {
|
2022-06-30 17:23:14 +02:00
|
|
|
console.error("Affiliations Component: Error getting affiliations for community with id: " + this.community.communityId, error);
|
2021-05-19 13:40:29 +02:00
|
|
|
this.loading = false;
|
2019-06-11 15:52:12 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
public get organizationsEnabled(): boolean {
|
|
|
|
return !this.organizationsPage || this.organizationsPage.isEnabled;
|
2019-07-08 15:09:45 +02:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
editAffiliationOpen(index: number = -1) {
|
|
|
|
let affiliation: Affiliation;
|
|
|
|
this.index = index;
|
|
|
|
if (index === -1) {
|
|
|
|
affiliation = new Affiliation();
|
2022-06-30 17:23:14 +02:00
|
|
|
affiliation.communityId = this.community.communityId;
|
2021-05-19 13:40:29 +02:00
|
|
|
this.affiliationModal.alertTitle = 'Add Organization';
|
|
|
|
this.affiliationModal.okButtonText = 'Add';
|
2019-06-11 15:52:12 +02:00
|
|
|
} else {
|
2021-05-19 13:40:29 +02:00
|
|
|
affiliation = this.affiliations[this.index];
|
|
|
|
this.affiliationModal.alertTitle = 'Edit Organization';
|
|
|
|
this.affiliationModal.okButtonText = 'Update';
|
2019-06-11 15:52:12 +02:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
this.affiliationFb = this.fb.group({
|
|
|
|
communityId: this.fb.control(affiliation.communityId, Validators.required),
|
|
|
|
id: this.fb.control(affiliation.id),
|
|
|
|
name: this.fb.control(affiliation.name, Validators.required),
|
|
|
|
logo_url: this.fb.control(affiliation.logo_url, [Validators.required, StringUtils.urlValidator()]),
|
|
|
|
website_url: this.fb.control(affiliation.website_url, [Validators.required, StringUtils.urlValidator()])
|
|
|
|
});
|
2019-06-11 15:52:12 +02:00
|
|
|
this.affiliationModal.okButtonLeft = false;
|
2021-05-19 13:40:29 +02:00
|
|
|
this.affiliationModal.cancelButtonText = 'Cancel';
|
2019-06-11 15:52:12 +02:00
|
|
|
this.affiliationModal.open();
|
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
deleteAffiliationOpen(index: number) {
|
2019-06-11 15:52:12 +02:00
|
|
|
this.index = index;
|
2021-05-19 13:40:29 +02:00
|
|
|
let affiliation: Affiliation = this.affiliations[index];
|
|
|
|
this.removeAffiliationModal.alertTitle = 'Delete Organization';
|
|
|
|
this.removeAffiliationModal.message = 'Do you want to remove <b>' +
|
|
|
|
affiliation.name + '</b> from Organizations?';
|
|
|
|
this.removeAffiliationModal.okButtonText = 'Yes';
|
|
|
|
this.removeAffiliationModal.cancelButtonText = 'No';
|
|
|
|
this.removeAffiliationModal.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentPage(): Affiliation[] {
|
|
|
|
if (this.affiliations) {
|
|
|
|
return this.affiliations.slice((this.page - 1) * this.pageSize, this.page * this.pageSize);
|
2019-06-11 15:52:12 +02:00
|
|
|
} else {
|
2021-05-19 13:40:29 +02:00
|
|
|
return [];
|
2019-06-11 15:52:12 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
getIndex(index: number): number {
|
|
|
|
return (this.page - 1)*this.pageSize + index;
|
|
|
|
}
|
|
|
|
|
|
|
|
public updatePage(event) {
|
2022-07-04 20:45:20 +02:00
|
|
|
HelperFunctions.scroll();
|
2021-05-19 13:40:29 +02:00
|
|
|
this.page = event.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
editAffiliation() {
|
|
|
|
this.loading = true;
|
2022-06-30 17:23:14 +02:00
|
|
|
this.affiliationService.updateAffiliation(this.properties.communityAPI + this.community.communityId + '/organizations',
|
2021-05-19 13:40:29 +02:00
|
|
|
this.affiliationFb.value).subscribe((affiliation) => {
|
|
|
|
if (affiliation) {
|
|
|
|
if (this.index === -1) {
|
|
|
|
this.affiliations.push(affiliation);
|
|
|
|
this.page = Math.ceil(this.affiliations.length/this.pageSize);
|
2019-06-11 15:52:12 +02:00
|
|
|
} else {
|
2021-05-19 13:40:29 +02:00
|
|
|
this.affiliations[this.index] = affiliation;
|
2019-06-11 15:52:12 +02:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
if (affiliation.id) {
|
|
|
|
this.handleUpdateSuccess('Your organization has been updated successfully!');
|
|
|
|
} else {
|
|
|
|
this.handleUpdateSuccess('Your organization has been saved successfully!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.handleUpdateError('An error has been occurred. Try again later!');
|
|
|
|
});
|
2019-06-11 15:52:12 +02:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
|
2019-06-11 15:52:12 +02:00
|
|
|
removeAffiliation() {
|
2021-05-19 13:40:29 +02:00
|
|
|
this.loading = true;
|
2022-06-30 17:23:14 +02:00
|
|
|
this.affiliationService.deleteAffiliation(this.properties.communityAPI + this.community.communityId + '/organizations',
|
2021-05-19 13:40:29 +02:00
|
|
|
this.affiliations[this.index].id).subscribe((deleteOK) => {
|
|
|
|
this.affiliations.splice(this.index, 1);
|
|
|
|
if (this.currentPage.length === 0) {
|
|
|
|
this.page = 1;
|
2019-06-11 15:52:12 +02:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
this.handleUpdateSuccess('Organization has been deleted');
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.handleUpdateError('An error has been occurred. Try again later!');
|
|
|
|
});
|
2019-06-11 15:52:12 +02:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
|
2019-07-09 11:43:19 +02:00
|
|
|
private organizationsPageStatus() {
|
2022-06-30 17:23:14 +02:00
|
|
|
this.helpContentService.getCommunityPagesByRoute(this.community.communityId, '/organizations', this.properties.adminToolsAPIURL).subscribe((page) => {
|
2021-05-19 13:40:29 +02:00
|
|
|
this.organizationsPage = page;
|
2019-07-09 11:43:19 +02:00
|
|
|
})
|
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
handleUpdateError(message: string) {
|
2022-06-30 17:23:14 +02:00
|
|
|
NotificationHandler.rise(message, "danger");
|
2021-05-19 13:40:29 +02:00
|
|
|
this.loading = false;
|
2019-06-11 15:52:12 +02:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
|
2019-06-11 15:52:12 +02:00
|
|
|
handleUpdateSuccess(message) {
|
2022-06-30 17:23:14 +02:00
|
|
|
NotificationHandler.rise(message);
|
2021-05-19 13:40:29 +02:00
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
enableAffiliations() {
|
2022-06-30 17:23:14 +02:00
|
|
|
this.helpContentService.togglePages(this.community.communityId, [this.organizationsPage._id], true, this.properties.adminToolsAPIURL).subscribe(() => {
|
2021-05-19 13:40:29 +02:00
|
|
|
this.organizationsPage.isEnabled = true;
|
2022-06-30 17:23:14 +02:00
|
|
|
NotificationHandler.rise('Organizations Page has been <b>enabled successfully</b>');
|
2021-05-19 13:40:29 +02:00
|
|
|
}, error => {
|
|
|
|
this.handleUpdateError('An error has been occurred. Try again later!');
|
|
|
|
});
|
2020-09-24 13:48:17 +02:00
|
|
|
}
|
2019-06-11 15:52:12 +02:00
|
|
|
}
|