Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import { ActivatedRoute} from '@angular/router';
3
import {Location} from '@angular/common';
4
import { Filter, Value} from './searchUtils/searchHelperClasses.class';
5
// import {RefineResultsService} from '../services/servicesUtils/refineResuts.service';
6

    
7
import {SearchProjectsService} from '../services/searchProjects.service';
8
import {SearchResult}     from '../utils/entities/searchResult';
9
import {OpenaireProperties, ErrorCodes} from '../utils/properties/openaireProperties';
10
import {SearchFields} from '../utils/properties/searchFields';
11
import {SearchPageComponent } from './searchUtils/searchPage.component';
12
@Component({
13
    selector: 'search-projects',
14
    template: `
15

    
16
    <search-page pageTitle="Search Projects" type="project" [(filters)] = "filters"
17
                 [(results)] = "results" [(totalResults)] = "totalResults" [(keyword)] = "keyword"
18
                 [(page)] = "page" [(size)] = "size" [(status)] = "status"   [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)"  >
19
    </search-page>
20
     `
21

    
22
})
23
export class SearchProjectsComponent {
24
  public results =[];
25
  private filters: Filter[] =[];
26
  public totalResults:number  = 0 ;
27
  public status:number;
28
  private baseUrl:string;
29
  private keyword = '';
30
  private page :number = 1;
31
  private size :number = 10;
32
  private sub: any;
33
  private refineFields = [];
34
  private searchFields:SearchFields = new SearchFields();
35
  private urlParams : Map<string, string>  ;
36
  private _location:Location;
37

    
38
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
39

    
40
  constructor (private route: ActivatedRoute, private _searchProjectsService: SearchProjectsService) {
41
    var errorCodes:ErrorCodes = new ErrorCodes();
42
    this.status =errorCodes.LOADING;
43
    this.baseUrl = OpenaireProperties.getLinkToSearchProjects();
44
    this.refineFields = this.searchFields.getPROJECT_FIELDS();
45
    if(!this.searchPage){
46
      this.searchPage = new SearchPageComponent(this._location);
47
    }
48
  }
49

    
50
  private ngOnInit() {
51
    this.searchPage.refineFields = this.refineFields;
52
    console.info("Here:: init pr"+this.refineFields.length);
53
    //get refine field filters  from url parameters
54

    
55
    this.sub =  this.route.queryParams.subscribe(params => {
56

    
57
        //get keyword from url parameters
58
        this.keyword = (params['keyword']?params['keyword']:'');
59

    
60
        //get page from url parameters
61
        this.page = (params['page']=== undefined)?1:+params['page'];
62

    
63

    
64
         this.searchPage.getSelectedFiltersFromUrl(params);
65
        this.getResults(this.keyword, this.page, this.size);
66
    });
67
  }
68

    
69
  private ngOnDestroy() {
70
    this.sub.unsubscribe();
71
  }
72

    
73
  public getResults(parameters:string, page: number, size: number){
74

    
75
    this._searchProjectsService.searchProjects(parameters+this.searchPage.getRefineFieldsQuery(), page, size, this.searchPage.getFields()).subscribe(
76
        data => {
77
            this.totalResults = data[0];
78
            console.info("search Projects: [Parameters:"+parameters+" ]  [total results:"+this.totalResults+"]");
79
            this.results = data[1];
80
              this.filters = this.searchPage.checkSelectedFilters(data[2]);
81
              this.searchPage.updateBaseUrlWithParameters(this.filters);
82
            var errorCodes:ErrorCodes = new ErrorCodes();
83
            this.status = errorCodes.DONE;
84
            if(this.totalResults == 0 ){
85
              this.status = errorCodes.NONE;
86
            }
87
        },
88
        err => {
89
            console.error(err);
90
             //TODO check erros (service not available, bad request)
91
            // if( ){
92
            //   this.status = ErrorCodes.ERROR;
93
            // }
94
            var errorCodes:ErrorCodes = new ErrorCodes();
95
            this.status = errorCodes.ERROR;
96
        }
97
    );
98
  }
99

    
100

    
101
  private queryChanged($event) {
102
    this.urlParams = undefined;
103
    var parameters = $event.value;
104
    this.getResults(parameters, this.page, this.size);
105
  }
106

    
107
}
(8-8/9)