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 { Component, OnDestroy, OnInit } from '@angular/core';
import { FormBuilder } from '@angular/forms'; import { FormBuilder, FormGroup } from '@angular/forms';
import { Champion } from 'src/models/champion'; import { Champion } from 'src/models/champion';
import { Bonus } from 'src/models/bonus'; import { Bonus } from 'src/models/bonus';
import { PoolService } from 'src/shared/pool.service'; 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 { DomSanitizer } from '@angular/platform-browser';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { DataToShare } from 'src/models/dataToShare';
@ -17,13 +19,13 @@ import { Subscription } from 'rxjs';
}) })
export class HomeComponent implements OnInit, OnDestroy { export class HomeComponent implements OnInit, OnDestroy {
formFilters; formFilters: FormGroup;
bonusesPool: Bonus[] = []; bonusesPool: Bonus[] = [];
champions: Champion[] = []; champions: Champion[] = [];
noChampSelected = true; noChampSelected = true;
roles: [] = []; roles: string[] = [];
rolesCount: [] = []; rolesCount: number[] = [];
rolesPool: [] = []; rolesPool: string[] = [];
teamSize = 0; teamSize = 0;
subscription: Subscription; subscription: Subscription;
@ -33,7 +35,7 @@ export class HomeComponent implements OnInit, OnDestroy {
private sanitizer: DomSanitizer, private sanitizer: DomSanitizer,
private poolService: PoolService private poolService: PoolService
) { ) {
this.subscription = this.poolService.setChampions().subscribe( data => { this.subscription = this.poolService.setChampions().subscribe( (data: DataToShare) => {
if (data) { if (data) {
const {champions, roles, rolesPool, bonusesPool, noChampSelected, teamSize } = data; const {champions, roles, rolesPool, bonusesPool, noChampSelected, teamSize } = data;
this.champions = champions; 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 { Constants } from 'src/shared/constants';
import { Bonus } from 'src/models/bonus'; import { Bonus } from 'src/models/bonus';
import { Champion } from 'src/models/champion'; import { Champion } from 'src/models/champion';
import { DataToShare } from 'src/models/dataToShare';
import * as _ from 'lodash'; import * as _ from 'lodash';
@ -16,12 +17,12 @@ export class PoolService {
champions: Champion[] = [...Constants.Champions]; champions: Champion[] = [...Constants.Champions];
championsPool: Champion[] = []; championsPool: Champion[] = [];
noChampSelected = true; noChampSelected = true;
roles = [...Constants.roles]; roles: string[]= [...Constants.roles];
rolesCount = []; rolesCount: number[]= [];
rolesPool = []; rolesPool: string[] = [];
selectedRole = ''; selectedRole = '';
teamSize = 0; teamSize = 0;
dataToShare = new BehaviorSubject<any>(null); dataToShare = new BehaviorSubject<DataToShare>(null);
constructor() { } constructor() { }
@ -29,7 +30,7 @@ export class PoolService {
* Set Champion pool and return data * Set Champion pool and return data
* @returns Observable * @returns Observable
*/ */
setChampions(): Observable<any> { setChampions(): Observable<DataToShare> {
this.roles.forEach(role => { this.roles.forEach(role => {
this.rolesCount[role] = 0; this.rolesCount[role] = 0;
}); });
@ -38,7 +39,7 @@ export class PoolService {
return this.dataToShare.asObservable(); return this.dataToShare.asObservable();
} }
updateDataToShare() { updateDataToShare(): void {
this.dataToShare.next({ this.dataToShare.next({
champions: this.champions, champions: this.champions,
roles: this.roles, roles: this.roles,