35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
import {inject, Injectable, Signal, signal} from "@angular/core";
|
|
import {PublicationRestService} from "../../core/rest-services/publications/publication.rest-service";
|
|
import {MatSnackBar} from "@angular/material/snack-bar"
|
|
import {Publication} from "../../core/rest-services/publications/model/publication";
|
|
|
|
@Injectable()
|
|
export class HomeService {
|
|
private publicationRestService = inject(PublicationRestService);
|
|
private snackBar = inject(MatSnackBar);
|
|
|
|
#publications = signal<Publication[]>([]);
|
|
#isLoadingSubject = signal<boolean>(false);
|
|
|
|
get isLoading(): Signal<boolean> {
|
|
return this.#isLoadingSubject.asReadonly();
|
|
}
|
|
|
|
get publications(): Signal<Publication[]> {
|
|
return this.#publications.asReadonly();
|
|
}
|
|
|
|
startLatestPublicationsRetrieving(): void {
|
|
this.#isLoadingSubject.set(true);
|
|
|
|
this.publicationRestService.getLatest()
|
|
.then(publications => this.#publications.set(publications))
|
|
.catch(error => {
|
|
const errorMessage = $localize`An error occurred while retrieving latest publications...`;
|
|
this.snackBar.open(errorMessage, $localize`Close`, {duration: 5000});
|
|
console.error(errorMessage, error);
|
|
})
|
|
.finally(() => this.#isLoadingSubject.set(false));
|
|
}
|
|
}
|