import {ChangeDetectorRef, Component, OnDestroy, OnInit, ViewChild} from "@angular/core";
import {CommunityService} from "../../openaireLibrary/connect/community/community.service";
import {AdvancedCriteriaService} from "./advanced-criteria.service";
import {Subscription} from "rxjs";
import {ContentProvider, SelectionCriteria} from "../../openaireLibrary/utils/entities/contentProvider";
import {
FullScreenModalComponent
} from "../../openaireLibrary/utils/modal/full-screen-modal/full-screen-modal.component";
import {NotificationHandler} from "../../openaireLibrary/utils/notification-handler";
import {CriteriaComponent} from "../content-providers/criteria/criteria.component";
import {CriteriaUtils} from "../content-providers/criteria-utils";
import {OpenaireEntities} from "../../openaireLibrary/utils/properties/searchFields";
import {CommunityInfo} from "../../openaireLibrary/connect/community/communityInfo";
@Component({
selector: 'advanced-criteria',
template: `
0" class="uk-margin-small-bottom">
Criteria
No criteria yet
Add multiple constraints in AND to define a criterion: results that satisfy at least one of the criteria will be included in your community.
No Criteria yet
Add multiple constraints in AND to define a criterion: results that satisfy at least one of the criteria will be included in your community.
`
})
export class AdvancedCriteriaComponent implements OnInit, OnDestroy {
public loading = true;
public selectionCriteria: SelectionCriteria;
public community: CommunityInfo;
public criteriaUtils: CriteriaUtils = new CriteriaUtils();
private subscriptions: any[] = [];
@ViewChild('criteria') criteria: CriteriaComponent;
@ViewChild('filtersModal', { static: true }) filtersModal: FullScreenModalComponent;
constructor(private communityService: CommunityService,
private cdr: ChangeDetectorRef,
private advancedCriteriaService: AdvancedCriteriaService) {}
ngOnInit() {
this.loading = true;
this.subscriptions.push(this.communityService.getCommunityAsObservable().subscribe(community => {
this.community = community;
this.selectionCriteria = community.selectionCriteria;
this.loading = false;
}));
}
saveCriteria() {
let callback = (selectionCriteria): void => {
this.advancedCriteriaService.saveAdvancedCriteria(this.community.communityId, selectionCriteria).subscribe(() => {
this.criteria.reset();
this.criteria.loading = false;
NotificationHandler.rise('Filters have been successfully updated');
}, error => {
this.criteria.loading = false;
this.criteria.handeError('An error has been occurred. Try again later!', error);
});
}
this.loading = true;
this.criteria.save(callback);
}
public openCriteria() {
this.criteria.reset();
this.filtersModal.title = 'Criteria';
this.filtersModal.okButtonText = "Save";
this.filtersModal.back = true;
this.filtersModal.okButton = true;
this.filtersModal.open();
this.cdr.detectChanges();
}
ngOnDestroy() {
this.subscriptions.forEach(subscription => {
if(subscription instanceof Subscription) {
subscription.unsubscribe();
}
})
}
}