2021-05-19 13:40:29 +02:00
|
|
|
import {Component, Input, OnInit, ViewChild} from '@angular/core';
|
|
|
|
import {FormBuilder, FormControl} from '@angular/forms';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
|
|
|
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';
|
|
|
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
|
|
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
2021-05-19 13:40:29 +02:00
|
|
|
import {ManageZenodoCommunitiesService} from '../../services/manageZenodoCommunities.service';
|
|
|
|
import {SearchInputComponent} from '../../openaireLibrary/sharedComponents/search-input/search-input.component';
|
|
|
|
import {Subject, Subscription} from 'rxjs';
|
|
|
|
import {debounceTime, distinctUntilChanged} from 'rxjs/operators';
|
|
|
|
|
|
|
|
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;
|
|
|
|
@Input() properties: EnvProperties = null;
|
|
|
|
@Input() communityId = null;
|
|
|
|
@ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent;
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
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 = "";
|
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
|
|
|
"redirectUrl": this._router.url
|
2018-11-21 10:42:37 +01:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.LOADING;
|
|
|
|
if (this.communityId != null && this.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
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) {
|
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
|
|
|
"redirectUrl": this._router.url
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.zenodoCommunitySearchUtils.page = page;
|
|
|
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.LOADING;
|
|
|
|
if(this.subResults){
|
|
|
|
this.subResults.unsubscribe();
|
2018-11-21 10:42:37 +01:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.zenodoCommunitySearchUtils.status = this.errorCodes.ERROR;
|
|
|
|
}
|
|
|
|
);
|
2018-11-21 10:42:37 +01: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) {
|
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
|
|
|
"redirectUrl": this._router.url
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.subscriptions.push(this._manageZenodoCommunitiesService.addZCommunity(this.properties, this.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'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
this.handleError('An error has been occurred. Try again later!');
|
|
|
|
console.log(err.status);
|
|
|
|
}/*,
|
2018-11-21 10:42:37 +01:00
|
|
|
() => {
|
|
|
|
console.info("completed ADD");
|
2019-02-14 13:20:00 +01:00
|
|
|
}*/
|
2021-05-19 13:40:29 +02:00
|
|
|
));
|
2018-11-21 10:42:37 +01:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public removeCommunity(comm) {
|
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
|
|
|
"redirectUrl": this._router.url
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var openaireId = this.getOpenaireId(comm);
|
|
|
|
this.subscriptions.push(this._manageZenodoCommunitiesService.removeZCommunity(this.properties, this.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;
|
2018-12-07 15:09:23 +01:00
|
|
|
}
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
if (pos != -1) {
|
|
|
|
this.selectedCommunities.splice(pos, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
UIkit.notification('Community successfully removed!', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
this.handleError('An error has been occurred. Try again later!');
|
|
|
|
console.log(err.status);
|
|
|
|
}/*,
|
2018-11-21 10:42:37 +01:00
|
|
|
() => {
|
|
|
|
console.info("completed remove");
|
2019-02-14 13:20:00 +01:00
|
|
|
}*/
|
2021-05-19 13:40:29 +02:00
|
|
|
));
|
2018-12-07 15:09:23 +01:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public onSearchClose() {
|
|
|
|
this.zenodoCommunitySearchUtils.keyword = this.filterForm.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public resetInput() {
|
|
|
|
this.zenodoCommunitySearchUtils.keyword = null;
|
|
|
|
this.searchInputComponent.reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(message: string) {
|
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
}
|
2018-11-21 10:42:37 +01:00
|
|
|
}
|