funky-framadate-front/src/app/core/services/user.service.ts

38 lines
1.2 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
2020-05-05 18:17:12 +02:00
import { DialogService } from 'primeng';
import { BehaviorSubject, Observable } from 'rxjs';
2020-05-05 18:17:12 +02:00
import { UserRole } from '../enums/user-role.enum';
import { User } from '../models/user.model';
2020-05-01 19:10:17 +02:00
import { ApiService } from './api.service';
@Injectable({
providedIn: 'root',
})
export class UserService {
2020-06-12 19:17:39 +02:00
public anonymous: User = new User('', '', [], UserRole.ANONYMOUS);
2020-05-05 18:17:12 +02:00
2020-05-01 19:10:17 +02:00
private _user: BehaviorSubject<User> = new BehaviorSubject<User>(this.anonymous);
public readonly user: Observable<User> = this._user.asObservable();
2020-05-05 18:17:12 +02:00
constructor(private apiService: ApiService, public dialogService: DialogService) {}
public updateUser(user: User): void {
this._user.next(user);
}
2020-05-01 19:10:17 +02:00
2020-05-05 18:17:12 +02:00
public isCurrentUserIdentifiable(): boolean {
return this._user.getValue().pseudo ? true : false;
}
2020-05-01 19:10:17 +02:00
public async getUserPolls(): Promise<void> {
const currentUser: User = this._user.getValue();
2020-05-12 19:16:23 +02:00
currentUser.polls = await this.apiService.getPollsUrlsByUserEmail(currentUser.email);
2020-05-01 19:10:17 +02:00
this.updateUser(currentUser);
}
public async sendEmailToUserTheListOfItsPolls(): Promise<void> {
2020-05-12 19:16:23 +02:00
await this.apiService.sendEmailToUserOfItsPollsList(this._user.getValue().email);
2020-05-01 19:10:17 +02:00
}
}