Fixing Angular 21 by migrating all values by signals. (#11)
Some checks failed
Build and Deploy Java Gradle Application / build-and-deploy (push) Failing after 53s

This commit was merged in pull request #11.
This commit is contained in:
2026-02-03 15:07:55 +01:00
parent 1ca2f872f7
commit 0cce8b2982
102 changed files with 4102 additions and 4852 deletions

View File

@@ -1,57 +1,56 @@
import { inject, Injectable } from "@angular/core";
import { PublicationRestService } from "../../core/rest-services/publications/publication.rest-service";
import { AuthenticationService } from "../../core/service/authentication.service";
import { BehaviorSubject, Observable } from "rxjs";
import { Publication } from "../../core/rest-services/publications/model/publication";
import { Router } from "@angular/router";
import { MatSnackBar } from "@angular/material/snack-bar";
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 {
private readonly authenticationService = inject(AuthenticationService);
private readonly publicationRestService = inject(PublicationRestService);
private readonly snackBar = inject(MatSnackBar);
private readonly router = inject(Router);
private publicationsSubject = new BehaviorSubject<Publication[]>([]);
private isLoadingSubject = new BehaviorSubject<boolean>(false);
private isLoadedSubject = new BehaviorSubject<boolean>(false);
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$(): Observable<Publication[]> {
return this.publicationsSubject.asObservable();
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']);
}
get isLoading$(): Observable<boolean> {
return this.isLoadingSubject.asObservable();
}
get isLoaded$(): Observable<boolean> {
return this.isLoadedSubject.asObservable();
}
loadAuthenticatedUserPublications(): void {
const authenticatedUser = this.authenticationService.getAuthenticatedUser();
if (authenticatedUser) {
this.isLoadingSubject.next(true);
this.isLoadedSubject.next(false);
const query = `author_id=${authenticatedUser.id}`;
this.publicationRestService.search(query)
.then(publications => {
this.publicationsSubject.next(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.isLoadingSubject.next(false);
this.isLoadedSubject.next(true);
});
} else {
this.authenticationService.unauthenticate();
this.snackBar.open($localize`You are unauthenticated. Please, log-in first.`, $localize`Close`, { duration: 5000 });
this.router.navigate(['/login']);
}
}
}
}
}