56 lines
1.9 KiB
TypeScript
56 lines
1.9 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
|
|
|
import { UserRole } from '../enums/user-role.enum';
|
|
import { Choice } from '../models/choice.model';
|
|
import { Poll } from '../models/poll.model';
|
|
import { User } from '../models/user.model';
|
|
import { ApiService } from './api.service';
|
|
import { UserService } from './user.service';
|
|
import { UuidService } from './uuid.service';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class MockingService {
|
|
private _pollsAvailables: BehaviorSubject<Poll[]> = new BehaviorSubject<Poll[]>([]);
|
|
public readonly pollsAvailables: Observable<Poll[]> = this._pollsAvailables.asObservable();
|
|
|
|
constructor(private apiService: ApiService, private userService: UserService, private uuidService: UuidService) {}
|
|
|
|
public async init(): Promise<void> {
|
|
const pollsAvailable = await this.apiService.getAllAvailablePolls();
|
|
this._pollsAvailables.next(pollsAvailable);
|
|
|
|
if (this._pollsAvailables.getValue() && this._pollsAvailables.getValue().length > 0) {
|
|
// arbitrary choose first owner available
|
|
const currentUser = this._pollsAvailables.getValue()[0].owner;
|
|
currentUser.polls = [this._pollsAvailables.getValue()[0]];
|
|
this.userService.updateUser(currentUser);
|
|
} else {
|
|
this.loadMock();
|
|
}
|
|
}
|
|
|
|
public loadMock(): void {
|
|
const owner = new User('TOTO', 'toto@gafam.com', [], UserRole.REGISTERED);
|
|
|
|
const poll1: Poll = new Poll(owner, this.uuidService.getUUID(), 'Quand le picnic ?', 'Pour faire la teuf');
|
|
const poll2: Poll = new Poll(
|
|
owner,
|
|
this.uuidService.getUUID(),
|
|
'On fait quoi à la soirée ?',
|
|
'Balancez vos idées'
|
|
);
|
|
|
|
poll1.choices = [new Choice('mardi prochain'), new Choice('mercredi')];
|
|
poll2.choices = [new Choice('jeux'), new Choice('danser'), new Choice('discuter en picolant')];
|
|
|
|
this._pollsAvailables.next([poll1, poll2]);
|
|
owner.polls = [poll1, poll2];
|
|
this.userService.updateUser(owner);
|
|
|
|
console.info('MOCKING user', { user: owner });
|
|
}
|
|
}
|