From 8f8b95c5bd4382713a91fe988c60f5deb58fc093 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 28 Dec 2019 22:52:33 +0100 Subject: [PATCH 1/4] requests are disabled if the user already received a request --- src/app/app.module.ts | 4 +++- .../main-navigation.component.html | 12 ++++++---- .../main-navigation.component.sass | 1 - .../main-navigation.component.ts | 9 ++++++-- .../components/search/search.component.html | 2 +- src/app/components/search/search.component.ts | 23 +++++++++++++++---- src/app/models/friendRequest.ts | 8 +++++++ src/app/models/user.ts | 10 +++++--- src/app/services/datasharing.service.ts | 1 + src/app/services/login/login.service.ts | 21 +++++++++++++---- src/app/services/register/register.service.ts | 5 +++- src/app/services/selfservice/self.service.ts | 6 +++-- src/styles/greenvironment-material-theme.scss | 8 +++---- 13 files changed, 81 insertions(+), 29 deletions(-) create mode 100644 src/app/models/friendRequest.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index a94ae7e..9ad549b 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -45,6 +45,7 @@ import { OverlayModule} from '@angular/cdk/overlay'; import {MatSlideToggleModule} from '@angular/material/slide-toggle'; import {MatMenuModule} from '@angular/material/menu'; import {MatRippleModule} from '@angular/material/core'; +import {MatBadgeModule} from '@angular/material/badge'; import { FlexLayoutModule } from '@angular/flex-layout'; import { MainNavigationComponent } from './components/main-navigation/main-navigation.component'; @@ -122,7 +123,8 @@ const appRoutes: Routes = [ MatMenuModule, MatRippleModule, MatTableModule, - MatSortModule + MatSortModule, + MatBadgeModule ], providers: [], bootstrap: [AppComponent] diff --git a/src/app/components/main-navigation/main-navigation.component.html b/src/app/components/main-navigation/main-navigation.component.html index e8a57c6..c0f3232 100644 --- a/src/app/components/main-navigation/main-navigation.component.html +++ b/src/app/components/main-navigation/main-navigation.component.html @@ -57,21 +57,23 @@ - - - request + {{request.senderUsername}} +   @{{request.senderHandle}} diff --git a/src/app/components/main-navigation/main-navigation.component.sass b/src/app/components/main-navigation/main-navigation.component.sass index 8a7d485..7338cbb 100644 --- a/src/app/components/main-navigation/main-navigation.component.sass +++ b/src/app/components/main-navigation/main-navigation.component.sass @@ -26,7 +26,6 @@ .mat-tab-link height: 56px - #link-box padding: 0.5em diff --git a/src/app/components/main-navigation/main-navigation.component.ts b/src/app/components/main-navigation/main-navigation.component.ts index dc6b72d..0c957c7 100644 --- a/src/app/components/main-navigation/main-navigation.component.ts +++ b/src/app/components/main-navigation/main-navigation.component.ts @@ -102,11 +102,9 @@ export class MainNavigationComponent implements OnInit { const headers = new Headers(); headers.set('Content-Type', 'application/json'); - const body = {query: `mutation { logout }`}; - this.http.post(url, body).subscribe(response => { console.log(response.text()); }); this.loggedIn = false; @@ -115,4 +113,11 @@ export class MainNavigationComponent implements OnInit { this.data.changeUserInfo(user); this.router.navigate(['login']); } + + acceptRequest(id: number) { + console.log('accept request with id: ' + id); + } + denyRequest(id: number) { + console.log('deny request with id: ' + id); + } } diff --git a/src/app/components/search/search.component.html b/src/app/components/search/search.component.html index c9dc522..9c0151b 100644 --- a/src/app/components/search/search.component.html +++ b/src/app/components/search/search.component.html @@ -32,7 +32,7 @@ {{user.username}} {{user.handle}}
- +
diff --git a/src/app/components/search/search.component.ts b/src/app/components/search/search.component.ts index 641916e..cbcb0a9 100644 --- a/src/app/components/search/search.component.ts +++ b/src/app/components/search/search.component.ts @@ -5,6 +5,7 @@ import {Headers, Http} from '@angular/http'; import { User } from 'src/app/models/user'; import {environment} from 'src/environments/environment'; import { Router } from '@angular/router'; +import { DatasharingService } from '../../services/datasharing.service'; @Component({ selector: 'home-search', @@ -14,14 +15,19 @@ import { Router } from '@angular/router'; export class SearchComponent implements OnInit { searchValue = ' '; category = 'user'; + user: User; foundUsers: Array; constructor( private searchService: SearchService, private requestService: RequestService, private http: Http, - private router: Router) { } + private router: Router, + private data: DatasharingService) { } ngOnInit() { + this.data.currentUserInfo.subscribe(user => { + this.user = user; + }); } changeCategory(value: string) { @@ -47,9 +53,17 @@ export class SearchComponent implements OnInit { headers.set('Content-Type', 'application/json'); this.http.post(environment.graphQLUrl, this.searchService.buildJsonUser(name)) .subscribe(response => { - console.log('response received'); - console.log(response); this.foundUsers = this.searchService.renderUsers(response.json()); + for (const foundUser of this.foundUsers) { + if (!this.user.loggedIn) {foundUser.allowedToSendRequest = false; } else { + for (const receiverID of this.user.sentRequestUserIDs) { + if (foundUser.userID === receiverID || + foundUser.userID === this.user.userID) { + foundUser.allowedToSendRequest = false; + } + } + } + } }); } @@ -58,12 +72,11 @@ export class SearchComponent implements OnInit { } public sendFriendRequest(user: User) { + user.allowedToSendRequest = false; const headers = new Headers(); headers.set('Content-Type', 'application/json'); this.http.post(environment.graphQLUrl, this.requestService.buildJsonRequest(user.userID, 'FRIENDREQUEST')) .subscribe(response => { - console.log('response received'); - console.log(response); }); } } diff --git a/src/app/models/friendRequest.ts b/src/app/models/friendRequest.ts new file mode 100644 index 0000000..4d8e0c9 --- /dev/null +++ b/src/app/models/friendRequest.ts @@ -0,0 +1,8 @@ +import { User } from 'src/app/models/user'; + +export class FriendRequest { + id: number; + senderUserID: number; + senderHandle: string; + senderUsername: string; + } diff --git a/src/app/models/user.ts b/src/app/models/user.ts index 4453391..bbdac8c 100644 --- a/src/app/models/user.ts +++ b/src/app/models/user.ts @@ -1,5 +1,7 @@ +import { FriendRequest } from 'src/app/models/friendRequest'; + export class User { - loggedIn: boolean; + loggedIn = false; userID: number; username: string; handle: string; @@ -11,6 +13,8 @@ export class User { friendIDs: number[]; groupIDs: number[]; chatIDs: number[]; - - requestIDs: number[]; + receivedRequests: FriendRequest[] = new Array(); + sentRequestUserIDs: number[] = new Array(); // IDs of users that already received requests of the logged in user + allowedToSendRequest = true; /* if a user already received a request this should + be false to avoid multiple invitations*/ } diff --git a/src/app/services/datasharing.service.ts b/src/app/services/datasharing.service.ts index 8798f16..bc0d047 100644 --- a/src/app/services/datasharing.service.ts +++ b/src/app/services/datasharing.service.ts @@ -15,6 +15,7 @@ export class DatasharingService { constructor() { } changeUserInfo(pUserInfo: User) { + console.log('DatasharingService: user info updated'); this.userInfoSource.next(pUserInfo); } diff --git a/src/app/services/login/login.service.ts b/src/app/services/login/login.service.ts index 11ba72b..13eb803 100644 --- a/src/app/services/login/login.service.ts +++ b/src/app/services/login/login.service.ts @@ -1,10 +1,11 @@ import {Injectable} from '@angular/core'; -import {Headers, Http} from '@angular/http'; +import {Headers, Http, Request} from '@angular/http'; import {Login} from '../../models/login'; import {User} from 'src/app/models/user'; import {DatasharingService} from '../datasharing.service'; import {Router} from '@angular/router'; import {environment} from 'src/environments/environment'; +import { FriendRequest } from 'src/app/models/friendRequest'; @Injectable({ providedIn: 'root' @@ -35,6 +36,7 @@ export class LoginService { public updateUserInfo(response: any) { const user: User = new User(); + let friendRequest: FriendRequest = new FriendRequest(); user.loggedIn = true; user.userID = response.data.login.id; user.username = response.data.login.name; @@ -45,10 +47,19 @@ export class LoginService { user.friendIDs = response.data.login.friends; user.groupIDs = response.data.login.groups; user.chatIDs = response.data.login.chats; - user.requestIDs = response.data.login.requests; - + for (const request of response.data.login.sentRequests) { + user.sentRequestUserIDs.push(request.receiver.id); + } + for (const request of response.data.login.receivedRequests) { + friendRequest = new FriendRequest(); + friendRequest.id = request.id; + friendRequest.senderUserID = request.sender.id; + friendRequest.senderUsername = request.sender.name; + friendRequest.senderHandle = request.sender.handle; + user.receivedRequests.push(friendRequest); + } + console.log(user.receivedRequests); this.data.changeUserInfo(user); - } public buildJson(login: Login): any { @@ -61,6 +72,8 @@ export class LoginService { handle, points, level, + receivedRequests{id, sender{name, handle, id}}, + sentRequests{receiver{id}}, friends { id }, diff --git a/src/app/services/register/register.service.ts b/src/app/services/register/register.service.ts index 0e2ef11..b268774 100644 --- a/src/app/services/register/register.service.ts +++ b/src/app/services/register/register.service.ts @@ -48,7 +48,9 @@ export class RegisterService { user.friendIDs = response.data.register.friends; user.groupIDs = response.data.register.groups; user.chatIDs = response.data.register.chats; - user.requestIDs = response.data.register.requests; + for (const request of response.data.register.sentRequests) { + user.sentRequestUserIDs.push(request.receiver.id); + } this.data.changeUserInfo(user); @@ -62,6 +64,7 @@ export class RegisterService { handle, points, level, + sentRequests{receiver{id}} friends{id}, groups{id}, chats{id} diff --git a/src/app/services/selfservice/self.service.ts b/src/app/services/selfservice/self.service.ts index 03e4caa..1071eb8 100644 --- a/src/app/services/selfservice/self.service.ts +++ b/src/app/services/selfservice/self.service.ts @@ -53,14 +53,16 @@ export class SelfService { user.friendIDs = response.data.getSelf.friends; user.groupIDs = response.data.getSelf.groups; user.chatIDs = response.data.getSelf.chats; - user.requestIDs = response.data.getSelf.requests; + for (const request of response.data.getSelf.sentRequests) { + user.sentRequestUserIDs.push(request.receiver.id); + } this.data.changeUserInfo(user); } public buildJson(): any { const body = {query: `{ - getSelf{id, name,email, handle, points, level, friends{id}, groups{id},chats{id}} + getSelf{id, name, email, handle, points, level, sentRequests{receiver{id}}, friends{id}, groups{id},chats{id}} }`, variables: { }}; return body; diff --git a/src/styles/greenvironment-material-theme.scss b/src/styles/greenvironment-material-theme.scss index 88c7c5b..5c30826 100644 --- a/src/styles/greenvironment-material-theme.scss +++ b/src/styles/greenvironment-material-theme.scss @@ -9,8 +9,8 @@ // Define the palettes for your theme using the Material Design palettes available in palette.scss // (imported above). For each palette, you can optionally specify a default, lighter, and darker // hue. Available color palettes: https://material.io/design/color/ -$primary: mat-palette($mat-green); -$accent: mat-palette($mat-pink, A200, A100, A400); +$primary: mat-palette($mat-light-green); +$accent: mat-palette($mat-brown, A200, A100, A400); $background-color: map_get($mat-grey, 50); // The warn palette is optional (defaults to red). $warn: mat-palette($mat-red); @@ -27,8 +27,8 @@ $light-theme: map_merge($light-theme, (background: $background)); -$dark-primary: mat-palette($mat-green); -$dark-accent: mat-palette($mat-pink, A200, A100, A400); +$dark-primary: mat-palette($mat-light-green); +$dark-accent: mat-palette($mat-brown, A200, A100, A400); // The warn palette is optional (defaults to red). $dark-warn: mat-palette($mat-red); From cef2e80cf5d03e108d6ebc12b6f5bcc224c803c0 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 29 Dec 2019 01:09:55 +0100 Subject: [PATCH 2/4] requests and friends work completely now --- .../main-navigation.component.html | 6 +-- .../main-navigation.component.ts | 29 +++++++++++++- .../components/profile/profile.component.ts | 1 - src/app/components/search/search.component.ts | 10 +++++ .../social/friends/friends.component.html | 12 +----- .../social/friends/friends.component.ts | 39 ++----------------- src/app/models/user.ts | 3 +- src/app/services/login/login.service.ts | 11 ++++-- src/app/services/register/register.service.ts | 23 +++++++++-- src/app/services/request/request.service.ts | 25 ++++++++++++ src/app/services/search/search.service.ts | 2 +- src/app/services/selfservice/self.service.ts | 38 ++++++++++++++++-- 12 files changed, 136 insertions(+), 63 deletions(-) diff --git a/src/app/components/main-navigation/main-navigation.component.html b/src/app/components/main-navigation/main-navigation.component.html index c0f3232..3922891 100644 --- a/src/app/components/main-navigation/main-navigation.component.html +++ b/src/app/components/main-navigation/main-navigation.component.html @@ -57,7 +57,7 @@ \ No newline at end of file diff --git a/src/app/components/search/search.component.sass b/src/app/components/search/search.component.sass index 8b866e0..2989fc4 100644 --- a/src/app/components/search/search.component.sass +++ b/src/app/components/search/search.component.sass @@ -37,4 +37,4 @@ .icon-box text-align: right - width: 100% \ No newline at end of file + width: 100% diff --git a/src/app/components/search/search.component.ts b/src/app/components/search/search.component.ts index 6a0b79e..4cd8ea4 100644 --- a/src/app/components/search/search.component.ts +++ b/src/app/components/search/search.component.ts @@ -13,6 +13,7 @@ import { DatasharingService } from '../../services/datasharing.service'; styleUrls: ['./search.component.sass'] }) export class SearchComponent implements OnInit { + loading = false; searchValue = ' '; category = 'user'; user: User; @@ -40,6 +41,7 @@ export class SearchComponent implements OnInit { this.searchValue = searchWord; if (searchWord) { // if not null or empty if (this.category === 'user') { + this.loading = true; this.findUser(searchWord); } else if (this.category === 'groupe') { // this.findUserByHandle(searchWord); @@ -74,6 +76,7 @@ export class SearchComponent implements OnInit { } } } + this.loading = false; }); } From fee4778f30572e86197bded204e887127a77cb16 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 29 Dec 2019 01:35:39 +0100 Subject: [PATCH 4/4] fixed building bug --- .../components/main-navigation/main-navigation.component.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/components/main-navigation/main-navigation.component.html b/src/app/components/main-navigation/main-navigation.component.html index 3922891..7e6bd26 100644 --- a/src/app/components/main-navigation/main-navigation.component.html +++ b/src/app/components/main-navigation/main-navigation.component.html @@ -57,7 +57,7 @@