Added posts and possibility to like post

This commit is contained in:
2026-05-14 13:55:00 +01:00
parent d5584ebf0a
commit eee7958bce
4 changed files with 101 additions and 6 deletions
+43 -3
View File
@@ -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<GetFriendDto[]>([]);
posts = input.required<GetPostDto[]>();
localPosts = signal<GetPostDto[]>([]);
postsFriends = signal<GetPostDto[]>([]);
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();
}
}