Refactor poolservice

This commit is contained in:
Daniel Rodriguez 2021-08-04 23:57:43 +02:00
parent a45e4f2c1d
commit 197fa9c530
3 changed files with 29 additions and 12 deletions

View file

@ -1,5 +1,6 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Champion } from 'src/models/champion';
import { Bonus } from 'src/models/bonus';
import { PoolService } from 'src/shared/pool.service';
@ -7,6 +8,7 @@ import { PoolService } from 'src/shared/pool.service';
import { DomSanitizer } from '@angular/platform-browser';
import * as _ from 'lodash';
import { Subscription } from 'rxjs';
import { DataToShare } from 'src/models/dataToShare';
@ -17,13 +19,13 @@ import { Subscription } from 'rxjs';
})
export class HomeComponent implements OnInit, OnDestroy {
formFilters;
formFilters: FormGroup;
bonusesPool: Bonus[] = [];
champions: Champion[] = [];
noChampSelected = true;
roles: [] = [];
rolesCount: [] = [];
rolesPool: [] = [];
roles: string[] = [];
rolesCount: number[] = [];
rolesPool: string[] = [];
teamSize = 0;
subscription: Subscription;
@ -33,7 +35,7 @@ export class HomeComponent implements OnInit, OnDestroy {
private sanitizer: DomSanitizer,
private poolService: PoolService
) {
this.subscription = this.poolService.setChampions().subscribe( data => {
this.subscription = this.poolService.setChampions().subscribe( (data: DataToShare) => {
if (data) {
const {champions, roles, rolesPool, bonusesPool, noChampSelected, teamSize } = data;
this.champions = champions;

14
src/models/dataToShare.ts Normal file
View file

@ -0,0 +1,14 @@
import { Bonus } from './bonus';
import { Champion } from './champion';
export class DataToShare {
constructor(
public champions: Champion[],
public roles: string[],
public rolesCount: number[],
public rolesPool: string[],
public bonusesPool: Bonus[],
public noChampSelected: boolean,
public teamSize: number
) {}
}

View file

@ -4,6 +4,7 @@ import { BehaviorSubject, Observable } from 'rxjs';
import { Constants } from 'src/shared/constants';
import { Bonus } from 'src/models/bonus';
import { Champion } from 'src/models/champion';
import { DataToShare } from 'src/models/dataToShare';
import * as _ from 'lodash';
@ -16,12 +17,12 @@ export class PoolService {
champions: Champion[] = [...Constants.Champions];
championsPool: Champion[] = [];
noChampSelected = true;
roles = [...Constants.roles];
rolesCount = [];
rolesPool = [];
roles: string[]= [...Constants.roles];
rolesCount: number[]= [];
rolesPool: string[] = [];
selectedRole = '';
teamSize = 0;
dataToShare = new BehaviorSubject<any>(null);
dataToShare = new BehaviorSubject<DataToShare>(null);
constructor() { }
@ -29,7 +30,7 @@ export class PoolService {
* Set Champion pool and return data
* @returns Observable
*/
setChampions(): Observable<any> {
setChampions(): Observable<DataToShare> {
this.roles.forEach(role => {
this.rolesCount[role] = 0;
});
@ -38,7 +39,7 @@ export class PoolService {
return this.dataToShare.asObservable();
}
updateDataToShare() {
updateDataToShare(): void {
this.dataToShare.next({
champions: this.champions,
roles: this.roles,