|
|
@ -1,10 +1,10 @@
|
|
|
|
import { CommonModule } from '@angular/common';
|
|
|
|
import { CommonModule } from '@angular/common';
|
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
|
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 { 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 { LocalStorageService } from '../../services/local-storage/local-storage.service';
|
|
|
|
import { debounceTime, distinctUntilChanged, Subject } from 'rxjs';
|
|
|
|
import { UserService } from '../../services/user/user.service';
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
@Component({
|
|
|
|
selector: 'app-friend-page',
|
|
|
|
selector: 'app-friend-page',
|
|
|
@ -20,42 +20,39 @@ export class FriendPageComponent implements OnInit {
|
|
|
|
}[] = [];
|
|
|
|
}[] = [];
|
|
|
|
|
|
|
|
|
|
|
|
protected listUser: {
|
|
|
|
protected listUser: {
|
|
|
|
uid: string,
|
|
|
|
uid: string;
|
|
|
|
username: string
|
|
|
|
username: string;
|
|
|
|
}[] = [];
|
|
|
|
}[] = [];
|
|
|
|
|
|
|
|
|
|
|
|
userId: string = '';
|
|
|
|
userId: string = '';
|
|
|
|
status: string = '';
|
|
|
|
status: string = '';
|
|
|
|
isFriendModalOpen: boolean = false;
|
|
|
|
isFriendModalOpen: boolean = false;
|
|
|
|
hasAcceptedFriends: boolean = false;
|
|
|
|
hasAcceptedFriends: boolean = false;
|
|
|
|
hasPendingFriends: boolean = false;;
|
|
|
|
hasPendingFriends: boolean = false;
|
|
|
|
searchTerm: string = '';
|
|
|
|
searchTerm: string = '';
|
|
|
|
searchTermChanged = new Subject<string>();
|
|
|
|
searchTermChanged = new Subject<string>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
constructor(private friendService: FriendsService, private userService:UserService, private localStorage: LocalStorageService) {
|
|
|
|
private friendService: FriendsService,
|
|
|
|
|
|
|
|
private userService: UserService,
|
|
|
|
}
|
|
|
|
private localStorage: LocalStorageService
|
|
|
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.getFriendData();
|
|
|
|
this.getFriendData();
|
|
|
|
this.searchTermChanged
|
|
|
|
this.searchTermChanged
|
|
|
|
.pipe(
|
|
|
|
.pipe(debounceTime(200), distinctUntilChanged())
|
|
|
|
debounceTime(200),
|
|
|
|
.subscribe((username: string) => {
|
|
|
|
distinctUntilChanged()
|
|
|
|
this.searchUser(username);
|
|
|
|
)
|
|
|
|
});
|
|
|
|
.subscribe((username: string) => {
|
|
|
|
|
|
|
|
this.searchUser(username);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected searchUser(username: string) {
|
|
|
|
protected searchUser(username: string) {
|
|
|
|
this.searchTerm = username
|
|
|
|
this.searchTerm = username;
|
|
|
|
if (this.searchTerm) {
|
|
|
|
if (this.searchTerm) {
|
|
|
|
this.getUserData(this.searchTerm.trim())
|
|
|
|
this.getUserData(this.searchTerm.trim());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
else {
|
|
|
|
this.listUser = [];
|
|
|
|
this.listUser = []
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -63,27 +60,38 @@ export class FriendPageComponent implements OnInit {
|
|
|
|
this.searchTermChanged.next(username);
|
|
|
|
this.searchTermChanged.next(username);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private getUserData(search:string): void{
|
|
|
|
private getUserData(search: string): void {
|
|
|
|
const username = this.localStorage.getUsername()
|
|
|
|
const username = this.localStorage.getUsername();
|
|
|
|
this.userService.getUser('^(?!'+username+')'+search).subscribe((data:any[]) => {
|
|
|
|
this.userService
|
|
|
|
if (data.length > 0) {
|
|
|
|
.getUser('^(?!' + username + ')' + search)
|
|
|
|
const existingFriendIds = this.listFriend.map(friend => friend.friend_user_id);
|
|
|
|
.subscribe((data: any[]) => {
|
|
|
|
this.listUser = data.filter(user => !existingFriendIds.includes(user.uid));
|
|
|
|
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 {
|
|
|
|
protected addUser(user_id: string): void {
|
|
|
|
this.friendService.addFriend(user_id).subscribe((data:any) => {
|
|
|
|
this.friendService.addFriend(user_id).subscribe((data: any) => {
|
|
|
|
if(data.id){
|
|
|
|
if (data.id) {
|
|
|
|
const add_user = this.listUser.find(x => x.uid == user_id)
|
|
|
|
const add_user = this.listUser.find((x) => x.uid == user_id);
|
|
|
|
if (add_user){
|
|
|
|
if (add_user) {
|
|
|
|
this.listFriend.push({username:add_user.username, status:"pending", friend_user_id:add_user.uid, id:data.id})
|
|
|
|
this.listFriend.push({
|
|
|
|
|
|
|
|
username: add_user.username,
|
|
|
|
|
|
|
|
status: 'pending',
|
|
|
|
|
|
|
|
friend_user_id: add_user.uid,
|
|
|
|
|
|
|
|
id: data.id,
|
|
|
|
|
|
|
|
});
|
|
|
|
this.searchTerm = '';
|
|
|
|
this.searchTerm = '';
|
|
|
|
this.listUser = [];
|
|
|
|
this.listUser = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private getFriendData(): void {
|
|
|
|
private getFriendData(): void {
|
|
|
@ -143,27 +151,35 @@ export class FriendPageComponent implements OnInit {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
deleteFriend(id: string) {
|
|
|
|
deleteFriend(id: string) {
|
|
|
|
this.friendService.deleteFriend(id).subscribe((data :any) => {
|
|
|
|
this.friendService.deleteFriend(id).subscribe((data: any) => {
|
|
|
|
if (data.message == 'Friend deleted') {
|
|
|
|
if (data.message == 'Friend deleted') {
|
|
|
|
this.listFriend.forEach((friend, index) => {
|
|
|
|
this.listFriend.forEach((friend, index) => {
|
|
|
|
if(friend.id == id) {
|
|
|
|
if (friend.id == id) {
|
|
|
|
this.listFriend.splice(index, 1)
|
|
|
|
this.listFriend.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
hasNoAcceptedFriends(): boolean {
|
|
|
|
hasNoAcceptedFriends(): boolean {
|
|
|
|
return this.listFriend.filter(friend => friend.status === 'accepted').length === 0;
|
|
|
|
return (
|
|
|
|
|
|
|
|
this.listFriend.filter((friend) => friend.status === 'accepted')
|
|
|
|
|
|
|
|
.length === 0
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
hasPendingApprovalFriend(): boolean {
|
|
|
|
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 {
|
|
|
|
hasPendingFriend(): boolean {
|
|
|
|
return this.listFriend.filter(friend => friend.status === 'pending').length !== 0;
|
|
|
|
return (
|
|
|
|
|
|
|
|
this.listFriend.filter((friend) => friend.status === 'pending').length !==
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|