-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,77 @@ | ||
<script lang='ts'> | ||
import './paises.css' | ||
import { goto } from '$app/navigation' | ||
import { Pais } from '../lib/pais' | ||
import { paisService } from '../lib/paisService' | ||
<script lang="ts"> | ||
import './paises.css' | ||
import { goto } from '$app/navigation' | ||
import { Pais } from '../lib/pais' | ||
import { paisService } from '../lib/paisService' | ||
import Bandera from '$lib/Bandera.svelte' | ||
let paisBusqueda = $state('') | ||
let busquedaAutomatica = $state(false) | ||
let paises = $state<Pais[]>([]) | ||
let buscarHabilitado = $derived(paisBusqueda.trim() !== '') | ||
let paisBusqueda = $state('') | ||
let busquedaAutomatica = $state(false) | ||
let paises = $state<Pais[]>([]) | ||
let buscarHabilitado = $derived(paisBusqueda.trim() !== '') | ||
const buscar = async () => { | ||
paises = await paisService.buscarPais(paisBusqueda) | ||
} | ||
const buscar = async () => { | ||
paises = await paisService.buscarPais(paisBusqueda) | ||
} | ||
const handleKeyDownBuscar = async (event: { keyCode: number }) => { | ||
if (event.keyCode === 13) { | ||
await buscar() | ||
} | ||
} | ||
const handleKeyDownBuscar = async (event: { keyCode: number }) => { | ||
if (event.keyCode === 13) { | ||
await buscar() | ||
} | ||
} | ||
let timeout: ReturnType<typeof setTimeout> | ||
let timeout: ReturnType<typeof setTimeout> | ||
const debounce = (callback: Function, wait: number) => { | ||
return (...args: any[]) => { | ||
clearTimeout(timeout) | ||
timeout = setTimeout(() => { callback(...args) }, wait) | ||
} | ||
} | ||
const debounce = (callback: (...args: unknown[]) => void, wait: number) => { | ||
return (...args: unknown[]) => { | ||
clearTimeout(timeout) | ||
timeout = setTimeout(() => { | ||
callback(...args) | ||
}, wait) | ||
} | ||
} | ||
const handleKeyUpBuscar = async () => { | ||
if (busquedaAutomatica && buscarHabilitado) { | ||
debounce(buscar, 1000)() | ||
} | ||
} | ||
const handleKeyUpBuscar = async () => { | ||
if (busquedaAutomatica && buscarHabilitado) { | ||
debounce(buscar, 1000)() | ||
} | ||
} | ||
</script> | ||
|
||
<div class='titulo'> | ||
<h2>Países</h2> | ||
<div class="titulo"> | ||
<h2>Países</h2> | ||
</div> | ||
<div class='busqueda'> | ||
<input | ||
data-testid='paisBusqueda' | ||
onkeydown={handleKeyDownBuscar} | ||
onkeyup={handleKeyUpBuscar} | ||
bind:value={paisBusqueda} | ||
placeholder='Ingrese un valor para buscar países' | ||
/> | ||
{#if !busquedaAutomatica} | ||
<button data-testid='buscar' onclick={buscar} disabled={!buscarHabilitado}>Buscar</button> | ||
{/if} | ||
<div class="busqueda"> | ||
<input | ||
data-testid="paisBusqueda" | ||
onkeydown={handleKeyDownBuscar} | ||
onkeyup={handleKeyUpBuscar} | ||
bind:value={paisBusqueda} | ||
placeholder="Ingrese un valor para buscar países" | ||
/> | ||
{#if !busquedaAutomatica} | ||
<button data-testid="buscar" onclick={buscar} disabled={!buscarHabilitado}>Buscar</button> | ||
{/if} | ||
</div> | ||
<div class='check'> | ||
<input class='checkbox' type="checkbox" name="busquedaAutomatica" data-testid="busquedaAutomatica" bind:checked={busquedaAutomatica}> | ||
<label for="busquedaAutomatica">Buscar automáticamente</label> | ||
<div class="check"> | ||
<input | ||
class="checkbox" | ||
type="checkbox" | ||
name="busquedaAutomatica" | ||
data-testid="busquedaAutomatica" | ||
bind:checked={busquedaAutomatica} | ||
/> | ||
<label for="busquedaAutomatica">Buscar automáticamente</label> | ||
</div> | ||
<div class='paises'> | ||
{#each paises as pais} | ||
<button | ||
class='pais' | ||
data-testid={`pais-${pais.codigo}`} | ||
onclick={() => goto(`/pais/${pais.codigo}`)} | ||
> | ||
<Bandera bandera={pais.bandera}/> | ||
<div class='nombre_pais'>{pais.nombre}</div> | ||
</button> | ||
{/each} | ||
<div class="paises"> | ||
{#each paises as pais} | ||
<button | ||
class="pais" | ||
data-testid={`pais-${pais.codigo}`} | ||
onclick={() => goto(`/pais/${pais.codigo}`)} | ||
> | ||
<Bandera bandera={pais.bandera} /> | ||
<div class="nombre_pais">{pais.nombre}</div> | ||
</button> | ||
{/each} | ||
</div> |