Files
BeReadyFrontend/src/app/components/post/post.component.ts
T

83 lines
2.6 KiB
TypeScript

import {Component, inject, input, OnInit, signal} from '@angular/core';
import {FriendsService, GetFriendDto, GetPostDto, PostsService} from "../../services/api";
import {firstValueFrom} from "rxjs";
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);
friends = signal<GetFriendDto[]>([]);
posts = input.required<GetPostDto[]>();
localPosts = signal<GetPostDto[]>([]);
postsFriends = signal<GetPostDto[]>([]);
async ngOnInit() {
const loading = await this.loadCtrl.create({
message: 'Chargement...',
spinner: 'lines-sharp-small'
});
await loading.present();
this.localPosts.set(this.posts());
try {
const friends = await firstValueFrom(this.friendsService.getAllFriendsEndpoint());
this.friends.set(friends);
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.',
duration: 2000,
color: 'danger'
});
await toast.present();
}
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();
}
}