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
  private errorCodes: ErrorCodes;
7

    
8
  public results =[];
9

    
10
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
11
  public sub: any;
12
  public subResults: any;
13

    
14

    
15

    
16
  constructor ( private _searchOrganizationsService: SearchOrganizationsService ) {
17

    
18
    this.errorCodes = new ErrorCodes();
19
    this.searchUtils.status = this.errorCodes.LOADING;
20

    
21
  }
22

    
23

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

    
33

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

    
40
      //var errorCodes:ErrorCodes = new ErrorCodes();
41
      this.searchUtils.status = this.errorCodes.LOADING;
42

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

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

    
64
              if(err.status == '404') {
65
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
66
              } else if(err.status == '500') {
67
                this.searchUtils.status = this.errorCodes.ERROR;
68
              } else {
69
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
70
              }
71
           }
72
      );
73
    }
74

    
75

    
76
}
(3-3/6)