import {inject, Injectable, Signal, signal} from "@angular/core"; import {PublicationRestService} from "../../core/rest-services/publications/publication.rest-service"; import {Publication} from "../../core/rest-services/publications/model/publication"; import {MatSnackBar} from "@angular/material/snack-bar"; @Injectable() export class SearchPublicationsService { #snackBar = inject(MatSnackBar); #publicationRestService = inject(PublicationRestService); #publications = signal([]); #isLoading = signal(false); #isLoaded = signal(false); get publications(): Signal { return this.#publications.asReadonly(); } get isLoading(): Signal { return this.#isLoading.asReadonly(); } get isLoaded(): Signal { return this.#isLoaded.asReadonly(); } loadPublications(searchCriteria: string): void { this.#isLoading.set(true); this.#isLoaded.set(false); this.#publications.set([]); this.#publicationRestService.search(searchCriteria) .then(publications => { this.#publications.set(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.#isLoading.set(false); this.#isLoaded.set(true); }); } }