import {inject, Injectable, Signal, signal} from "@angular/core"; import {PublicationRestService} from "../../core/rest-services/publications/publication.rest-service"; import {AuthenticationService} from "../../core/service/authentication.service"; import {Publication} from "../../core/rest-services/publications/model/publication"; import {Router} from "@angular/router"; import {MatSnackBar} from "@angular/material/snack-bar"; @Injectable() export class MyPublicationsService { readonly #authenticationService = inject(AuthenticationService); readonly #publicationRestService = inject(PublicationRestService); readonly #snackBar = inject(MatSnackBar); readonly #router = inject(Router); #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(); } loadAuthenticatedUserPublications(): void { const authenticatedUser = this.#authenticationService.getAuthenticatedUser(); if (authenticatedUser) { this.#isLoading.set(true); this.#isLoaded.set(false); const query = `author_id=${authenticatedUser.id}`; this.#publicationRestService.search(query) .then(publications => { this.#publications.set(publications); }) .catch(error => { const errorMessage = $localize`An error occurred while retrieving your publications...`; this.#snackBar.open(errorMessage, $localize`Close`, {duration: 5000}); console.error(errorMessage, error); }) .finally(() => { this.#isLoading.set(false); this.#isLoaded.set(true); }); } else { this.#authenticationService.unauthenticate(); this.#snackBar.open($localize`You are unauthenticated. Please, log-in first.`, $localize`Close`, {duration: 5000}); this.#router.navigate(['/login']); } } }