From 42f4a737b290551942d35a95ba499202a322dbe8 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 20 Jan 2020 11:11:42 +0100 Subject: [PATCH] Refactor datasharing service --- src/app/app.component.ts | 2 +- src/app/components/feed/feed.component.ts | 2 +- src/app/components/group/group.component.ts | 15 +++++--- src/app/components/home/home.component.ts | 2 +- .../main-navigation.component.ts | 4 +- .../components/profile/profile.component.ts | 2 +- src/app/components/search/search.component.ts | 2 +- .../social/friends/friends.component.ts | 2 +- .../social/groups/groups.component.sass | 1 + .../social/groups/groups.component.ts | 2 +- src/app/services/chat/chat.service.ts | 2 +- src/app/services/datasharing.service.ts | 38 ++++++++++++------- src/app/services/request/request.service.ts | 3 +- 13 files changed, 45 insertions(+), 32 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 5e15e30..9f11034 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -13,7 +13,7 @@ export class AppComponent implements OnInit { } ngOnInit() { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { if (user.loggedIn !== true) { this.selfservice.checkIfLoggedIn().subscribe(); } diff --git a/src/app/components/feed/feed.component.ts b/src/app/components/feed/feed.component.ts index 1ceb1d9..d7d85b8 100644 --- a/src/app/components/feed/feed.component.ts +++ b/src/app/components/feed/feed.component.ts @@ -39,7 +39,7 @@ export class FeedComponent implements OnInit { } ngOnInit() { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.user = user; this.loggedIn = user.loggedIn; }); diff --git a/src/app/components/group/group.component.ts b/src/app/components/group/group.component.ts index 24c9719..a8cc934 100644 --- a/src/app/components/group/group.component.ts +++ b/src/app/components/group/group.component.ts @@ -23,8 +23,7 @@ export class DialogCreateEventComponent { constructor( public dialogRef: MatDialogRef, private group: GroupService, - private router: Router, - private datasharingService: DatasharingService) { + private router: Router) { this.groupId = this.router.url.substr(this.router.url.lastIndexOf('/') + 1); } @@ -44,7 +43,7 @@ export class DialogCreateEventComponent { if (name && date && time) { date = date + ' ' + time; this.group.createEvent(name, (new Date(date)).getTime().toString(), this.groupId) - .subscribe(() => { + .subscribe((response) => { this.dialogRef.close(); }, (error) => { if (error.error) { @@ -85,7 +84,8 @@ export class GroupComponent implements OnInit { public dialog: MatDialog, private requestService: RequestService, private data: DatasharingService, - private groupService: GroupService) { + private groupService: GroupService, + private datasharingService: DatasharingService) { router.events.forEach((event) => { // check if url changes if (event instanceof NavigationEnd) { @@ -103,7 +103,7 @@ export class GroupComponent implements OnInit { ngOnInit() { this.loading = true; this.id = this.router.url.substr(this.router.url.lastIndexOf('/') + 1); - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.self = user; }); this.groupService.getGroupData(this.id).subscribe(); @@ -151,7 +151,10 @@ export class GroupComponent implements OnInit { public joinGroup(group: Group) { group.allowedToJoinGroup = false; - this.requestService.joinGroup(group); + this.requestService.joinGroup(group) + .subscribe(() => { + this.datasharingService.addGroupToUser(group); + }); } public joinEvent(event: Event) { diff --git a/src/app/components/home/home.component.ts b/src/app/components/home/home.component.ts index 1e80db3..d4a3951 100644 --- a/src/app/components/home/home.component.ts +++ b/src/app/components/home/home.component.ts @@ -15,7 +15,7 @@ export class HomeComponent implements OnInit { } ngOnInit() { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.loggedIn = user.loggedIn; }); } diff --git a/src/app/components/main-navigation/main-navigation.component.ts b/src/app/components/main-navigation/main-navigation.component.ts index 609d8d5..2990672 100644 --- a/src/app/components/main-navigation/main-navigation.component.ts +++ b/src/app/components/main-navigation/main-navigation.component.ts @@ -61,7 +61,7 @@ export class MainNavigationComponent implements OnInit { this.toggleTheme(); this.darkModeButtonChecked = true; } - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.user = user; this.loggedIn = user.loggedIn; this.userId = user.userID; @@ -132,7 +132,7 @@ export class MainNavigationComponent implements OnInit { this.loggedIn = false; const user = new User(); user.loggedIn = false; - this.data.changeUserInfo(user); + this.data.currentUser.next(user); this.router.navigate(['login']); }); } diff --git a/src/app/components/profile/profile.component.ts b/src/app/components/profile/profile.component.ts index 5242f09..9f92026 100644 --- a/src/app/components/profile/profile.component.ts +++ b/src/app/components/profile/profile.component.ts @@ -49,7 +49,7 @@ export class ProfileComponent implements OnInit { ngOnInit() { this.loading = true; this.id = this.router.url.substr(this.router.url.lastIndexOf('/') + 1); - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.self = user; }); this.profileService.getUserData(this.id); diff --git a/src/app/components/search/search.component.ts b/src/app/components/search/search.component.ts index bc379ea..9dd92f8 100644 --- a/src/app/components/search/search.component.ts +++ b/src/app/components/search/search.component.ts @@ -27,7 +27,7 @@ export class SearchComponent implements OnInit { } ngOnInit() { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.user = user; }); } diff --git a/src/app/components/social/friends/friends.component.ts b/src/app/components/social/friends/friends.component.ts index 6ef08f0..4cd3840 100644 --- a/src/app/components/social/friends/friends.component.ts +++ b/src/app/components/social/friends/friends.component.ts @@ -16,7 +16,7 @@ export class FriendsComponent implements OnInit { } ngOnInit() { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.user = user; }); } diff --git a/src/app/components/social/groups/groups.component.sass b/src/app/components/social/groups/groups.component.sass index d7f5f1a..3e12dce 100644 --- a/src/app/components/social/groups/groups.component.sass +++ b/src/app/components/social/groups/groups.component.sass @@ -11,6 +11,7 @@ margin-top: 0.5em outline: none user-select: none + cursor: pointer ::ng-deep .mat-card-header-text width: 1000% margin: auto 0 auto 24px diff --git a/src/app/components/social/groups/groups.component.ts b/src/app/components/social/groups/groups.component.ts index 4448a75..6097462 100644 --- a/src/app/components/social/groups/groups.component.ts +++ b/src/app/components/social/groups/groups.component.ts @@ -54,7 +54,7 @@ export class GroupsComponent implements OnInit { } ngOnInit() { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.user = user; }); } diff --git a/src/app/services/chat/chat.service.ts b/src/app/services/chat/chat.service.ts index be92ac2..14a5c82 100644 --- a/src/app/services/chat/chat.service.ts +++ b/src/app/services/chat/chat.service.ts @@ -16,7 +16,7 @@ export class ChatService { chats: Array = []; constructor(private http: Http, private data: DatasharingService) { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.ownID = user.userID; }); } diff --git a/src/app/services/datasharing.service.ts b/src/app/services/datasharing.service.ts index 160cc66..9f21f30 100644 --- a/src/app/services/datasharing.service.ts +++ b/src/app/services/datasharing.service.ts @@ -1,37 +1,47 @@ import {Injectable} from '@angular/core'; import {BehaviorSubject} from 'rxjs'; import {User} from '../models/user'; +import {FriendInfo} from '../models/friendinfo'; +import {Group} from '../models/group'; +import {GroupInfo} from '../models/groupinfo'; @Injectable({ providedIn: 'root' }) export class DatasharingService { - private userInfoSource = new BehaviorSubject(new User()); - private chatIDsSource = new BehaviorSubject(new Array()); - currentUserInfo = this.userInfoSource.asObservable(); - currentChatIDs = this.chatIDsSource.asObservable(); + currentUser = new BehaviorSubject(new User()); constructor() { } - changeUserInfo(pUserInfo: User) { - this.userInfoSource.next(pUserInfo); - } - addSentRequestUserID(id: number) { - const user: User = this.userInfoSource.getValue(); + const user: User = this.currentUser.getValue(); user.sentRequestUserIDs.push(id); - this.changeUserInfo(user); + this.currentUser.next(user); + } + + addGroupToUser(group: GroupInfo) { + const user = this.currentUser.getValue(); + user.groups.push(group); + user.groupCount++; + this.currentUser.next(user); } + addFriendToUser(friend: FriendInfo) { + const user = this.currentUser.getValue(); + user.friends.push(friend); + user.friendCount++; + this.currentUser.next(user); +} + setDarkMode(active: boolean) { - const user: User = this.userInfoSource.getValue(); + const user: User = this.currentUser.getValue(); user.darkmode = active; - this.changeUserInfo(user); + this.currentUser.next(user); } - changeChatIDs(pChatIDs: number[]) { - this.chatIDsSource.next(pChatIDs); + changeUserInfo(user: User) { + this.currentUser.next(user); } } diff --git a/src/app/services/request/request.service.ts b/src/app/services/request/request.service.ts index 94ecdc3..bb949da 100644 --- a/src/app/services/request/request.service.ts +++ b/src/app/services/request/request.service.ts @@ -127,8 +127,7 @@ export class RequestService extends BaseService { * @param group */ public joinGroup(group: GroupInfo) { - this.postGraphql(RequestService.buildJoinGroupBody(group.id)) - .subscribe(); + return this.postGraphql(RequestService.buildJoinGroupBody(group.id)); } /**