From f13541ccab26dc760b944d6d04efb05e6cbd6500 Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Tue, 14 Apr 2026 13:46:17 +0100 Subject: [PATCH] Fixed refresh error with social page --- .../friend-request.component.ts | 35 +-------------- .../friends/friends-list.component.ts | 31 +------------ src/app/pages/social/social.component.ts | 34 ++++++++++++-- src/app/services/friends-state.ts | 45 ++++++++++++++----- 4 files changed, 69 insertions(+), 76 deletions(-) diff --git a/src/app/components/friend-request/friend-request.component.ts b/src/app/components/friend-request/friend-request.component.ts index fa7ff1f..c016668 100644 --- a/src/app/components/friend-request/friend-request.component.ts +++ b/src/app/components/friend-request/friend-request.component.ts @@ -1,4 +1,4 @@ -import {Component, inject, OnInit} from '@angular/core'; +import {Component, inject} from '@angular/core'; import {IonicModule, LoadingController, ToastController} from "@ionic/angular"; import {checkmarkCircleOutline, closeCircleOutline} from 'ionicons/icons'; import {addIcons} from "ionicons"; @@ -21,7 +21,7 @@ addIcons({ PipeComponent ] }) -export class FriendRequestComponent implements OnInit { +export class FriendRequestComponent { private friendsService = inject(FriendsService); private toastCtrl = inject(ToastController); private friendsState = inject(FriendsStateService); @@ -29,37 +29,6 @@ export class FriendRequestComponent implements OnInit { friendsRequest = this.friendsState.requests; - async ngOnInit() { - await this.fetchFriendsRequest(); - } - - async ionViewWillEnter() { - await this.fetchFriendsRequest(); - } - - - async fetchFriendsRequest() { - const loading = await this.loadCtrl.create({ - message: 'Chargement...', - spinner: 'lines-sharp-small' - }); - await loading.present(); - - try { - const requests = await firstValueFrom(this.friendsService.getAllFriendRequestsEndpoint()); - this.friendsState.setRequests(requests); - } catch (e) { - const toast = await this.toastCtrl.create({ - message: 'Erreur lors du chargement des demandes d\'amis', - duration: 2000, - color: 'primary' - }); - await toast.present(); - } - - await loading.dismiss(); - } - async acceptRequest(request: GetFriendRequestDto) { const loading = await this.loadCtrl.create({ message: 'Chargement...', diff --git a/src/app/components/friends/friends-list.component.ts b/src/app/components/friends/friends-list.component.ts index befc069..783e9e6 100644 --- a/src/app/components/friends/friends-list.component.ts +++ b/src/app/components/friends/friends-list.component.ts @@ -18,7 +18,7 @@ addIcons({ IonicModule ] }) -export class FriendsListComponent implements OnInit { +export class FriendsListComponent { private friendsService = inject(FriendsService); private usersService = inject(UsersService); private toastCtrl = inject(ToastController); @@ -31,35 +31,6 @@ export class FriendsListComponent implements OnInit { friends = this.friendsState.friends; - async ngOnInit() { - await this.fetchFriends(); - } - - async ionViewWillEnter() { - await this.fetchFriends(); - } - - async fetchFriends() { - const loading = await this.loadCtrl.create({ - message: 'Chargement...', - spinner: 'lines-sharp-small' - }); - await loading.present(); - - try { - const friends = await firstValueFrom(this.friendsService.getAllFriendsEndpoint()); - this.friendsState.setFriends(friends); - } catch (e) { - const toast = await this.toastCtrl.create({ - message: 'Amis introuvables', - duration: 2000, - color: 'primary' - }); - await toast.present(); - } - await loading.dismiss(); - } - async deleteFriend(friendId: number) { const loading = await this.loadCtrl.create({ message: 'Chargement...', diff --git a/src/app/pages/social/social.component.ts b/src/app/pages/social/social.component.ts index d4c4ff5..22aa688 100644 --- a/src/app/pages/social/social.component.ts +++ b/src/app/pages/social/social.component.ts @@ -1,11 +1,12 @@ -import {Component} from '@angular/core'; +import {Component, inject, OnInit} from '@angular/core'; import {TitlePartComponent} from "../../components/title-part/title-part.component"; -import {IonicModule} from "@ionic/angular"; +import {IonicModule, LoadingController} from "@ionic/angular"; import {FriendRequestComponent} from "../../components/friend-request/friend-request.component"; import {FriendsListComponent} from "../../components/friends/friends-list.component"; import {addIcons} from "ionicons"; import {addCircleOutline} from 'ionicons/icons'; import {SearchFriendComponent} from "../../components/search-friend/search-friend.component"; +import {FriendsStateService} from "../../services/friends-state"; addIcons({ "send": addCircleOutline @@ -23,6 +24,33 @@ addIcons({ SearchFriendComponent ] }) -export class SocialComponent { +export class SocialComponent implements OnInit { + private friendsState = inject(FriendsStateService); + private loadCtrl = inject(LoadingController); + async ngOnInit() { + const loading = await this.loadCtrl.create({ + message: 'Chargement...', + spinner: 'lines-sharp-small' + }); + await loading.present(); + + await this.friendsState.fetchFriendsRequest(); + await this.friendsState.fetchFriends(); + + await loading.dismiss(); + } + + async ionViewWillEnter() { + const loading = await this.loadCtrl.create({ + message: 'Chargement...', + spinner: 'lines-sharp-small' + }); + await loading.present(); + + await this.friendsState.fetchFriendsRequest(); + await this.friendsState.fetchFriends(); + + await loading.dismiss(); + } } \ No newline at end of file diff --git a/src/app/services/friends-state.ts b/src/app/services/friends-state.ts index 13a659a..fb06717 100644 --- a/src/app/services/friends-state.ts +++ b/src/app/services/friends-state.ts @@ -1,20 +1,45 @@ -import {Injectable, signal} from '@angular/core'; -import {GetFriendDto, GetFriendRequestDto} from "./api"; +import {inject, Injectable, signal} from '@angular/core'; +import {FriendsService, GetFriendDto, GetFriendRequestDto} from "./api"; +import {LoadingController, ToastController} from "@ionic/angular"; +import {firstValueFrom} from "rxjs"; @Injectable({ providedIn: 'root' }) export class FriendsStateService { + private friendsService = inject(FriendsService); + private toastCtrl = inject(ToastController); + friends = signal([]); requests = signal([]); - setFriends(friends: GetFriendDto[]) { - this.friends.set(friends); + async fetchFriendsRequest() { + try { + const requests = await firstValueFrom(this.friendsService.getAllFriendRequestsEndpoint()); + this.requests.set(requests); + } catch { + const toast = await this.toastCtrl.create({ + message: 'Erreur lors du chargement des demandes d\'amis', + duration: 2000, + color: 'primary' + }); + await toast.present(); + } } - setRequests(requests: GetFriendRequestDto[]) { - this.requests.set(requests); + async fetchFriends() { + try { + const friends = await firstValueFrom(this.friendsService.getAllFriendsEndpoint()); + this.friends.set(friends); + } catch { + const toast = await this.toastCtrl.create({ + message: 'Amis introuvables', + duration: 2000, + color: 'primary' + }); + await toast.present(); + } } acceptRequest(request: GetFriendRequestDto) { @@ -30,11 +55,11 @@ export class FriendsStateService { ]); } - removeFriend(friendId: number) { - this.friends.set(this.friends().filter(x => x.friendId !== friendId)); - } - removeRequest(friendId: number) { this.requests.set(this.requests().filter(x => x.userId !== friendId)); } + + removeFriend(friendId: number) { + this.friends.set(this.friends().filter(x => x.friendId !== friendId)); + } } \ No newline at end of file