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 = new ReplaySubject(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 { return this._user.asObservable(); } // ----------------------------------------------------------------------------------------------------- // @ Public methods // ----------------------------------------------------------------------------------------------------- /** * Get the current logged in user data */ get(): Observable { return this._httpClient.get('api/common/user').pipe( tap((user) => { this._user.next(user); }) ); } /** * Update the user * * @param user */ update(user: User): Observable { return this._httpClient.patch('api/common/user', {user}).pipe( map((response) => { this._user.next(response); }) ); } }