48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
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<Publication[]>([]);
|
|
#isLoading = signal(false);
|
|
#isLoaded = signal(false);
|
|
|
|
get publications(): Signal<Publication[]> {
|
|
return this.#publications.asReadonly();
|
|
}
|
|
|
|
get isLoading(): Signal<boolean> {
|
|
return this.#isLoading.asReadonly();
|
|
}
|
|
|
|
get isLoaded(): Signal<boolean> {
|
|
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);
|
|
});
|
|
}
|
|
}
|