openaire-library/utils/fetchEntitiesClasses/fetchOrganizations.class.ts

77 lines
2.4 KiB
TypeScript

import {SearchOrganizationsService} from '../../services/searchOrganizations.service';
import { ErrorCodes} from '../../utils/properties/openaireProperties';
import {SearchUtilsClass } from '../../searchPages/searchUtils/searchUtils.class';
export class FetchOrganizations {
private errorCodes: ErrorCodes;
public results =[];
public searchUtils:SearchUtilsClass = new SearchUtilsClass();
public sub: any;
public subResults: any;
constructor ( private _searchOrganizationsService: SearchOrganizationsService ) {
this.errorCodes = new ErrorCodes();
this.searchUtils.status = this.errorCodes.LOADING;
}
public ngOnDestroy() {
if(this.sub){
this.sub.unsubscribe();
}
if(this.subResults){
this.subResults.unsubscribe();
}
}
public getResultsByKeyword(keyword:string , page: number, size: number){
var parameters = "";
if(keyword.length > 0){
parameters = "q=" + keyword;
}
//var errorCodes:ErrorCodes = new ErrorCodes();
this.searchUtils.status = this.errorCodes.LOADING;
this.subResults = this._searchOrganizationsService.searchOrganizations(parameters, null, page, size, []).subscribe(
data => {
this.searchUtils.totalResults = data[0];
console.info("search Organizations: [Parameters:"+parameters+" ] [total results:"+this.searchUtils.totalResults+"]");
this.results = data[1];
//var errorCodes:ErrorCodes = new ErrorCodes();
this.searchUtils.status = this.errorCodes.DONE;
if(this.searchUtils.totalResults == 0 ){
this.searchUtils.status = this.errorCodes.NONE;
}
},
err => {
console.log(err);
//TODO check erros (service not available, bad request)
// if( ){
// this.searchUtils.status = ErrorCodes.ERROR;
// }
//var errorCodes:ErrorCodes = new ErrorCodes();
//this.searchUtils.status = errorCodes.ERROR;
if(err.status == '404') {
this.searchUtils.status = this.errorCodes.NOT_FOUND;
} else if(err.status == '500') {
this.searchUtils.status = this.errorCodes.ERROR;
} else {
this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
}
}
);
}
}