File

src/app/core/profile/profile.service.ts

Index

Properties
Methods

Constructor

constructor(authService: AuthService, http: AuthHttp)
Parameters :
Name Type Optional Description
authService AuthService
http AuthHttp

Methods

checkNickname
checkNickname(name: string)
Parameters :
Name Type Optional Description
name string
Returns : any
getLocalProfile
getLocalProfile()
Returns : any
getProfile
getProfile(user: User)
Parameters :
Name Type Optional Description
user User
Private updateLocalProfile
updateLocalProfile(user: User)
Parameters :
Name Type Optional Description
user User
Returns : void
updateProfile
updateProfile(profile: UpdateProfileData)
Parameters :
Name Type Optional Description
profile UpdateProfileData
Returns : any

Properties

getProfileSubscription
getProfileSubscription: Subscription
Type : Subscription
Private profile
profile: Profile
Type : Profile
Default value : new EmptyProfile(this.authService.getUser())
profileState
profileState:
Default value : this.profileSubject.asObservable()
Private profileSubject
profileSubject:
Default value : new Subject<Profile>()
import {Injectable} from "@angular/core";
import {EmptyProfile, UpdateProfileData, ProfileImpl} from "./profile.data";
import {AuthService} from "../auth/auth.service";
import {Subject, Subscription, Observable} from "rxjs";
import {AuthHttp} from "../auth/auth-http.service";
import {User} from "../models/auth/user.model";
import {Profile} from "../models/profile/profile.model";


@Injectable()
export class ProfileService {

  getProfileSubscription: Subscription;

  private profile: Profile = new EmptyProfile(this.authService.getUser());
  private profileSubject = new Subject<Profile>();
  profileState = this.profileSubject.asObservable();

  constructor(private authService: AuthService, private http: AuthHttp) {
    if (authService.isLoggedIn()) {
      this.updateLocalProfile(authService.getUser());
    }
    this.authService.userState.subscribe(user => this.updateLocalProfile(user))
  }

  updateProfile(profile: UpdateProfileData) {
    return this.http.post('/api/v1/profile/update', JSON.stringify(profile)).map(rs => {
      this.profile = new ProfileImpl(this.authService.getUser()).setFields(profile);
      this.profileSubject.next(this.profile);
      return rs.json()
    })
  }

  getProfile(user: User): Observable<Profile> {
    return this.http.get('/api/v1/profile').map(rs => {
      this.profile = rs.json();
      this.profile.user = user;
      this.profileSubject.next(this.profile);
      return rs.json();
    })
  }

  getLocalProfile() {
    return this.profile;
  }

  checkNickname(name: string) {
    return this.http.get('/api/v1/profile/check/nickname/' + name)
  }

  private updateLocalProfile(user: User) {
    if (this.getProfileSubscription) {
      this.getProfileSubscription.unsubscribe()
    }
    this.getProfileSubscription = this.getProfile(user).subscribe()
  }


}

results matching ""

    No results matching ""