🗑️ Cleanup

tutorial
Alexis Feron 3 weeks ago
parent 685fbf1103
commit ab8d48234c

@ -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();

@ -56,10 +56,10 @@
/>
<div *ngIf="listUser" class="text-gray-500 text-sm">
<div
style="padding-top: 10px;"
*ngFor="let user of listUser"
class="friend flex items-center justify-between space-x-3"
<div
style="padding-top: 10px"
*ngFor="let user of listUser"
class="friend flex items-center justify-between space-x-3"
>
<div class="friend flex items-center space-x-3">
<img
@ -72,26 +72,40 @@
}}</span>
</div>
<button
(click)="addUser(user.uid)"
class="p-2 bg-green-500 text-white rounded-full"
(click)="addUser(user.uid)"
class="p-2 bg-green-500 text-white rounded-full"
>
<svg class="w-6 h-6 text-gray-800 dark:text-white" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" viewBox="0 0 24 24">
<path fill-rule="evenodd" d="M9 4a4 4 0 1 0 0 8 4 4 0 0 0 0-8Zm-2 9a4 4 0 0 0-4 4v1a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2v-1a4 4 0 0 0-4-4H7Zm8-1a1 1 0 0 1 1-1h1v-1a1 1 0 1 1 2 0v1h1a1 1 0 1 1 0 2h-1v1a1 1 0 1 1-2 0v-1h-1a1 1 0 0 1-1-1Z" clip-rule="evenodd"/>
</svg>
<svg
class="w-6 h-6 text-gray-800 dark:text-white"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
fill="currentColor"
viewBox="0 0 24 24"
>
<path
fill-rule="evenodd"
d="M9 4a4 4 0 1 0 0 8 4 4 0 0 0 0-8Zm-2 9a4 4 0 0 0-4 4v1a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2v-1a4 4 0 0 0-4-4H7Zm8-1a1 1 0 0 1 1-1h1v-1a1 1 0 1 1 2 0v1h1a1 1 0 1 1 0 2h-1v1a1 1 0 1 1-2 0v-1h-1a1 1 0 0 1-1-1Z"
clip-rule="evenodd"
/>
</svg>
</button>
</div>
</div>
</div>
<div class="p-4 space-y-3">
<p class="dark:text-white">Amis</p>
<div *ngIf="hasNoAcceptedFriends()" class="text-gray-500 dark:text-gray-300">
<p><small><em>Aucun amis</em></small></p>
</div>
<div
*ngFor="let friend of listFriend"
*ngIf="hasNoAcceptedFriends()"
class="text-gray-500 dark:text-gray-300"
>
<p>
<small><em>Aucun amis</em></small>
</p>
</div>
<div *ngFor="let friend of listFriend">
<div
*ngIf="friend.status == 'accepted'"
class="flex justify-between items-center w-full space-x-2"
@ -175,7 +189,7 @@
class="w-4 h-4"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"private
stroke="currentColor"
>
<path
stroke-linecap="round"
@ -197,7 +211,6 @@
*ngIf="friend.status == 'pending'"
class="flex justify-between items-center w-full"
>
<div class="friend flex items-center space-x-3">
<img
class="w-10 h-10 rounded-full"
@ -208,21 +221,35 @@
friend.username
}}</span>
</div>
<svg class="w-6 h-6 text-gray-800 dark:text-white" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M18.5 4h-13m13 16h-13M8 20v-3.333a2 2 0 0 1 .4-1.2L10 12.6a1 1 0 0 0 0-1.2L8.4 8.533a2 2 0 0 1-.4-1.2V4h8v3.333a2 2 0 0 1-.4 1.2L13.957 11.4a1 1 0 0 0 0 1.2l1.643 2.867a2 2 0 0 1 .4 1.2V20H8Z"/>
<svg
class="w-6 h-6 text-gray-800 dark:text-white"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
fill="none"
viewBox="0 0 24 24"
>
<path
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M18.5 4h-13m13 16h-13M8 20v-3.333a2 2 0 0 1 .4-1.2L10 12.6a1 1 0 0 0 0-1.2L8.4 8.533a2 2 0 0 1-.4-1.2V4h8v3.333a2 2 0 0 1-.4 1.2L13.957 11.4a1 1 0 0 0 0 1.2l1.643 2.867a2 2 0 0 1 .4 1.2V20H8Z"
/>
</svg>
</div>
</div>
</div>
<!-- Pied du modal -->
<div class="flex justify-end p-4 border-t dark:border-gray-700">
<button
(click)="closeFriendModal()"
class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600"
>
Fermer
</button>
<!-- Pied du modal -->
<div class="flex justify-end p-4 border-t dark:border-gray-700">
<button
(click)="closeFriendModal()"
class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600"
>
Fermer
</button>
</div>
</div>
</div>
</div>

@ -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<string>();
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
);
}
}

@ -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<any>(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<any>(url, { friend_user_id:user_id } , { headers })
return this.http.post<any>(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<any>(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<any>(url, { headers });
}
deleteFriend(id: string){
deleteFriend(id: string) {
const url = `${this.apiURL}/friend/${id}/delete`;
const headers = new HttpHeaders({
'Content-Type': 'application/json',

Loading…
Cancel
Save