Project

General

Profile

1
 import {SearchOrganizationsService} from '../../services/searchOrganizations.service';
2
 import { ErrorCodes} from '../../utils/properties/openaireProperties';
3
 import {SearchUtilsClass } from '../../searchPages/searchUtils/searchUtils.class';
4

    
5
export class FetchOrganizations {
6
  public results =[];
7

    
8
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
9
  public sub: any;
10
  public subResults: any;
11

    
12

    
13

    
14
  constructor ( private _searchOrganizationsService: SearchOrganizationsService ) {
15

    
16
    var errorCodes:ErrorCodes = new ErrorCodes();
17
    this.searchUtils.status =errorCodes.LOADING;
18

    
19
  }
20

    
21

    
22
  public ngOnDestroy() {
23
    if(this.sub){
24
      this.sub.unsubscribe();
25
    }
26
    if(this.subResults){
27
      this.subResults.unsubscribe();
28
    }
29
  }
30

    
31

    
32
    public getResultsByKeyword(keyword:string , page: number, size: number){
33
      var parameters = "";
34
      if(keyword.length > 0){
35
        parameters = "q=" + keyword;
36
      }
37

    
38
       var errorCodes:ErrorCodes = new ErrorCodes();
39
      this.searchUtils.status = errorCodes.LOADING;
40

    
41
      this.subResults = this._searchOrganizationsService.searchOrganizations(parameters, null, page, size, []).subscribe(
42
          data => {
43
              this.searchUtils.totalResults = data[0];
44
              console.info("search Organizations: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
45
              this.results = data[1];
46

    
47
              var errorCodes:ErrorCodes = new ErrorCodes();
48
              this.searchUtils.status = errorCodes.DONE;
49
              if(this.searchUtils.totalResults == 0 ){
50
                this.searchUtils.status = errorCodes.NONE;
51
              }
52
           },
53
          err => {
54
              console.log(err);
55
               //TODO check erros (service not available, bad request)
56
              // if( ){
57
              //   this.searchUtils.status = ErrorCodes.ERROR;
58
              // }
59
              var errorCodes:ErrorCodes = new ErrorCodes();
60
              this.searchUtils.status = errorCodes.ERROR;
61
           }
62
      );
63
    }
64

    
65

    
66
}
(3-3/6)