Add "my-publications" page.

This commit is contained in:
Florian THIERRY
2024-09-04 14:06:10 +02:00
parent c4dea2cc85
commit 64119a956a
9 changed files with 200 additions and 13 deletions

View File

@@ -0,0 +1,57 @@
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";
@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);
get publications$(): Observable<Publication[]> {
return this.publicationsSubject.asObservable();
}
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 = 'An error occurred while retrieving your publications...';
this.snackBar.open(errorMessage);
console.error(errorMessage, error);
})
.finally(() => {
this.isLoadingSubject.next(false);
this.isLoadedSubject.next(true);
});
} else {
this.authenticationService.unauthenticate();
this.snackBar.open('You are unauthenticated. Please, log-in first.', 'Close', { duration: 5000 });
this.router.navigate(['/login']);
}
}
}