From 906cff5f244591e3b29ef477cb8a13194618953a Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 22 Jan 2020 18:20:52 +0100 Subject: [PATCH] Add remove friend functionality --- .../components/profile/profile.component.html | 12 +++++++--- .../components/profile/profile.component.ts | 23 ++++++++++++++++++- .../social/friends/friends.component.html | 21 ++++++++++++----- .../social/friends/friends.component.sass | 4 ++++ .../social/friends/friends.component.ts | 7 +++++- src/app/services/datasharing.service.ts | 13 ++++++++++- src/app/services/social/social.service.ts | 21 +++++++++++++++-- 7 files changed, 87 insertions(+), 14 deletions(-) diff --git a/src/app/components/profile/profile.component.html b/src/app/components/profile/profile.component.html index e427abc..7fd86d6 100644 --- a/src/app/components/profile/profile.component.html +++ b/src/app/components/profile/profile.component.html @@ -53,12 +53,18 @@ {{userProfile.username}} @{{userProfile.handle}} - +
diff --git a/src/app/components/profile/profile.component.ts b/src/app/components/profile/profile.component.ts index 815ba80..81bb20b 100644 --- a/src/app/components/profile/profile.component.ts +++ b/src/app/components/profile/profile.component.ts @@ -6,6 +6,7 @@ import {RequestService} from 'src/app/services/request/request.service'; import {DatasharingService} from '../../services/datasharing.service'; import {ProfileService} from 'src/app/services/profile/profile.service'; import {HttpClient} from '@angular/common/http'; +import {SocialService} from 'src/app/services/social/social.service'; import {SelfService} from '../../services/selfservice/self.service'; import {MatDialog} from '@angular/material'; import {DialogFileUploadComponent} from './fileUpload/fileUpload.component'; @@ -24,7 +25,7 @@ export class ProfileComponent implements OnInit { id: string; rankname: string; profileNotFound = false; - + isFriendOfSelf = false; loading = false; constructor( @@ -33,6 +34,7 @@ export class ProfileComponent implements OnInit { private requestService: RequestService, private data: DatasharingService, private profileService: ProfileService, + private socialService: SocialService, private lightbox: Lightbox, public dialog: MatDialog) { router.events.forEach((event) => { @@ -52,6 +54,7 @@ export class ProfileComponent implements OnInit { this.id = this.router.url.substr(this.router.url.lastIndexOf('/') + 1); this.data.currentUser.subscribe(user => { this.self = user; + this.checkIfUserIsFriend(); }); this.profileService.getUserData(this.id); this.profileService.proflile.subscribe(response => { @@ -68,11 +71,29 @@ export class ProfileComponent implements OnInit { }); } + checkIfUserIsFriend() { + this.isFriendOfSelf = false; + for (const friend of this.self.friends) { + if (friend.id.toString() === this.id) { + this.isFriendOfSelf = true; + break; + } + } + } + public sendFriendRequest(user: User) { user.allowedToSendRequest = false; this.requestService.sendFriendRequest(user); } + removeFriend(user: User) { + this.socialService.removeFriend(user.userID).subscribe(response => { + this.checkIfUserIsFriend(); + // tslint:disable-next-line:max-line-length + this.userProfile.allowedToSendRequest = this.requestService.isAllowedToSendRequest(this.userProfile.userID, this.self); + }); + } + /** * Opens the file upload dialog */ diff --git a/src/app/components/social/friends/friends.component.html b/src/app/components/social/friends/friends.component.html index 6347cc4..c036f75 100644 --- a/src/app/components/social/friends/friends.component.html +++ b/src/app/components/social/friends/friends.component.html @@ -4,15 +4,24 @@
+ [class.selected]="friend === selectedFriend" + tabindex="0"> -
+
+ + + + +
+
- {{friend.name}} - {{friend.rankname}} + {{friend.name}} + {{friend.rankname}}
diff --git a/src/app/components/social/friends/friends.component.sass b/src/app/components/social/friends/friends.component.sass index 253ee4a..ea7af14 100644 --- a/src/app/components/social/friends/friends.component.sass +++ b/src/app/components/social/friends/friends.component.sass @@ -12,6 +12,10 @@ cursor: pointer outline: none user-select: none + #button-box + text-align: right + margin-left: auto + width: auto ::ng-deep .mat-card-header-text margin: 0 24px diff --git a/src/app/components/social/friends/friends.component.ts b/src/app/components/social/friends/friends.component.ts index 4cd3840..0420c82 100644 --- a/src/app/components/social/friends/friends.component.ts +++ b/src/app/components/social/friends/friends.component.ts @@ -1,5 +1,6 @@ import {Component, OnInit} from '@angular/core'; import {DatasharingService} from 'src/app/services/datasharing.service'; +import {SocialService} from 'src/app/services/social/social.service'; import {FriendInfo} from 'src/app/models/friendinfo'; import {Router} from '@angular/router'; import {User} from 'src/app/models/user'; @@ -12,7 +13,7 @@ import {User} from 'src/app/models/user'; export class FriendsComponent implements OnInit { user: User; - constructor(private data: DatasharingService, private router: Router) { + constructor(private data: DatasharingService, private router: Router, private socialService: SocialService) { } ngOnInit() { @@ -25,4 +26,8 @@ export class FriendsComponent implements OnInit { this.router.navigate(['profile/' + pFriend.id]); } + removeFriend(friend: FriendInfo) { + this.socialService.removeFriend(friend.id).subscribe(); + } + } diff --git a/src/app/services/datasharing.service.ts b/src/app/services/datasharing.service.ts index b79793c..67241db 100644 --- a/src/app/services/datasharing.service.ts +++ b/src/app/services/datasharing.service.ts @@ -44,7 +44,18 @@ export class DatasharingService { user.friends.push(friend); user.friendCount++; this.currentUser.next(user); -} + } + + removeFriendFromUser(id: number) { + const user: User = this.currentUser.getValue(); + for (let i = 0; i < user.friends.length; i++) { + if (user.friends[i].id === id) { + user.friends.splice(i, 1); + return; + } + } + this.currentUser.next(user); + } setDarkMode(active: boolean) { const user: User = this.currentUser.getValue(); diff --git a/src/app/services/social/social.service.ts b/src/app/services/social/social.service.ts index 2dc1c56..d42310e 100644 --- a/src/app/services/social/social.service.ts +++ b/src/app/services/social/social.service.ts @@ -2,6 +2,8 @@ import {Injectable} from '@angular/core'; import {environment} from 'src/environments/environment'; import {HttpClient} from '@angular/common/http'; import {BaseService} from '../base.service'; +import { tap } from 'rxjs/internal/operators/tap'; +import {DatasharingService} from 'src/app/services/datasharing.service'; const graphqlCreateGroupQuery = `mutation($name: String!) { createGroup(name: $name) { @@ -14,11 +16,12 @@ const graphqlCreateGroupQuery = `mutation($name: String!) { }) export class SocialService extends BaseService { - constructor(http: HttpClient) { + constructor(http: HttpClient, + private data: DatasharingService) { super(http); } - /** + /** * Builds the body for a group creation request * @param name */ @@ -38,4 +41,18 @@ export class SocialService extends BaseService { const body = SocialService.buildGroupCreateBody(name); return this.postGraphql(body); } + + public removeFriend(id: number) { + const body = { + query: `mutation($id: ID!) { + removeFriend(friendId: $id) + }`, variables: { + id + } + }; + return this.postGraphql(body) + .pipe(tap(response => { + this.data.removeFriendFromUser(id); + })); + } }