added data sharing through siblings

"login" and "app" share user object via DataSharingService
master
lampe_n1 5 years ago
parent 6536389eb8
commit 0b9f9cbbcb

@ -1,4 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { User } from './models/user';
import { DatasharingService } from './services/datasharing.service';
@Component({
selector: 'app-root',
@ -6,7 +8,9 @@ import { Component, OnInit } from '@angular/core';
styleUrls: ['./app.component.sass']
})
export class AppComponent implements OnInit {
constructor() { }
constructor(private data: DatasharingService) { }
userInfo: User
loggedIn : boolean = false;
userID : number;
@ -22,5 +26,7 @@ export class AppComponent implements OnInit {
requestIDs : number[];
ngOnInit() { }
ngOnInit() {
this.data.currentUserInfo.subscribe(user => this.userInfo = user)
}
}

@ -1,15 +1,15 @@
export interface User {
loggedIn : boolean;
userID : number;
username : string;
handle : string;
email : string;
points : number;
level : number;
export class User {
loggedIn : boolean
userID : number
username : string
handle : string
email : string
points : number
level : number
friendIDs : number[];
groupIDs : number[];
chatIDs : number[];
friendIDs : number[]
groupIDs : number[]
chatIDs : number[]
requestIDs : number[];
}
requestIDs : number[]
}

@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { DatasharingService } from './datasharing.service';
describe('DatasharingService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: DatasharingService = TestBed.get(DatasharingService);
expect(service).toBeTruthy();
});
});

@ -0,0 +1,18 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { User } from '../models/user';
@Injectable({
providedIn: 'root'
})
export class DatasharingService {
private userInfoSource = new BehaviorSubject<User>(new User())
currentUserInfo = this.userInfoSource.asObservable();
constructor() { }
changeUserInfo(pUserInfo: User) {
this.userInfoSource.next(pUserInfo)
}
}

@ -1,15 +1,15 @@
import { Injectable, EventEmitter, Output } from '@angular/core';
import {Http, URLSearchParams, Headers} from '@angular/http';
import { Login } from '../../models/login';
import { User } from 'src/app/models/user';
import { DatasharingService } from '../datasharing.service';
@Injectable({
providedIn: 'root'
})
export class LoginService {
constructor(private http: Http) { }
@Output() showChatEvent = new EventEmitter<Chatinfo>();
constructor(private http: Http, private data: DatasharingService) { }
public login(login : Login) {
@ -22,12 +22,12 @@ export class LoginService {
return this.http.post(url, this.buildJson(login))
.subscribe(response => {
console.log(response.text());
this.saveUserData(response.json())
this.updateUserInfo(response.json())
});
}
public saveUserData(text : any){
app = text.name;
public updateUserInfo(pUserInfo : User){
this.data.changeUserInfo(pUserInfo)
}

Loading…
Cancel
Save