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

78 lines
2.5 KiB
TypeScript

import {SearchOrganizationsService} from '../../services/searchOrganizations.service';
import { ErrorCodes} from '../../utils/properties/errorCodes';
import {SearchUtilsClass } from '../../searchPages/searchUtils/searchUtils.class';
import{EnvProperties} from '../../utils/properties/env-properties';
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, properties:EnvProperties){
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, [], properties).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;
}
}
);
}
}