Last active
June 27, 2022 09:14
-
-
Save NyaGarcia/b69cd88602f3c348ddc340d8e9060bca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { Observable, filter, tap } from 'rxjs'; | |
import { FormComponent } from './components/form/form.component'; | |
import { MatDialog } from '@angular/material/dialog'; | |
import { PokedexFirestoreService } from 'src/app/core/pokedex-firestore.service'; | |
import { Pokemon } from './interfaces/pokemon.interface'; | |
@Component({ | |
selector: 'app-pokemon', | |
templateUrl: './pokemon.component.html', | |
styleUrls: ['./pokemon.component.scss'], | |
}) | |
export class PokemonComponent implements OnInit { | |
allPokemon$: Observable<Pokemon[]>; | |
selectedPokemon?: Pokemon; | |
constructor( | |
private readonly pokedexService: PokedexFirestoreService, | |
private readonly dialog: MatDialog | |
) {} | |
ngOnInit(): void { | |
this.allPokemon$ = this.pokedexService.getAll(); | |
} | |
addPokemon() { | |
const dialogRef = this.dialog.open(FormComponent, { | |
data: {}, | |
width: '40%', | |
}); | |
dialogRef | |
.afterClosed() | |
.pipe( | |
filter(Boolean), | |
tap((pokemon) => this.pokedexService.create(pokemon)) | |
) | |
.subscribe(); | |
} | |
updatePokemon() { | |
const dialogRef = this.dialog.open(FormComponent, { | |
data: { ...this.selectedPokemon }, | |
width: '40%', | |
}); | |
dialogRef | |
.afterClosed() | |
.pipe( | |
filter(Boolean), | |
tap((pokemon) => this.pokedexService.update(pokemon)), | |
tap((pokemon) => this.selectPokemon(pokemon)) | |
) | |
.subscribe(); | |
} | |
selectPokemon(pokemon: Pokemon) { | |
this.selectedPokemon = pokemon; | |
} | |
deletePokemon() { | |
this.pokedexService.delete(this.selectedPokemon!.id); | |
this.selectedPokemon = undefined; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment