fuse-angular/src/app/core/user/user.service.ts
sercan 3a1a7d44b6 Updated RxJS to 7.4.0
Optimized import paths
2021-11-05 11:36:03 +03:00

70 lines
1.7 KiB
TypeScript

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map, Observable, ReplaySubject, tap } from 'rxjs';
import { User } from 'app/core/user/user.types';
@Injectable({
providedIn: 'root'
})
export class UserService
{
private _user: ReplaySubject<User> = new ReplaySubject<User>(1);
/**
* Constructor
*/
constructor(private _httpClient: HttpClient)
{
}
// -----------------------------------------------------------------------------------------------------
// @ Accessors
// -----------------------------------------------------------------------------------------------------
/**
* Setter & getter for user
*
* @param value
*/
set user(value: User)
{
// Store the value
this._user.next(value);
}
get user$(): Observable<User>
{
return this._user.asObservable();
}
// -----------------------------------------------------------------------------------------------------
// @ Public methods
// -----------------------------------------------------------------------------------------------------
/**
* Get the current logged in user data
*/
get(): Observable<User>
{
return this._httpClient.get<User>('api/common/user').pipe(
tap((user) => {
this._user.next(user);
})
);
}
/**
* Update the user
*
* @param user
*/
update(user: User): Observable<any>
{
return this._httpClient.patch<User>('api/common/user', {user}).pipe(
map((response) => {
this._user.next(response);
})
);
}
}