Affichage d'un post individuellement

This commit is contained in:
Guams 2024-12-26 23:33:30 +01:00
parent 54e69e3440
commit a7fed73ee5
11 changed files with 75 additions and 9 deletions

View File

@ -8,6 +8,7 @@ import {authGuard} from './guards/auth.guard';
import {ProfileComponent} from './profile/profile.component'; import {ProfileComponent} from './profile/profile.component';
import {NewPostComponent} from './new-post/new-post.component'; import {NewPostComponent} from './new-post/new-post.component';
import {writerGuard} from './guards/writer.guard'; import {writerGuard} from './guards/writer.guard';
import {PostComponent} from './post/post.component';
export const routes: Routes = [ export const routes: Routes = [
{path: '', component: HomeComponent}, {path: '', component: HomeComponent},
@ -15,6 +16,7 @@ export const routes: Routes = [
{path: 'register', component: RegisterComponent, canActivate: [authGuard]}, {path: 'register', component: RegisterComponent, canActivate: [authGuard]},
{path: 'logout', component: LogoutComponent}, {path: 'logout', component: LogoutComponent},
{path: 'profile/:authorId', component: ProfileComponent}, {path: 'profile/:authorId', component: ProfileComponent},
{path: 'post/:postId', component: PostComponent},
{path: 'new-post', component: NewPostComponent, canActivate: [writerGuard]}, {path: 'new-post', component: NewPostComponent, canActivate: [writerGuard]},
{path: '**', component: NotFoundComponent} {path: '**', component: NotFoundComponent}
]; ];

View File

@ -2,7 +2,8 @@
<div class="main-content"> <div class="main-content">
@for (post of posts; track post.id) { @for (post of posts; track post.id) {
<app-post-home [illustration]="post.illustration" <app-post-home [illustration]="post.illustration"
[date]="post.publication_date" [date]="post.publicationDate"
[postId]="post.id"
[category]="post.category" [category]="post.category"
[title]="post.title" [title]="post.title"
[username]="'Guams'" [username]="'Guams'"

View File

@ -1,10 +1,10 @@
<app-header></app-header> <app-header></app-header>
<label for="username">Nom d'utilisateur</label> <label for="username">Nom d'utilisateur</label>
<input id="username" placeholder="Jean Zay" pInputText [(ngModel)]="name"/> <input type="text" id="username" placeholder="Jean Zay" pInputText [(ngModel)]="name"/>
<label for="password">Mot de passe</label> <label for="password">Mot de passe</label>
<input id="password" placeholder="motDePasseTrèsSecret" pInputText [(ngModel)]="password"/> <input type="password" id="password" placeholder="motDePasseTrèsSecret" pInputText [(ngModel)]="password"/>
<label for="confirm-password">Confirmez le mot de passe</label> <label for="confirm-password">Confirmez le mot de passe</label>
<input id="confirm-password" placeholder="motDePasseTrèsSecret" pInputText [(ngModel)]="confirmPassword" (keyup.enter)="sendLogins()" /> <input type="password" id="confirm-password" placeholder="motDePasseTrèsSecret" pInputText [(ngModel)]="confirmPassword" (keyup.enter)="sendLogins()" />
<p-button <p-button
label="Se connecter" label="Se connecter"
icon="pi pi-check" icon="pi pi-check"

View File

@ -5,5 +5,5 @@ export interface Post {
title: string title: string
body: string body: string
category: string category: string
publication_date: Date publicationDate: Date
} }

View File

@ -0,0 +1,8 @@
img {
border-radius: 5px;
object-fit: cover;
width: 100%;
height: 300px;
display: block;
margin: auto;
}

View File

@ -2,7 +2,7 @@
<img src="data:image/jpeg;base64,{{ illustration }}"/> <img src="data:image/jpeg;base64,{{ illustration }}"/>
<h2>{{ title }}</h2> <h2>{{ title }}</h2>
<p>{{ category }}</p> <p>{{ category }}</p>
<em>{{ date }}</em> <p>{{ date | date : "dd/MM/yyyy" }}</p>
<p>{{ description }}</p> <p>{{ description }}</p>
<p-button label="Lire la suite"/> <p-button routerLink="post/{{ postId }}" label="Lire la suite"/>
</p-card> </p-card>

View File

@ -1,24 +1,30 @@
import {Component, Input} from '@angular/core'; import {Component, Input} from '@angular/core';
import {Button} from 'primeng/button'; import {Button} from 'primeng/button';
import {CardModule} from 'primeng/card'; import {CardModule} from 'primeng/card';
import {DatePipe} from '@angular/common';
import {RouterLink} from '@angular/router';
@Component({ @Component({
selector: 'app-post-home', selector: 'app-post-home',
standalone: true, standalone: true,
imports: [ imports: [
Button, Button,
CardModule CardModule,
DatePipe,
RouterLink
], ],
templateUrl: './post-home.component.html', templateUrl: './post-home.component.html',
styleUrl: './post-home.component.css' styleUrl: './post-home.component.css'
}) })
export class PostHomeComponent { export class PostHomeComponent {
@Input() postId: bigint = BigInt(0);
@Input() title: string = ''; @Input() title: string = '';
@Input() illustration: string = ''; @Input() illustration: string = '';
@Input() category: string = ''; @Input() category: string = '';
@Input() date: Date = new Date(); @Input() date: Date | undefined;
@Input() description: string = ''; @Input() description: string = '';
@Input() username: string = ''; @Input() username: string = '';
@Input() avatar: string = ''; @Input() avatar: string = '';
constructor() {}
} }

View File

View File

@ -0,0 +1,4 @@
<app-header></app-header>
<h1>{{ concernedPost?.title }}</h1>
<em>{{ concernedPost?.publicationDate | date: "dd/MM/yyyy" }}</em>
<div [innerHTML]="concernedPost?.body"></div>

View File

@ -0,0 +1,41 @@
import { Component } from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {AuthorService} from '../services/author.service';
import {CookieService} from 'ngx-cookie-service';
import {Subscription} from 'rxjs';
import {Post} from '../models/post';
import {PostService} from '../services/post.service';
import {HeaderComponent} from '../header/header.component';
import {DatePipe, JsonPipe} from '@angular/common';
@Component({
selector: 'app-post',
standalone: true,
imports: [
HeaderComponent,
JsonPipe,
DatePipe
],
templateUrl: './post.component.html',
styleUrl: './post.component.css'
})
export class PostComponent {
subs: Subscription[] = [];
concernedPost: Post | undefined;
constructor(private route: ActivatedRoute,
private postService: PostService) {
this.route.paramMap.subscribe(params => {
const postId = params.get('postId');
if (postId) {
this.subs.push(
this.postService.getPost(BigInt(postId)).subscribe(post => {
this.concernedPost = post;
})
);
}
});
}
}

View File

@ -14,6 +14,10 @@ export class PostService {
return this.httpClient.get<Post[]>(this.url); return this.httpClient.get<Post[]>(this.url);
} }
getPost(id: bigint) {
return this.httpClient.get<Post>(`${this.url}/${id}`);
}
createPost(post: any, token: string | undefined): Observable<Post> { createPost(post: any, token: string | undefined): Observable<Post> {
const httpOptions = { const httpOptions = {
headers: new HttpHeaders({ headers: new HttpHeaders({