diff --git a/src/app/components/add-pin-popup/add-pin-popup.component.ts b/src/app/components/add-pin-popup/add-pin-popup.component.ts index 4201de9..9d506f1 100644 --- a/src/app/components/add-pin-popup/add-pin-popup.component.ts +++ b/src/app/components/add-pin-popup/add-pin-popup.component.ts @@ -61,7 +61,7 @@ export class AddPinPopupComponent implements OnInit { this.form .get('location') ?.valueChanges.pipe( - debounceTime(300), // Attendre 300ms après la dernière frappe + debounceTime(200), // Attendre 200ms après la dernière frappe distinctUntilChanged(), // Ignorer si la nouvelle valeur est la même que la précédente switchMap((query) => { const trimmedQuery = query.trim(); diff --git a/src/app/components/friend-page/friend-page.component.html b/src/app/components/friend-page/friend-page.component.html index 8cccf93..c52f54b 100644 --- a/src/app/components/friend-page/friend-page.component.html +++ b/src/app/components/friend-page/friend-page.component.html @@ -56,10 +56,10 @@ />
-
-

Amis

-
-

Aucun amis

-
+

+ Aucun amis +

+
+
-
-
-
- -
- + +
+ +
diff --git a/src/app/components/friend-page/friend-page.component.ts b/src/app/components/friend-page/friend-page.component.ts index 0cbf0a0..31b4daf 100644 --- a/src/app/components/friend-page/friend-page.component.ts +++ b/src/app/components/friend-page/friend-page.component.ts @@ -1,10 +1,10 @@ import { CommonModule } from '@angular/common'; import { Component, OnInit } from '@angular/core'; +import { FormsModule } from '@angular/forms'; +import { debounceTime, distinctUntilChanged, Subject } from 'rxjs'; import { FriendsService } from '../../services/friends/friends.service'; -import { FormsModule, NgModel } from '@angular/forms'; -import { UserService } from '../../services/user/user.service'; import { LocalStorageService } from '../../services/local-storage/local-storage.service'; -import { debounceTime, distinctUntilChanged, Subject } from 'rxjs'; +import { UserService } from '../../services/user/user.service'; @Component({ selector: 'app-friend-page', @@ -20,42 +20,39 @@ export class FriendPageComponent implements OnInit { }[] = []; protected listUser: { - uid: string, - username: string + uid: string; + username: string; }[] = []; userId: string = ''; status: string = ''; isFriendModalOpen: boolean = false; hasAcceptedFriends: boolean = false; - hasPendingFriends: boolean = false;; + hasPendingFriends: boolean = false; searchTerm: string = ''; searchTermChanged = new Subject(); - - constructor(private friendService: FriendsService, private userService:UserService, private localStorage: LocalStorageService) { - - } + constructor( + private friendService: FriendsService, + private userService: UserService, + private localStorage: LocalStorageService + ) {} ngOnInit(): void { this.getFriendData(); this.searchTermChanged - .pipe( - debounceTime(200), - distinctUntilChanged() - ) - .subscribe((username: string) => { - this.searchUser(username); - }); + .pipe(debounceTime(200), distinctUntilChanged()) + .subscribe((username: string) => { + this.searchUser(username); + }); } - + protected searchUser(username: string) { - this.searchTerm = username + this.searchTerm = username; if (this.searchTerm) { - this.getUserData(this.searchTerm.trim()) - } - else { - this.listUser = [] + this.getUserData(this.searchTerm.trim()); + } else { + this.listUser = []; } } @@ -63,27 +60,38 @@ export class FriendPageComponent implements OnInit { this.searchTermChanged.next(username); } - private getUserData(search:string): void{ - const username = this.localStorage.getUsername() - this.userService.getUser('^(?!'+username+')'+search).subscribe((data:any[]) => { - if (data.length > 0) { - const existingFriendIds = this.listFriend.map(friend => friend.friend_user_id); - this.listUser = data.filter(user => !existingFriendIds.includes(user.uid)); - } - }) + private getUserData(search: string): void { + const username = this.localStorage.getUsername(); + this.userService + .getUser('^(?!' + username + ')' + search) + .subscribe((data: any[]) => { + if (data.length > 0) { + const existingFriendIds = this.listFriend.map( + (friend) => friend.friend_user_id + ); + this.listUser = data.filter( + (user) => !existingFriendIds.includes(user.uid) + ); + } + }); } - protected addUser(user_id:string): void { - this.friendService.addFriend(user_id).subscribe((data:any) => { - if(data.id){ - const add_user = this.listUser.find(x => x.uid == user_id) - if (add_user){ - this.listFriend.push({username:add_user.username, status:"pending", friend_user_id:add_user.uid, id:data.id}) + protected addUser(user_id: string): void { + this.friendService.addFriend(user_id).subscribe((data: any) => { + if (data.id) { + const add_user = this.listUser.find((x) => x.uid == user_id); + if (add_user) { + this.listFriend.push({ + username: add_user.username, + status: 'pending', + friend_user_id: add_user.uid, + id: data.id, + }); this.searchTerm = ''; this.listUser = []; } } - }) + }); } private getFriendData(): void { @@ -143,27 +151,35 @@ export class FriendPageComponent implements OnInit { } deleteFriend(id: string) { - this.friendService.deleteFriend(id).subscribe((data :any) => { + this.friendService.deleteFriend(id).subscribe((data: any) => { if (data.message == 'Friend deleted') { this.listFriend.forEach((friend, index) => { - if(friend.id == id) { - this.listFriend.splice(index, 1) + if (friend.id == id) { + this.listFriend.splice(index, 1); } - }) + }); } }); } hasNoAcceptedFriends(): boolean { - return this.listFriend.filter(friend => friend.status === 'accepted').length === 0; + return ( + this.listFriend.filter((friend) => friend.status === 'accepted') + .length === 0 + ); } hasPendingApprovalFriend(): boolean { - return this.listFriend.filter(friend => friend.status === 'pending_approval').length !== 0; + return ( + this.listFriend.filter((friend) => friend.status === 'pending_approval') + .length !== 0 + ); } hasPendingFriend(): boolean { - return this.listFriend.filter(friend => friend.status === 'pending').length !== 0; + return ( + this.listFriend.filter((friend) => friend.status === 'pending').length !== + 0 + ); } - } diff --git a/src/app/services/friends/friends.service.ts b/src/app/services/friends/friends.service.ts index c9710c9..604d236 100644 --- a/src/app/services/friends/friends.service.ts +++ b/src/app/services/friends/friends.service.ts @@ -1,18 +1,15 @@ -import { Injectable, OnInit } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { Injectable } from '@angular/core'; import { environment } from '../../../environments/environment'; @Injectable({ - providedIn: 'root' + providedIn: 'root', }) -export class FriendsService { - +export class FriendsService { private apiURL = environment.apiURL; - constructor(private http: HttpClient) { - } + constructor(private http: HttpClient) {} getFriend() { - console.log("getFriends"); const url = `${this.apiURL}/friends`; const headers = new HttpHeaders({ 'Content-Type': 'application/json', @@ -30,16 +27,16 @@ export class FriendsService { return this.http.get(url, { headers }); } - addFriend(user_id: string){ + addFriend(user_id: string) { const url = `${this.apiURL}/friend/add`; const headers = new HttpHeaders({ 'Content-Type': 'application/json', Authorization: 'Bearer ' + localStorage.getItem('auth_token'), }); - return this.http.post(url, { friend_user_id:user_id } , { headers }) + return this.http.post(url, { friend_user_id: user_id }, { headers }); } - acceptFriendById(id: string){ + acceptFriendById(id: string) { const url = `${this.apiURL}/friend/${id}/accept`; const headers = new HttpHeaders({ 'Content-Type': 'application/json', @@ -48,7 +45,7 @@ export class FriendsService { return this.http.patch(url, [], { headers }); } - denyFriendById(id: string){ + denyFriendById(id: string) { const url = `${this.apiURL}/friend/${id}/deny`; const headers = new HttpHeaders({ 'Content-Type': 'application/json', @@ -57,7 +54,7 @@ export class FriendsService { return this.http.delete(url, { headers }); } - deleteFriend(id: string){ + deleteFriend(id: string) { const url = `${this.apiURL}/friend/${id}/delete`; const headers = new HttpHeaders({ 'Content-Type': 'application/json',