Files
codiki-hexagonal/frontend/src/app/pages/my-publications/my-publications.service.ts
Florian THIERRY - Takiguchi 0cce8b2982
Some checks failed
Build and Deploy Java Gradle Application / build-and-deploy (push) Failing after 53s
Fixing Angular 21 by migrating all values by signals. (#11)
2026-02-03 15:07:55 +01:00

57 lines
2.1 KiB
TypeScript

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<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();
}
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']);
}
}
}