diff --git a/src/app/features/plan/plan-pokemon/plan-pokemon.component.ts b/src/app/features/plan/plan-pokemon/plan-pokemon.component.ts index 4a87aa4..347d5ff 100644 --- a/src/app/features/plan/plan-pokemon/plan-pokemon.component.ts +++ b/src/app/features/plan/plan-pokemon/plan-pokemon.component.ts @@ -1,4 +1,4 @@ -import { Component, Input, Output, EventEmitter, ChangeDetectorRef } from '@angular/core'; +import { Component, Input, Output, EventEmitter, ChangeDetectorRef, SimpleChanges } from '@angular/core'; import { CommonModule } from '@angular/common'; import { MatExpansionModule } from '@angular/material/expansion'; import { MatIconModule } from '@angular/material/icon'; @@ -274,6 +274,10 @@ export class PlanPokemonComponent { ) {} ngOnInit() { + this.representative_pokemon = null; + this.evolve_to = [] + this.breed_for = [] + this.pokemonService.getPokemonFromPFIC(this.pokemon_family.representative).subscribe({ next: (pokemon) => { this.representative_pokemon = pokemon @@ -311,7 +315,67 @@ export class PlanPokemonComponent { } }); } + } + + ngOnChanges(changes: SimpleChanges) { + if (changes['pokemon_family']) { + const currentFamily = changes['pokemon_family'].currentValue; + const previousFamily = changes['pokemon_family'].previousValue; + + // Check if there's a meaningful change + if (currentFamily && currentFamily !== previousFamily) { + // Your logic here, e.g., re-fetch data or reset states + this.handlePokemonFamilyChange(currentFamily); + } + } + } + private handlePokemonFamilyChange(newFamily: PokemonFamilyEntry) { + // This function contains logic to handle the input change. + // For example, resetting component states or fetching additional data. + console.log('Pokemon family has changed:', newFamily); + + this.representative_pokemon = null; + this.evolve_to = [] + this.breed_for = [] + + this.pokemonService.getPokemonFromPFIC(this.pokemon_family.representative).subscribe({ + next: (pokemon) => { + this.representative_pokemon = pokemon + }, + error: (error) => { + console.error('Error loading Pokemon:', error); + this.cdr.markForCheck(); + } + }); + + for(const target of this.pokemon_family.evolve_to) { + this.pokemonService.getPokemonFromPFIC(target).subscribe({ + next: (pokemon) => { + if(pokemon) { + this.evolve_to.push(pokemon) + } + }, + error: (error) => { + console.error('Error loading Pokemon:', error); + this.cdr.markForCheck(); + } + }); + } + + for(const target of this.pokemon_family.breed_for) { + this.pokemonService.getPokemonFromPFIC(target).subscribe({ + next: (pokemon) => { + if(pokemon) { + this.breed_for.push(pokemon) + } + }, + error: (error) => { + console.error('Error loading Pokemon:', error); + this.cdr.markForCheck(); + } + }); + } } get hasTargets(): boolean { diff --git a/src/app/features/plan/plan.component.ts b/src/app/features/plan/plan.component.ts index b95ade9..4c39dec 100644 --- a/src/app/features/plan/plan.component.ts +++ b/src/app/features/plan/plan.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core'; import { CommonModule } from '@angular/common'; import { MatCardModule } from '@angular/material/card'; import { FormsModule } from '@angular/forms'; @@ -6,6 +6,7 @@ import { PlanGameComponent } from './plan-game/plan-game.component'; import { PlanService } from '../../core/services/plan.service'; import { GamePlan } from '../../core/models/plan.model'; import { PlanPokemonComponent } from "./plan-pokemon/plan-pokemon.component"; +import { ScrollingModule, CdkVirtualScrollViewport } from '@angular/cdk/scrolling'; @Component({ selector: 'app-plan', @@ -15,7 +16,8 @@ import { PlanPokemonComponent } from "./plan-pokemon/plan-pokemon.component"; MatCardModule, FormsModule, PlanGameComponent, - PlanPokemonComponent + PlanPokemonComponent, + ScrollingModule ], template: `