Skip to content

Commit 918affc

Browse files
committed
svelte-pokedex: add filter method to grid
1 parent 20e9c10 commit 918affc

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

pokedex-app/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ I keep asking myself, "Who would come up with that and think it was a good idea?
103103
104104
This doesn't work btw. Still struggling to fetch my `id` param from the URL. It seems like they completely revamped the API for this, all the Stackoverflow posts dated older than ~ 6 months are useless.
105105
106+
More highly esotheric syntax for observing variable changes: (https://stackoverflow.com/questions/56983938/in-svelte-how-to-console-logyes-when-a-variable-changed)[https://stackoverflow.com/questions/56983938/in-svelte-how-to-console-logyes-when-a-variable-changed]
106107
107-
Time to implement: ~ 1h
108+
109+
Time to implement: ~ 2.5h
108110
109111
Breakdown:
110-
- App Setup and UI: 1h
112+
- App Setup and UI: 2.5h
111113
- Testing Setup: 0h
112114
- CI/CD: 0h

pokedex-app/svelte-pokedex/src/components/Grid.svelte

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,26 @@
1212
let searchValue = '';
1313
let page = 0;
1414
15-
onMount(async function () {
15+
async function fetchData() {
1616
const response = await fetch(
1717
`https://pokeapi.co/api/v2/pokemon?limit=${FETCH_LIMIT}&offset=${page * FETCH_LIMIT}`
1818
);
1919
const res = await response.json();
2020
data = [...data, ...res.results];
21-
console.log('data', data);
21+
}
22+
23+
onMount(async function () {
24+
fetchData();
2225
});
26+
27+
$: if (page) {
28+
fetchData();
29+
}
30+
31+
$: if (searchValue || data) {
32+
const filteredCollection = data.filter((item) => item.name.includes(searchValue));
33+
displayData = filteredCollection;
34+
}
2335
</script>
2436

2537
<div class="grid-container">
@@ -33,12 +45,14 @@
3345
/>
3446
</div>
3547
<div class="grid-wrapper">
36-
{#each data as item}
48+
{#each displayData as item}
3749
{@const postId = getPostIdFromUrl(item.url)}
3850
<a href={`detail/${getPostIdFromUrl(item.url)}`}>
3951
<ItemCard name={item.name} postId={getPostIdFromUrl(item.url)} />
4052
</a>
4153
{/each}
4254
</div>
43-
<button class="load-more button is-primary is-fullwidth"> Load More </button>
55+
<button class="load-more button is-primary is-fullwidth" on:click={() => page++}>
56+
Load More
57+
</button>
4458
</div>

0 commit comments

Comments
 (0)