2022-07-05 00:31:56 +02:00
|
|
|
import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
|
2021-05-19 13:40:29 +02:00
|
|
|
import {FormBuilder, FormControl} from '@angular/forms';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
2022-07-05 00:31:56 +02:00
|
|
|
import {properties} from "../../../environments/environment";
|
2021-05-19 13:40:29 +02:00
|
|
|
import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
|
|
|
|
import {SearchUtilsClass} from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
|
2018-11-21 10:42:37 +01:00
|
|
|
import {ZenodoCommunitiesService} from '../../openaireLibrary/connect/zenodoCommunities/zenodo-communities.service';
|
2021-05-19 13:40:29 +02:00
|
|
|
import {ManageZenodoCommunitiesService} from '../../services/manageZenodoCommunities.service';
|
|
|
|
import {Subject, Subscription} from 'rxjs';
|
|
|
|
import {debounceTime, distinctUntilChanged} from 'rxjs/operators';
|
2022-07-05 00:31:56 +02:00
|
|
|
import {CommunityInfo} from "../../openaireLibrary/connect/community/communityInfo";
|
|
|
|
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
declare var UIkit;
|
|
|
|
|
2018-11-21 10:42:37 +01:00
|
|
|
@Component({
|
2021-05-19 13:40:29 +02:00
|
|
|
selector: 'add-zenodo-communities',
|
|
|
|
templateUrl: './add-zenodo-communities.component.html',
|
2018-11-21 10:42:37 +01:00
|
|
|
})
|
|
|
|
export class AddZenodoCommunitiesComponent implements OnInit {
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
public zenodoCommunities = null; // zenodo search API results
|
|
|
|
public totalZenodoCommunities = null;
|
2022-07-05 00:31:56 +02:00
|
|
|
properties: EnvProperties = properties;
|
|
|
|
@Input() community: CommunityInfo = null;
|
2021-05-19 13:40:29 +02:00
|
|
|
public filterForm: FormControl;
|
|
|
|
private subscriptions: any[] = [];
|
|
|
|
public subResults: any;
|
|
|
|
public zenodoCommunitySearchUtils: SearchUtilsClass = new SearchUtilsClass();
|
|
|
|
public searchTermStream = new Subject<string>();
|
|
|
|
errorCodes: ErrorCodes;
|
|
|
|
public rowsOnPage = 10;
|
|
|
|
@Input() masterCommunity = null;
|
|
|
|
@Input() selectedCommunities = [];
|
2022-07-05 00:31:56 +02:00
|
|
|
@Output() zenodoCommunitiesChanged = new EventEmitter();
|
|
|
|
|
2021-05-19 13:40:29 +02:00
|
|
|
constructor(private route: ActivatedRoute,
|
|
|
|
private _router: Router,
|
|
|
|
public _fb: FormBuilder,
|
|
|
|
private _zenodoCommunitieService: ZenodoCommunitiesService,
|
|
|
|
private _manageZenodoCommunitiesService: ManageZenodoCommunitiesService) {
|
|
|
|
|
|
|
|
this.errorCodes = new ErrorCodes();
|
|
|
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.LOADING;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.filterForm = this._fb.control('');
|
|
|
|
this.subscriptions.push(this.filterForm.valueChanges.subscribe(value => {
|
|
|
|
this.searchTermStream.next(value);
|
|
|
|
}));
|
|
|
|
this.subscriptions.push(this.searchTermStream
|
|
|
|
.pipe(debounceTime(1000), distinctUntilChanged())
|
|
|
|
.subscribe((term: string) => {
|
|
|
|
this.zenodoCommunitySearchUtils.keyword = term;
|
|
|
|
this.goTo(1);
|
|
|
|
}));
|
|
|
|
|
|
|
|
this.zenodoCommunitySearchUtils.keyword = "";
|
2022-07-05 00:31:56 +02:00
|
|
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.LOADING;
|
|
|
|
if (this.community.communityId != null && this.community.communityId != '') {
|
|
|
|
this._zenodoCommunitieService.getZenodoCommunities(this.properties, this.properties.zenodoCommunities + "?page=" + this.zenodoCommunitySearchUtils.page + "&size=" + this.rowsOnPage).subscribe(
|
|
|
|
result => {
|
|
|
|
this.zenodoCommunities = result[0];
|
|
|
|
this.totalZenodoCommunities = result[1];
|
|
|
|
this.zenodoCommunitySearchUtils.totalResults = result[1];
|
|
|
|
this.zenodoCommunitySearchUtils.page = 1;
|
|
|
|
this.zenodoCommunitySearchUtils.size = this.rowsOnPage;
|
|
|
|
if (this.totalZenodoCommunities == 0) {
|
|
|
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.NONE;
|
|
|
|
} else {
|
|
|
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.DONE;
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
2022-07-05 00:31:56 +02:00
|
|
|
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.ERROR;
|
|
|
|
}
|
|
|
|
);
|
2018-11-21 10:42:37 +01:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.subscriptions.forEach(subscription => {
|
|
|
|
if (subscription instanceof Subscription) {
|
|
|
|
subscription.unsubscribe();
|
2018-11-21 10:42:37 +01:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
});
|
|
|
|
if(this.subResults){
|
|
|
|
this.subResults.unsubscribe();
|
2018-11-21 10:42:37 +01:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public goTo(page: number = 1) {
|
2022-07-05 00:31:56 +02:00
|
|
|
this.zenodoCommunitySearchUtils.page = page;
|
|
|
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.LOADING;
|
|
|
|
if(this.subResults){
|
|
|
|
this.subResults.unsubscribe();
|
|
|
|
}
|
|
|
|
this.subResults = this._zenodoCommunitieService.getZenodoCommunities(this.properties,
|
|
|
|
this.properties.zenodoCommunities + "?page=" + this.zenodoCommunitySearchUtils.page
|
|
|
|
+ "&size=" + this.rowsOnPage
|
|
|
|
+ ((this.zenodoCommunitySearchUtils.keyword) ? ("&q=" + this.zenodoCommunitySearchUtils.keyword) : "")
|
|
|
|
).subscribe(
|
|
|
|
result => {
|
|
|
|
this.zenodoCommunities = result[0];
|
|
|
|
this.totalZenodoCommunities = result[1];
|
|
|
|
this.zenodoCommunitySearchUtils.totalResults = result[1];
|
|
|
|
this.zenodoCommunitySearchUtils.size = this.rowsOnPage;
|
|
|
|
if (this.totalZenodoCommunities == 0) {
|
|
|
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.NONE;
|
|
|
|
} else {
|
|
|
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.DONE;
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
2022-07-05 00:31:56 +02:00
|
|
|
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.ERROR;
|
2018-11-21 10:42:37 +01:00
|
|
|
}
|
2022-07-05 00:31:56 +02:00
|
|
|
);
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
totalPages(): number {
|
|
|
|
let totalPages: any = this.zenodoCommunitySearchUtils.totalResults / (this.rowsOnPage);
|
|
|
|
if (!(Number.isInteger(totalPages))) {
|
|
|
|
totalPages = (parseInt(totalPages, 10) + 1);
|
|
|
|
}
|
|
|
|
return totalPages;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public addCommunity(community) {
|
2022-07-05 00:31:56 +02:00
|
|
|
this.subscriptions.push(this._manageZenodoCommunitiesService.addZCommunity(this.properties, this.community.communityId, community.id).subscribe(
|
|
|
|
data => {
|
|
|
|
community["openaireId"] = data.id;
|
|
|
|
this.selectedCommunities.push(community);
|
|
|
|
UIkit.notification('Community successfully added!', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
this.zenodoCommunitiesChanged.emit({
|
|
|
|
value: this.selectedCommunities,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
this.handleError('An error has been occurred. Try again later!');
|
|
|
|
console.log(err.status);
|
|
|
|
}
|
|
|
|
));
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public removeCommunity(comm) {
|
2022-07-05 00:31:56 +02:00
|
|
|
var openaireId = this.getOpenaireId(comm);
|
|
|
|
this.subscriptions.push(this._manageZenodoCommunitiesService.removeZCommunity(this.properties, this.community.communityId, openaireId,).subscribe(
|
|
|
|
data => {
|
|
|
|
var pos = -1;
|
|
|
|
for (var i = 0; i < this.selectedCommunities.length; i++) {
|
|
|
|
if (this.selectedCommunities[i].id == comm.id) {
|
|
|
|
pos = i;
|
|
|
|
break;
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
2022-07-05 00:31:56 +02:00
|
|
|
}
|
|
|
|
if (pos != -1) {
|
|
|
|
this.selectedCommunities.splice(pos, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
UIkit.notification('Community successfully removed!', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
this.zenodoCommunitiesChanged.emit({
|
|
|
|
value: this.selectedCommunities,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
this.handleError('An error has been occurred. Try again later!');
|
|
|
|
console.log(err.status);
|
|
|
|
}
|
|
|
|
));
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public inThelist(community: any, list): any {
|
|
|
|
for (let com of list) {
|
|
|
|
if (com.id == community.id) {
|
|
|
|
return true;
|
2018-11-21 10:42:37 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getOpenaireId(community: any): string {
|
|
|
|
for (let com of this.selectedCommunities) {
|
|
|
|
if (com.id == community.id) {
|
|
|
|
return com.openaireId;
|
2018-11-21 10:42:37 +01:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-07-05 00:31:56 +02:00
|
|
|
|
|
|
|
get loading() {
|
|
|
|
return this.zenodoCommunitySearchUtils.status == this.errorCodes.LOADING
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleError(message: string) {
|
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
}
|
2018-11-21 10:42:37 +01:00
|
|
|
}
|