Files
BeReadyFrontend/src/app/components/friends/friends-list.component.ts
T

79 lines
2.6 KiB
TypeScript

import {Component, inject, OnInit, signal} from '@angular/core';
import {IonicModule, LoadingController, ToastController} from "@ionic/angular";
import {closeCircleOutline} from 'ionicons/icons';
import {addIcons} from "ionicons";
import {FriendsService, GetUserDto, UsersService} from "../../services/api";
import {firstValueFrom} from "rxjs";
import {FriendsStateService} from "../../services/friends-state";
import {GenericUserInfoComponent} from "../generic-user-info/generic-user-info.component";
addIcons({
'close': closeCircleOutline,
});
@Component({
selector: 'app-friends-list',
templateUrl: './friends-list.component.html',
styleUrls: ['./friends-list.component.scss'],
imports: [
IonicModule,
GenericUserInfoComponent
]
})
export class FriendsListComponent {
private friendsService = inject(FriendsService);
private usersService = inject(UsersService);
private toastCtrl = inject(ToastController);
private friendsState = inject(FriendsStateService);
private loadCtrl = inject(LoadingController)
selectedFriend = signal<GetUserDto>(null);
isModalOpen = false;
friends = this.friendsState.friends;
async deleteFriend(friendId: number) {
const loading = await this.loadCtrl.create({
message: 'Chargement...',
spinner: 'lines-sharp-small'
});
await loading.present();
try {
await firstValueFrom(this.friendsService.deleteFriendEndpoint(friendId));
const toast = await this.toastCtrl.create({
message: 'Vous avez supprimé cet ami',
duration: 2000,
color: 'success'
});
await toast.present();
} catch (e) {
const toast = await this.toastCtrl.create({
message: 'Vous ne pouvez pas supprimer cet ami',
duration: 2000,
color: 'danger'
});
await toast.present();
}
this.friendsState.removeFriend(friendId);
await loading.dismiss();
}
async setOpen(isOpen: boolean, userId: number) {
if (isOpen) {
try {
const userInfo = await firstValueFrom(this.usersService.getUserEndpoint(userId));
this.selectedFriend.set(userInfo);
} catch (e) {
const toast = await this.toastCtrl.create({
message: 'Impossible de charger les données du joueur',
duration: 2000,
color: 'primary'
});
await toast.present();
}
}
this.isModalOpen = isOpen;
}
}