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,11 @@
<h1>Your publications</h1>
@if ((isLoading$ | async) === true) {
<h2>Publication loading...</h2>
<mat-spinner></mat-spinner>
} @else {
@if ((isLoaded$ | async) === true) {
<app-publication-list [publications$]="publications$"></app-publication-list>
} @else {
<h2>There is no any publication...</h2>
}
}

View File

@@ -0,0 +1,5 @@
:host {
display: flex;
flex-direction: column;
align-items: center;
}

View File

@@ -0,0 +1,36 @@
import { Component, inject, OnInit } from "@angular/core";
import { MatProgressSpinnerModule } from "@angular/material/progress-spinner";
import { MyPublicationsService } from "./my-publications.service";
import { Observable } from "rxjs";
import { PublicationListComponent } from "../../components/publication-list/publication-list.component";
import { Publication } from "../../core/rest-services/publications/model/publication";
import { CommonModule } from "@angular/common";
@Component({
selector: 'app-my-component',
standalone: true,
templateUrl: './my-publications.component.html',
styleUrl: './my-publications.component.scss',
imports: [CommonModule, MatProgressSpinnerModule, PublicationListComponent],
providers: [MyPublicationsService]
})
export class MyPublicationsComponent implements OnInit {
private readonly myPublicationsService = inject(MyPublicationsService);
get publications$(): Observable<Publication[]> {
return this.myPublicationsService.publications$;
}
get isLoading$(): Observable<boolean> {
return this.myPublicationsService.isLoading$;
}
get isLoaded$(): Observable<boolean> {
return this.myPublicationsService.isLoaded$;
}
ngOnInit(): void {
this.myPublicationsService.loadAuthenticatedUserPublications();
}
}

View File

@@ -0,0 +1,7 @@
import { Route } from "@angular/router";
import { authenticationGuard } from "../../core/guard/authentication.guard";
import { MyPublicationsComponent } from "./my-publications.component";
export const ROUTES: Route[] = [
{ path: '', component: MyPublicationsComponent, canActivate: [authenticationGuard] }
]

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