86 lines
2.8 KiB
TypeScript
86 lines
2.8 KiB
TypeScript
import {Component, inject} from '@angular/core';
|
|
import {IonicModule, LoadingController, ToastController} from "@ionic/angular";
|
|
import {checkmarkCircleOutline, closeCircleOutline} from 'ionicons/icons';
|
|
import {addIcons} from "ionicons";
|
|
import {PipeComponent} from "../pipe/pipe.component";
|
|
import {FriendsService, GetFriendRequestDto} from "../../services/api";
|
|
import {firstValueFrom} from "rxjs";
|
|
import {FriendsStateService} from "../../services/friends-state";
|
|
|
|
addIcons({
|
|
'check': checkmarkCircleOutline,
|
|
'close': closeCircleOutline
|
|
});
|
|
|
|
@Component({
|
|
selector: 'app-friend-request',
|
|
templateUrl: './friend-request.component.html',
|
|
styleUrls: ['./friend-request.component.scss'],
|
|
imports: [
|
|
IonicModule,
|
|
PipeComponent
|
|
]
|
|
})
|
|
export class FriendRequestComponent {
|
|
private friendsService = inject(FriendsService);
|
|
private toastCtrl = inject(ToastController);
|
|
private friendsState = inject(FriendsStateService);
|
|
private loadCtrl = inject(LoadingController)
|
|
|
|
friendsRequest = this.friendsState.requests;
|
|
|
|
async acceptRequest(request: GetFriendRequestDto) {
|
|
const loading = await this.loadCtrl.create({
|
|
message: 'Chargement...',
|
|
spinner: 'lines-sharp-small'
|
|
});
|
|
await loading.present();
|
|
|
|
try {
|
|
await firstValueFrom(this.friendsService.acceptFriendRequestEndpoint(request.userId));
|
|
this.friendsState.acceptRequest(request);
|
|
const toast = await this.toastCtrl.create({
|
|
message: 'Demande d\'ami acceptée',
|
|
duration: 1000,
|
|
color: 'success'
|
|
});
|
|
await toast.present();
|
|
} catch (e) {
|
|
const toast = await this.toastCtrl.create({
|
|
message: 'Echec de l\'acceptation',
|
|
duration: 1000,
|
|
color: 'danger'
|
|
});
|
|
await toast.present();
|
|
}
|
|
await loading.dismiss();
|
|
}
|
|
|
|
async rejectRequest(id: number) {
|
|
const loading = await this.loadCtrl.create({
|
|
message: 'Chargement...',
|
|
spinner: 'lines-sharp-small'
|
|
});
|
|
await loading.present();
|
|
|
|
try {
|
|
await firstValueFrom(this.friendsService.rejectFriendRequestEndpoint(id));
|
|
this.friendsState.removeRequest(id);
|
|
const toast = await this.toastCtrl.create({
|
|
message: 'Demande d\'ami refusée',
|
|
duration: 1000,
|
|
color: 'success'
|
|
});
|
|
await toast.present();
|
|
} catch (e) {
|
|
const toast = await this.toastCtrl.create({
|
|
message: 'Echec du refus',
|
|
duration: 1000,
|
|
color: 'danger'
|
|
});
|
|
await toast.present();
|
|
}
|
|
await loading.dismiss();
|
|
}
|
|
}
|