connect-admin/src/app/pages/content-providers/add-content-providers.compo...

300 lines
11 KiB
TypeScript

import {Component, OnInit, Input, Output, EventEmitter, ViewChild} from '@angular/core';
import { ActivatedRoute, Router } from "@angular/router";
import {SearchResult} from '../../openaireLibrary/utils/entities/searchResult';
import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
import {SearchUtilsClass } from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
import {SearchDataprovidersService} from '../../openaireLibrary/services/searchDataproviders.service';
import {RouterHelper} from '../../openaireLibrary/utils/routerHelper.class';
import {StringUtils} from '../../openaireLibrary/utils/string-utils.class';
import {ManageCommunityContentProvidersService} from '../../services/manageContentProviders.service';
import {Session} from '../../openaireLibrary/login/utils/helper.class';
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
import {properties} from "../../../environments/environment";
import {FormBuilder, FormGroup} from "@angular/forms";
import {SearchInputComponent} from "../../openaireLibrary/sharedComponents/search-input/search-input.component";
import {Subscriber} from "rxjs";
import {debounceTime, distinctUntilChanged} from "rxjs/operators";
import {ResultPreview} from "../../openaireLibrary/utils/result-preview/result-preview";
declare var UIkit;
@Component({
selector: 'add-content-providers',
templateUrl: './add-content-providers.component.html',
})
export class AddContentProvidersComponent implements OnInit {
private subscriptions: any[] = [];
public subResults: any;
private community: string = '';
public routerHelper: RouterHelper = new RouterHelper();
public properties: EnvProperties = properties;
public errorCodes: ErrorCodes;
public openaireSearchUtils: SearchUtilsClass = new SearchUtilsClass();
@Output() communityContentProvidersChanged = new EventEmitter();
@Input() communityContentProviders = [];
public openaireContentProviders = [];
public queryParameters: string = "";
// public pagingLimit: number = properties.pagingLimit;
public resultsPerPage: number = properties.resultsPerPage;
filterForm: FormGroup;
@ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent;
private contentProviderUrl: string = "https://" + ((properties.environment == "beta" || properties.environment == "development") ? "beta." : "") + "explore.openaire.eu" + properties.searchLinkToDataProvider;
public body: string = "Send from page";
@Output() toggleView: EventEmitter<any> = new EventEmitter();
constructor(private route: ActivatedRoute, private _router: Router,
private _searchContentProvidersService: SearchDataprovidersService,
private _manageCommunityContentProvidersService: ManageCommunityContentProvidersService,
private _fb: FormBuilder) {
this.errorCodes = new ErrorCodes();
this.openaireSearchUtils.status = this.errorCodes.LOADING;
}
ngOnInit() {
this.subscriptions.push(this.route.params.subscribe(params => {
this.openaireSearchUtils.status = this.errorCodes.LOADING;
this.community = params['community'];
// this.contentProviderUrl = "https://" + ((this.properties.environment == "beta" || this.properties.environment == "development") ? "beta." : "")
// + this.community + ".openaire.eu" + this.properties.searchLinkToDataProvider;
this._getOpenaireContentProviders("", 1, this.resultsPerPage);
this.body = "[Please write your message here]";
this.body = StringUtils.URIEncode(this.body);
}));
this.openaireSearchUtils.keyword = "";
this.filterForm = this._fb.group({
keyword: [''],
});
this.subscriptions.push(this.filterForm.get('keyword').valueChanges
.pipe(debounceTime(1000), distinctUntilChanged())
.subscribe(value => {
this.keywordChanged(value);
})
);
}
public ngOnDestroy() {
this.subscriptions.forEach(sub => {
if (sub instanceof Subscriber) {
sub.unsubscribe();
}
});
if(this.subResults){
this.subResults.unsubscribe();
}
}
public addContentProvider(contenProvider: SearchResult) {
if (!Session.isLoggedIn()) {
this._router.navigate(['/user-info'], {
queryParams: {
"errorCode": LoginErrorCodes.NOT_VALID,
"redirectUrl": this._router.url
}
});
} else {
this.subscriptions.push(this._manageCommunityContentProvidersService.addContentProvider(this.properties, this.community, contenProvider).subscribe(
data => {
this.communityContentProviders.push(data);
UIkit.notification('Content Provider successfully added!', {
status: 'success',
timeout: 6000,
pos: 'bottom-right'
});
this.communityContentProvidersChanged.emit({
value: this.communityContentProviders,
});
},
err => {
this.handleError('An error has been occurred. Try again later!');
console.error(err.status);
}
));
}
}
public removeContentProvider(contentProvider) {
if (!Session.isLoggedIn()) {
this._router.navigate(['/user-info'], {
queryParams: {
"errorCode": LoginErrorCodes.NOT_VALID,
"redirectUrl": this._router.url
}
});
} else {
let communityContentProvider = this.getCommunityContentProvider(contentProvider);
let contentProviderId: string = communityContentProvider['id'];
this.subscriptions.push(this._manageCommunityContentProvidersService.removeContentProvider(this.properties, this.community, contentProviderId).subscribe(
data => {
let index = this.communityContentProviders.indexOf(communityContentProvider);
this.communityContentProviders.splice(index, 1);
UIkit.notification('Content Provider successfully removed!', {
status: 'success',
timeout: 6000,
pos: 'bottom-right'
});
this.communityContentProvidersChanged.emit({
value: this.communityContentProviders,
});
},
err => {
this.handleError('An error has been occurred. Try again later!');
console.error(err);
}
));
}
}
public getCommunityContentProvider(contentProvider: any): string {
let index: number = 0;
for (let communityContentProvider of this.communityContentProviders) {
if (communityContentProvider.openaireId == contentProvider.id) {
return communityContentProvider;
}
index++;
}
return "";
}
public getResultPreview(result: SearchResult): ResultPreview {
return ResultPreview.searchResultConvert(result, "dataprovider");
}
// public inCommunity(contentProvider: any): any {
// for(let communityContentProvider of this.communityContentProviders) {
// if(communityContentProvider.openaireId == contentProvider.id) {
// return true;
// }
// }
//
// if(this.undo[contentProvider.id]) {
// return true;
// }
// return false;
// }
private _getOpenaireContentProviders(parameters: string, page: number, size: number) {
if (!Session.isLoggedIn()) {
this._router.navigate(['/user-info'], {
queryParams: {
"errorCode": LoginErrorCodes.NOT_VALID,
"redirectUrl": this._router.url
}
});
} else {
// if (page > this.pagingLimit) {
// size = 0;
// }
if (this.openaireSearchUtils.status == this.errorCodes.LOADING) {
this.openaireSearchUtils.status = this.errorCodes.LOADING;
this.openaireContentProviders = [];
this.openaireSearchUtils.totalResults = 0;
if(this.subResults){
this.subResults.unsubscribe();
}
this.subResults = this._searchContentProvidersService.searchDataproviders(parameters, null, page, size, [], this.properties).subscribe(
data => {
this.openaireSearchUtils.totalResults = data[0];
this.openaireContentProviders = data[1];
//this.searchPage.checkSelectedFilters(this.filters);
this.openaireSearchUtils.status = this.errorCodes.DONE;
if (this.openaireSearchUtils.totalResults == 0) {
this.openaireSearchUtils.status = this.errorCodes.NONE;
}
// if (this.openaireSearchUtils.status == this.errorCodes.DONE) {
// // Page out of limit!!!
// let totalPages: any = this.openaireSearchUtils.totalResults / (this.openaireSearchUtils.size);
// if (!(Number.isInteger(totalPages))) {
// totalPages = (parseInt(totalPages, 10) + 1);
// }
// if (totalPages < page) {
// this.openaireSearchUtils.totalResults = 0;
// this.openaireSearchUtils.status = this.errorCodes.OUT_OF_BOUND;
// }
// }
},
err => {
console.error(err);
//TODO check erros (service not available, bad request)
if (err.status == '404') {
this.openaireSearchUtils.status = this.errorCodes.NOT_FOUND;
} else if (err.status == '500') {
this.openaireSearchUtils.status = this.errorCodes.ERROR;
} else {
this.openaireSearchUtils.status = this.errorCodes.NOT_AVAILABLE;
}
}
);
}
}
}
totalPages(): number {
let totalPages: any = this.openaireSearchUtils.totalResults / (this.resultsPerPage);
if (!(Number.isInteger(totalPages))) {
totalPages = (parseInt(totalPages, 10) + 1);
}
return totalPages;
}
keywordChanged(keyword) {
this.openaireSearchUtils.keyword = keyword;
this.buildQueryParameters();
this.goTo(1);
}
buildQueryParameters() {
this.queryParameters = "";
if (this.openaireSearchUtils.keyword) {
this.queryParameters = "q=" + StringUtils.URIEncode(this.openaireSearchUtils.keyword);
}
}
goTo(page: number = 1) {
this.openaireSearchUtils.page = page;
this.openaireSearchUtils.status = this.errorCodes.LOADING;
this._getOpenaireContentProviders(this.queryParameters, page, this.resultsPerPage);
}
back() {
this.toggleView.emit(null);
}
public onSearchClose() {
this.openaireSearchUtils.keyword = this.filterForm.get('keyword').value;
}
public resetInput() {
this.openaireSearchUtils.keyword = null;
this.searchInputComponent.reset()
}
handleError(message: string) {
UIkit.notification(message, {
status: 'danger',
timeout: 6000,
pos: 'bottom-right'
});
}
}