From 8aa2e01824515d1557c8826f4aa72e41c0656063 Mon Sep 17 00:00:00 2001 From: Ignace Maes Date: Sun, 18 Aug 2024 19:48:45 +0200 Subject: [PATCH] refactor nav to separate component --- app/components/pokemon-details.gts | 110 +--------------------- app/components/pokemon-evolution-nav.gts | 114 +++++++++++++++++++++++ app/templates/pokemon/pokemon.gts | 3 +- 3 files changed, 118 insertions(+), 109 deletions(-) create mode 100644 app/components/pokemon-evolution-nav.gts diff --git a/app/components/pokemon-details.gts b/app/components/pokemon-details.gts index 821781d..a9d6e16 100644 --- a/app/components/pokemon-details.gts +++ b/app/components/pokemon-details.gts @@ -1,53 +1,11 @@ import Component from '@glimmer/component'; -import { get } from '@ember/helper'; import type PokemonModel from 'ember-polaris-pokedex/models/pokemon'; -import type RouterService from '@ember/routing/router-service'; -import { service } from '@ember/service'; -import { fn } from '@ember/helper'; -import { on } from '@ember/modifier'; -import { preloadImage } from 'ember-polaris-pokedex/components/pokemon-grid-item'; import PokemonTypeBadge from 'ember-polaris-pokedex/components/pokemon-type-badge'; -import { type RouterScrollService } from 'ember-polaris-pokedex/utils/router-scroll-service'; - -export function getPokemonById(pokemons: PokemonModel[], id: string) { - return pokemons.find((pokemon) => pokemon.id!.toString() === id); -} +import PokemonEvolutionNav from 'ember-polaris-pokedex/components/pokemon-evolution-nav'; export default class PokemonDetails extends Component<{ Args: { pokemon: PokemonModel; allPokemon: PokemonModel[] }; }> { - @service declare router: RouterService; - - @service declare routerScroll: RouterScrollService; - - transitionToPokemonDetails = ( - pokemonId: string, - direction: 'forwards' | 'backwards', - ) => { - this.routerScroll.preserveScrollPosition = true; - // Fallback for browsers that don't support this API: - if (!document.startViewTransition) { - this.router.transitionTo('pokemon.pokemon', pokemonId.toString()); - return; - } - - // With a transition: - document.startViewTransition({ - // @ts-expect-error: No types for these options yet - update: () => { - this.router.transitionTo('pokemon.pokemon', pokemonId.toString()); - }, - types: ['slide', direction], - }); - }; - - preloadImageForPokemonId = (pokemonId: string) => { - const pokemon = getPokemonById(this.args.allPokemon, pokemonId); - if (pokemon) { - preloadImage(pokemon.image.hires); - } - }; -