From eee7958bcefbabda03fb17ebd4d84fc934e1c969 Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Thu, 14 May 2026 13:55:00 +0100 Subject: [PATCH] Added posts and possibility to like post --- src/app/components/post/post.component.html | 33 ++++++++++++- src/app/components/post/post.component.ts | 46 +++++++++++++++++-- .../publication/publication.component.html | 21 ++++++++- .../publication/publication.component.ts | 7 +++ 4 files changed, 101 insertions(+), 6 deletions(-) diff --git a/src/app/components/post/post.component.html b/src/app/components/post/post.component.html index 773e7c7..aa40640 100644 --- a/src/app/components/post/post.component.html +++ b/src/app/components/post/post.component.html @@ -1,3 +1,32 @@ -@for (post of posts(); track post.id) { - +@for (post of localPosts(); track post.id) { + +
+
+
+ {{ post.username.substring(0, 2) }} +
+ {{ post.username }} +
+ +
+ +
+ +
+
+ + {{ post.likes }} Likes +
+

{{ post.libelle }}

+
+
+
} \ No newline at end of file diff --git a/src/app/components/post/post.component.ts b/src/app/components/post/post.component.ts index 2cbc719..3da856e 100644 --- a/src/app/components/post/post.component.ts +++ b/src/app/components/post/post.component.ts @@ -1,14 +1,25 @@ import {Component, inject, input, OnInit, signal} from '@angular/core'; -import {FriendsService, GetFriendDto, GetPostDto} from "../../services/api"; +import {FriendsService, GetFriendDto, GetPostDto, PostsService} from "../../services/api"; import {firstValueFrom} from "rxjs"; -import {LoadingController, ToastController} from "@ionic/angular"; +import {IonicModule, LoadingController, ToastController} from "@ionic/angular"; +import {heart, heartOutline} from 'ionicons/icons'; +import {addIcons} from "ionicons"; + +addIcons({ + "heart": heart, + "heartOutline": heartOutline, +}); @Component({ selector: 'app-post', templateUrl: './post.component.html', styleUrls: ['./post.component.scss'], + imports: [ + IonicModule + ] }) export class PostComponent implements OnInit { + private postsService = inject(PostsService); private friendsService = inject(FriendsService); private loadCtrl = inject(LoadingController); private toastCtrl = inject(ToastController); @@ -16,6 +27,7 @@ export class PostComponent implements OnInit { friends = signal([]); posts = input.required(); + localPosts = signal([]); postsFriends = signal([]); async ngOnInit() { @@ -25,10 +37,12 @@ export class PostComponent implements OnInit { }); await loading.present(); + this.localPosts.set(this.posts()); + try { const friends = await firstValueFrom(this.friendsService.getAllFriendsEndpoint()); this.friends.set(friends); - this.postsFriends.set(this.posts().filter(post => this.friends().map(x => x.friendId).includes(post.userId))); + this.postsFriends.set(this.localPosts().filter(post => this.friends().map(x => x.friendId).includes(post.userId))); } catch { const toast = await this.toastCtrl.create({ message: 'Erreur lors du chargement des amis.', @@ -39,4 +53,30 @@ export class PostComponent implements OnInit { } await loading.dismiss(); } + + async likePost(postId: number) { + this.localPosts.update(x => x.map(x => x.id != postId ? x :{ + ...x, + likes: x.isLiked ? x.likes - 1 : x.likes + 1, + isLiked: !x.isLiked + })); + + const loading = await this.loadCtrl.create({ + message: 'Chargement...', + spinner: 'lines-sharp-small' + }); + await loading.present(); + + try { + await firstValueFrom(this.postsService.patchLikeEndpoint(postId)); + } catch { + const toast = await this.toastCtrl.create({ + message: 'Impossible d\'aimer ce contenu', + duration: 2000, + color: 'danger' + }); + await toast.present(); + } + await loading.dismiss(); + } } diff --git a/src/app/pages/publication/publication.component.html b/src/app/pages/publication/publication.component.html index c06b6ad..b80852a 100644 --- a/src/app/pages/publication/publication.component.html +++ b/src/app/pages/publication/publication.component.html @@ -5,5 +5,24 @@ - + @if (posts().length) { + + } @else { + +
+
+
+ +
+
+ +
+
+
+

C'est bien vide par ici

+

Les challengers dorment encore

+
+
+
+ }
diff --git a/src/app/pages/publication/publication.component.ts b/src/app/pages/publication/publication.component.ts index 7a74d0f..3a11184 100644 --- a/src/app/pages/publication/publication.component.ts +++ b/src/app/pages/publication/publication.component.ts @@ -3,6 +3,13 @@ import {IonicModule, LoadingController, ToastController} from "@ionic/angular"; import {GetPostDto, PostsService} from "../../services/api"; import {firstValueFrom} from "rxjs"; import {PostComponent} from "../../components/post/post.component"; +import {planet, moonOutline} from 'ionicons/icons'; +import {addIcons} from "ionicons"; + +addIcons({ + "planet": planet, + "moon-outline": moonOutline, +}) @Component({ selector: 'app-publication',