import { inject, Injectable } from "@angular/core"; import { PublicationRestService } from "../../core/rest-services/publications/publication.rest-service"; import { BehaviorSubject, Observable } from "rxjs"; import { Publication } from "../../core/rest-services/publications/model/publication"; import { MatSnackBar } from "@angular/material/snack-bar"; @Injectable() export class SearchPublicationsService { private publicationRestService = inject(PublicationRestService); private publicationsSubject = new BehaviorSubject([]); private isLoadingSubject = new BehaviorSubject(false); private isLoadedSubject = new BehaviorSubject(false); private snackBar = inject(MatSnackBar); get publications$(): Observable { return this.publicationsSubject.asObservable(); } get isLoading$(): Observable { return this.isLoadingSubject.asObservable(); } get isLoaded$(): Observable { return this.isLoadedSubject.asObservable(); } loadPublications(searchCriteria: string): void { this.isLoadingSubject.next(true); this.isLoadedSubject.next(false); this.publicationsSubject.next([]); this.publicationRestService.search(searchCriteria) .then(publications => { this.publicationsSubject.next(publications); }) .catch(error => { if (error.status !== 404) { const errorMessage = $localize`An error occured while retrieving publications.`; console.error(errorMessage, error); this.snackBar.open(errorMessage, $localize`Close`, { duration: 5000 }); } }) .finally(() => { this.isLoadingSubject.next(false); this.isLoadedSubject.next(true); }); } }