début de style et d'implémentation des commentaire sur la page de visualisation d'un post

This commit is contained in:
Guams 2025-01-12 22:49:36 +01:00
parent 118b01294a
commit 53b18fc4e0
6 changed files with 113 additions and 16 deletions

View File

@ -1,5 +1,9 @@
export interface Comment { export interface Comment {
id: bigint id: bigint
authorId: string
authorName: string
profilePicture: string
authorRole: string
content: string content: string
commentDate: Date commentDate: Date
isUpdated: boolean isUpdated: boolean

View File

@ -39,7 +39,14 @@ export class HomeComponent implements OnDestroy {
} }
this.subs.push(this.postService.list() this.subs.push(this.postService.list()
.subscribe({ .subscribe({
next: (posts: Post[]) => this.posts = posts, next: (posts: Post[]) => this.posts = posts.sort((a, b) => {
if (a.publicationDate < b.publicationDate) {
return -1
} else if (a.publicationDate > b.publicationDate) {
return 1
}
return 0
}),
error: (err) => console.error(err.message), error: (err) => console.error(err.message),
})); }));
} }

View File

@ -0,0 +1,31 @@
::ng-deep * {
box-sizing: border-box;
}
.body-content {
max-width: 100%;
display: flex;
flex-direction: column;
align-items: center;
}
.comment-div {
display: flex;
align-items: center;
gap: 1rem;
}
::ng-deep .body-content * {
white-space: normal;
word-break: break-word;
width: 100%;
}
img {
height: 100%
}
.main-content {
margin: auto;
width: 45%;
}

View File

@ -1,5 +1,33 @@
<app-header></app-header> <app-header></app-header>
<h1>{{ concernedPost?.title }}</h1> <div class="main-content">
<em>Publié le {{ concernedPost?.publicationDate | date: "dd/MM/yyyy à HH:mm" }}</em> <h1>{{ concernedPost?.title }}</h1>
<div [innerHTML]="concernedPost?.body"></div> <em>Publié le {{ concernedPost?.publicationDate | date: "dd/MM/yyyy à HH:mm" }}</em>
<div></div> <div class="body-content" [innerHTML]="concernedPost?.body"></div>
@for (comment of comments; track comment.id) {
@if (comment.profilePicture) {
<div class="comment-div">
<p-avatar image="data:image/jpeg;base64,{{ comment.profilePicture }}" styleClass="mr-2" size="large"></p-avatar>
<em>{{ comment.authorName }}</em>
<p class="{{ comment.authorRole }}">- {{ comment.authorRole }}</p>
@if (comment.isUpdated) {
<p> Mis à jour le {{ comment.commentDate | date: "dd/MM/yyyy à HH:mm" }}</p>
} @else {
<p> Envoyé le {{ comment.commentDate | date: "dd/MM/yyyy à HH:mm" }}</p>
}
</div>
} @else {
<div class="comment-div">
<p-avatar label="{{ comment.authorName.charAt(0).toUpperCase() }}" styleClass="mr-2" size="large"></p-avatar>
<em>{{ comment.authorName }}</em>
<p class="{{ comment.authorRole }}">- {{ comment.authorRole }}</p>
@if (comment.isUpdated) {
<p> Mis à jour le {{ comment.commentDate | date: "dd/MM/yyyy à HH:mm" }}</p>
} @else {
<p> Envoyé le {{ comment.commentDate | date: "dd/MM/yyyy à HH:mm" }}</p>
}
</div>
}
<p>{{ comment.content }}</p>
}
</div>

View File

@ -1,42 +1,68 @@
import {Component} from '@angular/core'; import {Component} from '@angular/core';
import {ActivatedRoute} from '@angular/router'; import {ActivatedRoute} from '@angular/router';
import {mergeMap, Subscription} from 'rxjs'; import {forkJoin, Subscription} from 'rxjs';
import {Post} from '../../models/post'; import {Post} from '../../models/post';
import {PostService} from '../../services/post.service'; import {PostService} from '../../services/post.service';
import {HeaderComponent} from '../../components/header/header.component'; import {HeaderComponent} from '../../components/header/header.component';
import {DatePipe} from '@angular/common'; import {DatePipe, JsonPipe} from '@angular/common';
import {CommentService} from '../../services/comment.service'; import {CommentService} from '../../services/comment.service';
import {MessageService} from 'primeng/api';
import {Comment} from '../../models/comment';
import {AvatarModule} from 'primeng/avatar';
@Component({ @Component({
selector: 'app-post', selector: 'app-post',
standalone: true, standalone: true,
imports: [ imports: [
HeaderComponent, HeaderComponent,
DatePipe DatePipe,
JsonPipe,
AvatarModule
], ],
templateUrl: './post.component.html', templateUrl: './post.component.html',
styleUrl: './post.component.css' styleUrl: './post.component.css'
}) })
export class PostComponent { export class PostComponent {
subs: Subscription[] = []; subs: Subscription[] = [];
comments: Comment[] = [];
concernedPost: Post | undefined; concernedPost: Post | undefined;
constructor(private route: ActivatedRoute, constructor(private route: ActivatedRoute,
private postService: PostService, private postService: PostService,
private commentService: CommentService) { private commentService: CommentService,
private messageService: MessageService,) {
this.route.paramMap.subscribe(params => { this.route.paramMap.subscribe(params => {
const postId = params.get('postId'); const postId = params.get('postId');
if (postId) { if (postId) {
this.subs.push( this.subs.push(
this.postService.getPost(BigInt(postId)).pipe( forkJoin({
mergeMap(post => this.commentService.list(post.id)) post: this.postService.getPost(BigInt(postId)),
).subscribe(post => { comment: this.commentService.list(BigInt(postId))
this.concernedPost = post; }).subscribe({
}) next: res => {
); this.concernedPost = res.post;
this.comments = res.comment.sort((a, b) => {
if (a.commentDate < b.commentDate) {
return -1
} else if (a.commentDate > b.commentDate) {
return 1
}
return 0
});
},
error: err => this.failureMessage("Erreur", err.message)
}));
} }
}); });
} }
failureMessage(summary: string, detail: string): void {
this.messageService.add({
severity: 'error',
summary: summary,
detail: detail,
life: 3000,
closable: false
});
}
} }

View File

@ -1,6 +1,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http'; import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
import {Comment} from '../models/comment';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'