Refactor datasharing service

master
Trivernis 5 years ago
parent 161c3f2afd
commit 42f4a737b2

@ -13,7 +13,7 @@ export class AppComponent implements OnInit {
} }
ngOnInit() { ngOnInit() {
this.data.currentUserInfo.subscribe(user => { this.data.currentUser.subscribe(user => {
if (user.loggedIn !== true) { if (user.loggedIn !== true) {
this.selfservice.checkIfLoggedIn().subscribe(); this.selfservice.checkIfLoggedIn().subscribe();
} }

@ -39,7 +39,7 @@ export class FeedComponent implements OnInit {
} }
ngOnInit() { ngOnInit() {
this.data.currentUserInfo.subscribe(user => { this.data.currentUser.subscribe(user => {
this.user = user; this.user = user;
this.loggedIn = user.loggedIn; this.loggedIn = user.loggedIn;
}); });

@ -23,8 +23,7 @@ export class DialogCreateEventComponent {
constructor( constructor(
public dialogRef: MatDialogRef<DialogCreateEventComponent>, public dialogRef: MatDialogRef<DialogCreateEventComponent>,
private group: GroupService, private group: GroupService,
private router: Router, private router: Router) {
private datasharingService: DatasharingService) {
this.groupId = this.router.url.substr(this.router.url.lastIndexOf('/') + 1); this.groupId = this.router.url.substr(this.router.url.lastIndexOf('/') + 1);
} }
@ -44,7 +43,7 @@ export class DialogCreateEventComponent {
if (name && date && time) { if (name && date && time) {
date = date + ' ' + time; date = date + ' ' + time;
this.group.createEvent(name, (new Date(date)).getTime().toString(), this.groupId) this.group.createEvent(name, (new Date(date)).getTime().toString(), this.groupId)
.subscribe(() => { .subscribe((response) => {
this.dialogRef.close(); this.dialogRef.close();
}, (error) => { }, (error) => {
if (error.error) { if (error.error) {
@ -85,7 +84,8 @@ export class GroupComponent implements OnInit {
public dialog: MatDialog, public dialog: MatDialog,
private requestService: RequestService, private requestService: RequestService,
private data: DatasharingService, private data: DatasharingService,
private groupService: GroupService) { private groupService: GroupService,
private datasharingService: DatasharingService) {
router.events.forEach((event) => { router.events.forEach((event) => {
// check if url changes // check if url changes
if (event instanceof NavigationEnd) { if (event instanceof NavigationEnd) {
@ -103,7 +103,7 @@ export class GroupComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.loading = true; this.loading = true;
this.id = this.router.url.substr(this.router.url.lastIndexOf('/') + 1); 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.self = user;
}); });
this.groupService.getGroupData(this.id).subscribe(); this.groupService.getGroupData(this.id).subscribe();
@ -151,7 +151,10 @@ export class GroupComponent implements OnInit {
public joinGroup(group: Group) { public joinGroup(group: Group) {
group.allowedToJoinGroup = false; group.allowedToJoinGroup = false;
this.requestService.joinGroup(group); this.requestService.joinGroup(group)
.subscribe(() => {
this.datasharingService.addGroupToUser(group);
});
} }
public joinEvent(event: Event) { public joinEvent(event: Event) {

@ -15,7 +15,7 @@ export class HomeComponent implements OnInit {
} }
ngOnInit() { ngOnInit() {
this.data.currentUserInfo.subscribe(user => { this.data.currentUser.subscribe(user => {
this.loggedIn = user.loggedIn; this.loggedIn = user.loggedIn;
}); });
} }

@ -61,7 +61,7 @@ export class MainNavigationComponent implements OnInit {
this.toggleTheme(); this.toggleTheme();
this.darkModeButtonChecked = true; this.darkModeButtonChecked = true;
} }
this.data.currentUserInfo.subscribe(user => { this.data.currentUser.subscribe(user => {
this.user = user; this.user = user;
this.loggedIn = user.loggedIn; this.loggedIn = user.loggedIn;
this.userId = user.userID; this.userId = user.userID;
@ -132,7 +132,7 @@ export class MainNavigationComponent implements OnInit {
this.loggedIn = false; this.loggedIn = false;
const user = new User(); const user = new User();
user.loggedIn = false; user.loggedIn = false;
this.data.changeUserInfo(user); this.data.currentUser.next(user);
this.router.navigate(['login']); this.router.navigate(['login']);
}); });
} }

@ -49,7 +49,7 @@ export class ProfileComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.loading = true; this.loading = true;
this.id = this.router.url.substr(this.router.url.lastIndexOf('/') + 1); 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.self = user;
}); });
this.profileService.getUserData(this.id); this.profileService.getUserData(this.id);

@ -27,7 +27,7 @@ export class SearchComponent implements OnInit {
} }
ngOnInit() { ngOnInit() {
this.data.currentUserInfo.subscribe(user => { this.data.currentUser.subscribe(user => {
this.user = user; this.user = user;
}); });
} }

@ -16,7 +16,7 @@ export class FriendsComponent implements OnInit {
} }
ngOnInit() { ngOnInit() {
this.data.currentUserInfo.subscribe(user => { this.data.currentUser.subscribe(user => {
this.user = user; this.user = user;
}); });
} }

@ -11,6 +11,7 @@
margin-top: 0.5em margin-top: 0.5em
outline: none outline: none
user-select: none user-select: none
cursor: pointer
::ng-deep .mat-card-header-text ::ng-deep .mat-card-header-text
width: 1000% width: 1000%
margin: auto 0 auto 24px margin: auto 0 auto 24px

@ -54,7 +54,7 @@ export class GroupsComponent implements OnInit {
} }
ngOnInit() { ngOnInit() {
this.data.currentUserInfo.subscribe(user => { this.data.currentUser.subscribe(user => {
this.user = user; this.user = user;
}); });
} }

@ -16,7 +16,7 @@ export class ChatService {
chats: Array<Chat> = []; chats: Array<Chat> = [];
constructor(private http: Http, private data: DatasharingService) { constructor(private http: Http, private data: DatasharingService) {
this.data.currentUserInfo.subscribe(user => { this.data.currentUser.subscribe(user => {
this.ownID = user.userID; this.ownID = user.userID;
}); });
} }

@ -1,37 +1,47 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {BehaviorSubject} from 'rxjs'; import {BehaviorSubject} from 'rxjs';
import {User} from '../models/user'; import {User} from '../models/user';
import {FriendInfo} from '../models/friendinfo';
import {Group} from '../models/group';
import {GroupInfo} from '../models/groupinfo';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class DatasharingService { export class DatasharingService {
private userInfoSource = new BehaviorSubject<User>(new User()); currentUser = new BehaviorSubject<User>(new User());
private chatIDsSource = new BehaviorSubject<number[]>(new Array<number>());
currentUserInfo = this.userInfoSource.asObservable();
currentChatIDs = this.chatIDsSource.asObservable();
constructor() { constructor() {
} }
changeUserInfo(pUserInfo: User) {
this.userInfoSource.next(pUserInfo);
}
addSentRequestUserID(id: number) { addSentRequestUserID(id: number) {
const user: User = this.userInfoSource.getValue(); const user: User = this.currentUser.getValue();
user.sentRequestUserIDs.push(id); 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) { setDarkMode(active: boolean) {
const user: User = this.userInfoSource.getValue(); const user: User = this.currentUser.getValue();
user.darkmode = active; user.darkmode = active;
this.changeUserInfo(user); this.currentUser.next(user);
} }
changeChatIDs(pChatIDs: number[]) { changeUserInfo(user: User) {
this.chatIDsSource.next(pChatIDs); this.currentUser.next(user);
} }
} }

@ -127,8 +127,7 @@ export class RequestService extends BaseService {
* @param group * @param group
*/ */
public joinGroup(group: GroupInfo) { public joinGroup(group: GroupInfo) {
this.postGraphql(RequestService.buildJoinGroupBody(group.id)) return this.postGraphql(RequestService.buildJoinGroupBody(group.id));
.subscribe();
} }
/** /**

Loading…
Cancel
Save