connect-admin/src/app/pages/zenodo-communities/zenodo-communities.componen...

158 lines
6.9 KiB
TypeScript

import {Component, ElementRef, Input, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
import {ZenodoCommunitiesService} from '../../openaireLibrary/connect/zenodoCommunities/zenodo-communities.service';
import {CommunityService} from '../../openaireLibrary/connect/community/community.service';
import {CommunityInfo} from '../../openaireLibrary/connect/community/communityInfo';
import {ZenodoCommunityInfo} from '../../openaireLibrary/connect/zenodoCommunities/zenodoCommunityInfo';
import {SearchUtilsClass} from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
import {Title} from '@angular/platform-browser';
import {properties} from '../../../environments/environment';
import {Subscription} from 'rxjs';
import {FullScreenModalComponent} from '../../openaireLibrary/utils/modal/full-screen-modal/full-screen-modal.component';
import {ManageZenodoCommunitiesComponent} from './manage-zenodo-communities.component';
@Component({
selector: 'zenodo-communities',
template: `
<manage-zenodo-communities #manage *ngIf="community && zenodoSearchUtils"
[loading]="showLoadingInRemove"
[masterCommunity]=masterZenodoCommunity [selectedCommunities]=selectedCommunities
[community]="community" [searchUtils]=zenodoSearchUtils (addZenodoCommunity)="openAddZenodoCommunites()"
(zenodoCommunitiesChanged)="zenodoCommunitiesChanged($event)"></manage-zenodo-communities>
<fs-modal #fsModal>
<add-zenodo-communities [masterCommunity]=masterZenodoCommunity
[selectedCommunities]=selectedCommunities
[community]="community" (zenodoCommunitiesChanged)="zenodoCommunitiesChanged($event)"></add-zenodo-communities>
</fs-modal>
`
})
export class ZenodoCommunitiesComponent implements OnInit, OnDestroy {
public community: CommunityInfo = null;
public toggle = false;
public pageTitle = 'Manage zenodo communities';
masterZenodoCommunityId = null;
masterZenodoCommunity = null;
public properties: EnvProperties = null;
selectedCommunityIds = null;
selectedCommunities = [];
zenodocommunitiesloadedCount = 0;
zenodoSearchUtils: SearchUtilsClass = new SearchUtilsClass();
public errorCodes: ErrorCodes = new ErrorCodes();
subscriptions = [];
@ViewChild('fsModal', { static: true }) fullscreen: FullScreenModalComponent;
@ViewChild(ManageZenodoCommunitiesComponent) manage: ManageZenodoCommunitiesComponent;
public showLoadingInRemove: boolean = true;
constructor(private element: ElementRef, private route: ActivatedRoute, private _router: Router,
private _zenodoCommunitieService: ZenodoCommunitiesService, private title: Title,
private _communityService: CommunityService) {
}
ngOnInit() {
this.zenodoSearchUtils.status = this.errorCodes.LOADING;
this.properties = properties;
this.subscriptions.push(this._communityService.getCommunityAsObservable().subscribe(
community => {
this.community = community;
this.title.setTitle(this.community.shortTitle.toUpperCase() + ' | Zenodo Communities');
this.masterZenodoCommunityId = this.community.zenodoCommunity;
if (this.masterZenodoCommunityId) {
this.subscriptions.push(this._zenodoCommunitieService.getZenodoCommunityById(this.properties,
this.masterZenodoCommunityId).subscribe(
result => {
this.masterZenodoCommunity = result;
this.selectedCommunities.unshift(this.masterZenodoCommunity);
},
error => {
const emptyCommunity: ZenodoCommunityInfo = new ZenodoCommunityInfo();
emptyCommunity.id = this.masterZenodoCommunityId;
emptyCommunity.title = this.masterZenodoCommunityId;
this.masterZenodoCommunity = emptyCommunity;
}
));
}
this.zenodoSearchUtils.status = this.errorCodes.LOADING;
this.zenodoSearchUtils.status = this.errorCodes.LOADING;
// this._searchZenodoCommunitiesService.searchZCommunities(this.properties, this.community.communityId).subscribe(
// result => {
this.selectedCommunityIds = this.community.otherZenodoCommunities;
this.zenodoSearchUtils.totalResults = this.selectedCommunityIds.length;
if (this.selectedCommunityIds.length === 0) {
this.zenodoSearchUtils.status = this.errorCodes.NONE;
}
for (let i = 0; i < this.selectedCommunityIds.length; i++) {
this.getZenodoCommunityById(
this.selectedCommunityIds[i]);
}
// },
// error => {
// console.log('list of zenodo communities couldn\'t be loaded');
// this.zenodoSearchUtils.status = this.errorCodes.DONE;
// }
// );
})
);
this.fullscreen.title = "Search and Add Zenodo Communities";
this.fullscreen.okButtonText = "Done";
this.fullscreen.okButton = true;
}
ngOnDestroy() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscription) {
subscription.unsubscribe();
}
});
}
getZenodoCommunityById(zenodoid) {
this.subscriptions.push(this._zenodoCommunitieService.getZenodoCommunityById(this.properties,
zenodoid).subscribe(
result => {
this.selectedCommunities.push(result);
this.zenodocommunitiesloadedCount++;
if (this.zenodocommunitiesloadedCount >= this.selectedCommunityIds.length) {
this.zenodoSearchUtils.status = this.errorCodes.DONE;
}
},
error => {
const emptyCommunity: ZenodoCommunityInfo = new ZenodoCommunityInfo();
emptyCommunity.id = zenodoid;
emptyCommunity.title = zenodoid;
this.selectedCommunities.push(emptyCommunity);
this.zenodocommunitiesloadedCount++;
if (this.zenodocommunitiesloadedCount >= this.selectedCommunityIds.length) {
this.zenodoSearchUtils.status = this.errorCodes.DONE;
}
console.log('Zenodo community\'' + zenodoid + '\' couldn\'t be loaded');
}
));
}
public openAddZenodoCommunites() {
this.fullscreen.title = "Search and Add Zenodo Communities";
this.fullscreen.okButtonText = "Done";
this.fullscreen.back = true;
this.fullscreen.okButton = true;
this.fullscreen.open();
}
public zenodoCommunitiesChanged($event) {
this.selectedCommunities = $event.value;
this.showLoadingInRemove = false;
if (this.fullscreen.isOpen) {
this.manage.filterPreviewCommunities(this.manage.filterForm.value);
}
}
}