connect-admin/src/app/pages/zenodo-communities/manage-zenodo-communities.c...

190 lines
5.9 KiB
TypeScript

import {
Component,
EventEmitter,
Input,
OnChanges,
OnDestroy,
OnInit,
Output, SimpleChanges,
ViewChild,
ViewEncapsulation
} 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';
import {Session} from '../../openaireLibrary/login/utils/helper.class';
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
import {ManageZenodoCommunitiesService} from '../../services/manageZenodoCommunities.service';
import {SearchInputComponent} from '../../openaireLibrary/sharedComponents/search-input/search-input.component';
import {Subscription} from 'rxjs';
declare var UIkit;
@Component({
selector: 'manage-zenodo-communities',
templateUrl: './manage-zenodo-communities.component.html',
encapsulation: ViewEncapsulation.None // this used in order styles to work
})
export class ManageZenodoCommunitiesComponent implements OnInit, OnDestroy {
@Input() properties: EnvProperties = null;
@Input() communityId = null;
@Input() searchUtils: SearchUtilsClass = null;
errorCodes: ErrorCodes;
public rowsOnPage = 10;
@Input() masterCommunity = null;
@Input() selectedCommunities = [];
previewCommunities = [];
@ViewChild('AlertModalDeleteCommunity') alertModalDeleteCommunity;
selectedToDelete = null;
@Output() toggleView: EventEmitter<any> = new EventEmitter();
page = 1;
size = 10;
@ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent;
public filterForm: FormControl;
private subscriptions: any[] = [];
constructor(private route: ActivatedRoute,
private _router: Router,
public _fb: FormBuilder,
private _manageZenodoCommunitiesService: ManageZenodoCommunitiesService) {
this.errorCodes = new ErrorCodes();
}
ngOnInit() {
this.init();
}
ngOnDestroy() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscription) {
subscription.unsubscribe();
}
});
}
private init() {
this.filterForm = this._fb.control('');
this.filterPreviewCommunities("");
this.subscriptions.push(this.filterForm.valueChanges.subscribe(value => {
this.page = 1;
this.filterPreviewCommunities(value);
}));
this.searchUtils.keyword = "";
this.searchUtils.totalResults = this.selectedCommunities.length;
}
public filterPreviewCommunities(value: string) {
this.previewCommunities = this.selectedCommunities.filter(community => {
return community.title.toLowerCase().indexOf(value.toLowerCase()) != -1
});
if (this.previewCommunities.slice((this.page - 1) * this.rowsOnPage, this.page * this.rowsOnPage).length == 0) {
this.page = 1;
}
}
public confirmedDeleteCommunity(data: any) {
if (!Session.isLoggedIn()) {
this._router.navigate(['/user-info'], {
queryParams: {
"errorCode": LoginErrorCodes.NOT_VALID,
"redirectUrl": this._router.url
}
});
} else {
this.subscriptions.push(this._manageZenodoCommunitiesService.removeZCommunity(this.properties, this.communityId, this.selectedToDelete.openaireId).subscribe(
data => {
var pos = -1;
for (var i = 0; i < this.selectedCommunities.length; i++) {
if (this.selectedCommunities[i].id == this.selectedToDelete.id) {
pos = i;
break;
}
}
if (pos != -1) {
this.selectedCommunities.splice(pos, 1);
this.searchUtils.totalResults = this.selectedCommunities.length;
}
this.searchUtils.totalResults = this.selectedCommunities.length;
this.filterPreviewCommunities(this.filterForm.value);
UIkit.notification('Community has been <b>successfully removed</b>!', {
status: 'success',
timeout: 6000,
pos: 'bottom-right'
});
},
err => {
this.handleError('An error has been occurred. Try again later!');
console.log(err.status);
}
));
}
}
public removeCommunity(comm) {
if (!Session.isLoggedIn()) {
this._router.navigate(['/user-info'], {
queryParams: {
"errorCode": LoginErrorCodes.NOT_VALID,
"redirectUrl": this._router.url
}
});
} else {
this.selectedToDelete = comm;
this.alertModalDeleteCommunity.cancelButton = true;
this.alertModalDeleteCommunity.okButton = true;
this.alertModalDeleteCommunity.alertTitle = "Remove zenodo community?";
let title = "";
if (comm.title) {
title = comm.title;
}
this.alertModalDeleteCommunity.message = "Zenodo community";
if (title) {
this.alertModalDeleteCommunity.message += " '" + title + "' ";
}
this.alertModalDeleteCommunity.message += "will be removed from your community. Are you sure?";
this.alertModalDeleteCommunity.okButtonText = "Yes";
this.alertModalDeleteCommunity.open();
}
}
totalPages(): number {
let totalPages: any = this.searchUtils.totalResults / (this.rowsOnPage);
if (!(Number.isInteger(totalPages))) {
totalPages = (parseInt(totalPages, 10) + 1);
}
return totalPages;
}
addNew() {
this.toggleView.emit(null);
}
public onSearchClose() {
this.searchUtils.keyword = this.filterForm.value;
}
public resetInput() {
this.searchUtils.keyword = null;
this.searchInputComponent.reset()
}
handleError(message: string) {
UIkit.notification(message, {
status: 'danger',
timeout: 6000,
pos: 'bottom-right'
});
}
}