connect/src/app/searchPages/simple/searchDataproviders.compone...

145 lines
6.4 KiB
TypeScript
Raw Normal View History

import {Component, ViewChild} from '@angular/core';
import { ActivatedRoute} from '@angular/router';
import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
import {ErrorMessagesComponent} from '../../openaireLibrary/utils/errorMessages.component';
import {SearchFields} from '../../openaireLibrary/utils/properties/searchFields';
import {SearchPageTableViewComponent } from '../../openaireLibrary/searchPages/searchUtils/searchPageTableView.component';
import {SearchCustomFilter, SearchUtilsClass} from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
import {SearchCommunityDataprovidersService} from '../../openaireLibrary/connect/contentProviders/searchDataproviders.service';
import {ConnectHelper} from '../../openaireLibrary/connect/connectHelper';
import {PiwikHelper} from '../../utils/piwikHelper';
import {properties} from "../../../environments/environment";
import {CommunitiesService} from "../../openaireLibrary/connect/communities/communities.service";
import {CommunityService} from "../../openaireLibrary/connect/community/community.service";
import {Subscriber} from "rxjs";
@Component({
selector: 'openaire-search-dataproviders',
template: `
<search-page-table pageTitle="Search Content Providers"
type="content providers" entityType="dataprovider"
[results] = "results" [searchUtils] = "searchUtils"
[columnNames]="columnNames"
[showResultCount]=false
[openaireLink]="'https://' + (properties.environment == 'production'?'':'beta.') + 'explore.openaire.eu/search/find/dataproviders'"
[disableForms]="disableForms"
[enableSearchView]="enableSearchView"
searchFormClass="datasourcesTableSearchForm"
formPlaceholderText="Search by name..."
[piwikSiteId]="piwikSiteId" [hasPrefix]="false" [customFilter]="customFilter" [enableEntitySelection]="true"
[includeOnlyResultsAndFilter]="false" [showBreadcrumb]="true">
</search-page-table>
`
})
export class OpenaireSearchDataprovidersComponent {
private errorCodes: ErrorCodes;
private errorMessages: ErrorMessagesComponent;
public columnNames = ['Name', 'Official Name'];
public results =[];
public filters =[];
public searchUtils:SearchUtilsClass = new SearchUtilsClass();
public searchFields:SearchFields = new SearchFields();
public refineFields: string[] = [];// = this.searchFields.JOURNAL_FIELDS;
properties:EnvProperties= properties;
public disableForms: boolean = false;
public enableSearchView: boolean = true;
private communityId: string = '';
subscriptions = [];
@ViewChild (SearchPageTableViewComponent) searchPage : SearchPageTableViewComponent ;
piwikSiteId = null;
customFilter: SearchCustomFilter = null;
initialLoad = true;
constructor (private route: ActivatedRoute, private _searchDataprovidersService: SearchCommunityDataprovidersService, private _communitiesService: CommunitiesService, private _communityService: CommunityService) {
this.errorCodes = new ErrorCodes();
this.errorMessages = new ErrorMessagesComponent();
this.searchUtils.status = this.errorCodes.LOADING;
}
public ngOnInit() {
this.subscriptions.push(this.route.queryParams.subscribe(params => {
this.subscriptions.push(this._communityService.getCommunityAsObservable().subscribe(community =>{
if(community != null) {
this.communityId = community.communityId;
this.customFilter = new SearchCustomFilter("Community", "communityId", this.communityId, community.shortTitle);
this.piwikSiteId = PiwikHelper.getSiteId(this.communityId,this.properties.environment);
this.searchUtils.keyword = (params['fv0']?params['fv0']:(params['keyword']?params['keyword']:''));
//this.filters = this.createFilters();
//this.searchPage.getParametersFromUrl(params);
if(this.initialLoad) {
this.initialLoad = false;
console.log("Init get results")
this._getResults(params);
} else {
// this.searchPage.transform(this.results);
this.searchPage.goTo(1);
console.log("Init transfrm " + this.searchUtils.keyword)
}
}
}));
}));
}
public ngOnDestroy() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscriber) {
subscription.unsubscribe();
}
});
}
private _getResults(params){
this.searchUtils.status = this.errorCodes.LOADING;
this.disableForms = true;
this.enableSearchView = false;
this.results = [];
this.searchUtils.totalResults = 0;
let size: number = 0;
this.subscriptions.push(this._searchDataprovidersService.searchDataproviders(this.properties, this.communityId).subscribe(
data => {
this.searchUtils.totalResults = data.length;
this.results = data;
this.searchUtils.status = this.errorCodes.DONE;
if(this.searchUtils.totalResults == 0 ){
this.searchUtils.status = this.errorCodes.NONE;
} else {
this.searchPage.triggerInitialLoad();
this.searchPage.transform(this.results, this.searchUtils);
this.disableForms = false;
}
this.enableSearchView = true;
},
err => {
/*
console.log(err);
//TODO check erros (service not available, bad request)
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;
}
*/
this.handleError("Error getting content providers for community with id: "+this.communityId, err);
this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
this.enableSearchView = true;
}
));
}
private handleError(message: string, error) {
console.error("Content Providers simple Search Page: "+message, error);
}
}