Use signals in publication page.

This commit is contained in:
Florian THIERRY
2024-09-25 09:07:13 +02:00
parent cfa3015a7b
commit 7f2e762a44
3 changed files with 27 additions and 26 deletions

View File

@@ -1,14 +1,18 @@
@if (isLoading) { @if (isLoading()) {
<h2 i18n>Publication content loading...</h2> <h2 i18n>Publication content loading...</h2>
<mat-spinner></mat-spinner> <mat-spinner></mat-spinner>
} @else { } @else {
@if (publication) { @if (isPublicationUndefined()) {
<div class="loading-failed">
<h1 i18n>Publication failed to load...</h1>
</div>
} @else {
<div class="card"> <div class="card">
<img src="/api/pictures/{{ publication.illustrationId }}" /> <img src="/api/pictures/{{ publication()?.illustrationId }}" />
<header> <header>
<h1>{{ publication.title }}</h1> <h1>{{ publication()?.title }}</h1>
<h2>{{ publication.description }}</h2> <h2>{{ publication()?.description }}</h2>
@if (isAuthorAndUserEquals) { @if (isAuthorAndUserEquals()) {
<a [routerLink]="['edit']" <a [routerLink]="['edit']"
class="button action" class="button action"
matTooltip="Click to edit the publication" matTooltip="Click to edit the publication"
@@ -17,18 +21,18 @@
</a> </a>
} }
</header> </header>
<main [innerHTML]="publication.parsedText"></main> <main [innerHTML]="publication()?.parsedText"></main>
<footer> <footer>
<div class="metadata"> <div class="metadata">
<img src="/api/pictures/{{ publication.author.image }}" [matTooltip]="publication.author.name" /> <img src="/api/pictures/{{ publication()?.author?.image }}" [matTooltip]="publication()?.author?.name" />
<div class="posting-data"> <div class="posting-data">
<span i18n>Publication posted by {{ publication.author.name }}</span> <span i18n>Publication posted by {{ publication()?.author?.name }}</span>
<span class="publication-date"> <span class="publication-date">
({{ publication.creationDate | date: 'short' }}) ({{ publication()?.creationDate | date: 'short' }})
</span> </span>
</div> </div>
</div> </div>
@if (isAuthorAndUserEquals) { @if (isAuthorAndUserEquals()) {
<button type="button" <button type="button"
(click)="deletePublication()" (click)="deletePublication()"
matTooltip="Click to delete the publication" matTooltip="Click to delete the publication"
@@ -40,9 +44,5 @@
} }
</footer> </footer>
</div> </div>
} @else {
<div class="loading-failed">
<h1 i18n>Publication failed to load...</h1>
</div>
} }
} }

View File

@@ -2,7 +2,8 @@ $cardBorderRadius: .5em;
:host { :host {
display: flex; display: flex;
justify-content: center; flex-direction: column;
align-items: center;
.card { .card {
display: flex; display: flex;

View File

@@ -1,4 +1,4 @@
import { Component, OnDestroy, OnInit, inject } from '@angular/core'; import { Component, OnDestroy, OnInit, computed, inject, signal } from '@angular/core';
import { PublicationRestService } from '../../core/rest-services/publications/publication.rest-service'; import { PublicationRestService } from '../../core/rest-services/publications/publication.rest-service';
import { ActivatedRoute, Router, RouterModule } from '@angular/router'; import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
@@ -30,9 +30,10 @@ export class PublicationComponent implements OnInit, OnDestroy {
private readonly snackBar = inject(MatSnackBar); private readonly snackBar = inject(MatSnackBar);
private paramMapSubscription?: Subscription; private paramMapSubscription?: Subscription;
private afterDialogCloseSubscription?: Subscription; private afterDialogCloseSubscription?: Subscription;
isLoading: boolean = false; isLoading = signal(false);
isAuthorAndUserEquals: boolean = false; isAuthorAndUserEquals = computed(() => this.authenticationService.getAuthenticatedUser()?.id === this.publication()?.author.id);
publication?: Publication; publication = signal<Publication | undefined>(undefined);
isPublicationUndefined = computed(() => !this.publication())
ngOnInit(): void { ngOnInit(): void {
this.paramMapSubscription = this.activatedRoute this.paramMapSubscription = this.activatedRoute
@@ -41,12 +42,11 @@ export class PublicationComponent implements OnInit, OnDestroy {
const publicationId = params.get('publicationId'); const publicationId = params.get('publicationId');
if (publicationId) { if (publicationId) {
this.isLoading = true; this.isLoading.set(true);
this.publicationRestService.getById(publicationId) this.publicationRestService.getById(publicationId)
.then(publication => { .then(publication => {
this.publication = publication; this.publication.set(publication);
this.isAuthorAndUserEquals = this.authenticationService.getAuthenticatedUser()?.id === this.publication.author.id;
setTimeout(() => Prism.highlightAll(), 100); setTimeout(() => Prism.highlightAll(), 100);
}) })
.catch(error => { .catch(error => {
@@ -55,7 +55,7 @@ export class PublicationComponent implements OnInit, OnDestroy {
console.error(errorMessage, error); console.error(errorMessage, error);
}) })
.finally(() => { .finally(() => {
this.isLoading = false; this.isLoading.set(false);
}); });
} }
}); });
@@ -79,8 +79,8 @@ export class PublicationComponent implements OnInit, OnDestroy {
this.afterDialogCloseSubscription = dialogRef.afterClosed() this.afterDialogCloseSubscription = dialogRef.afterClosed()
.subscribe(response => { .subscribe(response => {
if (response && this.publication?.id) { if (response && !this.isPublicationUndefined()) {
this.publicationRestService.delete(this.publication.id); this.publicationRestService.delete(this.publication()?.id!!);
this.snackBar.open($localize`Publication deleted`, $localize`Close`, { duration: 5000 }); this.snackBar.open($localize`Publication deleted`, $localize`Close`, { duration: 5000 });
this.location.back(); this.location.back();
} }