Skip to content

Commit

Permalink
refactor nav to separate component
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnaceMaes committed Aug 18, 2024
1 parent 7278d6a commit 8aa2e01
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 109 deletions.
110 changes: 2 additions & 108 deletions app/components/pokemon-details.gts
Original file line number Diff line number Diff line change
@@ -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);
}
};

<template>
<div
class='pokemon-details flex flex-col justify-center gap-16 md:flex-row'
Expand Down Expand Up @@ -78,71 +36,7 @@ export default class PokemonDetails extends Component<{
</div>
</div>

{{#if @pokemon.evolution}}
<section class='m-auto max-w-3xl'>
<h3 class='mt-12 text-2xl'>Evolutions</h3>

<div class='grid grid-cols-2 gap-8'>
<div>
{{#if @pokemon.evolution.prev}}
<button
type='button'
class='flex w-full cursor-pointer flex-col items-center rounded-xl bg-gradient-to-br from-pink-100 to-yellow-100 p-4 shadow transition-shadow hover:shadow-md'
{{on
'click'
(fn
this.transitionToPokemonDetails
(get @pokemon.evolution.prev 0)
'backwards'
)
}}
{{on
'pointerenter'
(fn
this.preloadImageForPokemonId
(get @pokemon.evolution.prev 0)
)
}}
>
⏪ Previous
</button>
{{else}}
<button
type='button'
class='flex w-full cursor-not-allowed flex-col items-center rounded-xl bg-gradient-to-br from-gray-100 to-slate-100 p-4 opacity-50 shadow'
>
⏪ Previous
</button>
{{/if}}
</div>
<div class='flex flex-col gap-2'>
{{#each @pokemon.evolution.next as |next|}}
<button
type='button'
class='flex w-full cursor-pointer flex-col items-center rounded-xl bg-gradient-to-br from-pink-100 to-yellow-100 p-4 shadow transition-shadow hover:shadow-md'
{{on
'click'
(fn this.transitionToPokemonDetails (get next 0) 'forwards')
}}
{{on
'pointerenter'
(fn this.preloadImageForPokemonId (get next 0))
}}
>
Next ⏩
</button>
{{else}}
<button
type='button'
class='flex w-full cursor-not-allowed flex-col items-center rounded-xl bg-gradient-to-br from-gray-100 to-slate-100 p-4 opacity-50 shadow'
>
Next ⏩
</button>
{{/each}}
</div>
</div>
</section>
{{/if}}
<PokemonEvolutionNav @pokemon={{@pokemon}} @allPokemon={{@allPokemon}} />

{{! prettier-ignore }}
<style>
Expand Down
114 changes: 114 additions & 0 deletions app/components/pokemon-evolution-nav.gts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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 { 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);
}

export default class PokemonEvolutionNav 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);
}
};

<template>
<section class='m-auto max-w-3xl'>
<h3 class='mt-12 text-2xl'>Evolutions</h3>

<div class='grid grid-cols-2 gap-8'>
<div>
{{#if @pokemon.evolution.prev}}
<button
type='button'
class='flex w-full cursor-pointer flex-col items-center rounded-xl bg-gradient-to-br from-pink-100 to-yellow-100 p-4 shadow transition-shadow hover:shadow-md'
{{on
'click'
(fn
this.transitionToPokemonDetails
(get @pokemon.evolution.prev 0)
'backwards'
)
}}
{{on
'pointerenter'
(fn
this.preloadImageForPokemonId (get @pokemon.evolution.prev 0)
)
}}
>
⏪ Previous
</button>
{{else}}
<button
type='button'
class='flex w-full cursor-not-allowed flex-col items-center rounded-xl bg-gradient-to-br from-gray-100 to-slate-100 p-4 opacity-50 shadow'
>
⏪ Previous
</button>
{{/if}}
</div>
<div class='flex flex-col gap-2'>
{{#each @pokemon.evolution.next as |next|}}
<button
type='button'
class='flex w-full cursor-pointer flex-col items-center rounded-xl bg-gradient-to-br from-pink-100 to-yellow-100 p-4 shadow transition-shadow hover:shadow-md'
{{on
'click'
(fn this.transitionToPokemonDetails (get next 0) 'forwards')
}}
{{on
'pointerenter'
(fn this.preloadImageForPokemonId (get next 0))
}}
>
Next ⏩
</button>
{{else}}
<button
type='button'
class='flex w-full cursor-not-allowed flex-col items-center rounded-xl bg-gradient-to-br from-gray-100 to-slate-100 p-4 opacity-50 shadow'
>
Next ⏩
</button>
{{/each}}
</div>
</div>
</section>
</template>
}
3 changes: 2 additions & 1 deletion app/templates/pokemon/pokemon.gts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import Component from '@glimmer/component';
import { Request } from '@warp-drive/ember';
import LoadingBar from 'ember-polaris-pokedex/components/loading-bar';
import HomeButton from 'ember-polaris-pokedex/components/home-button';
import PokemonDetails, { getPokemonById } from 'ember-polaris-pokedex/components/pokemon-details';
import PokemonDetails from 'ember-polaris-pokedex/components/pokemon-details';
import { getPokemonById } from 'ember-polaris-pokedex/components/pokemon-evolution-nav';

type PokemonTemplateSignature = RouteTemplateSignature<PokemonRoute>;

Expand Down

0 comments on commit 8aa2e01

Please sign in to comment.