Refactor datasharing service

master
Trivernis 5 years ago
parent 161c3f2afd
commit 42f4a737b2

@ -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();
}

@ -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;
});

@ -23,8 +23,7 @@ export class DialogCreateEventComponent {
constructor(
public dialogRef: MatDialogRef<DialogCreateEventComponent>,
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) {

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

@ -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']);
});
}

@ -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);

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

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

@ -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

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

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

@ -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<User>(new User());
private chatIDsSource = new BehaviorSubject<number[]>(new Array<number>());
currentUserInfo = this.userInfoSource.asObservable();
currentChatIDs = this.chatIDsSource.asObservable();
currentUser = new BehaviorSubject<User>(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);
}
}

@ -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));
}
/**

Loading…
Cancel
Save