Skip to content

Commit 34780b3

Browse files
committed
Add solution
1 parent dcd673e commit 34780b3

File tree

6 files changed

+75
-4
lines changed

6 files changed

+75
-4
lines changed

src/$lib/client/pokemon.svelte

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script lang="ts">
2+
import { onMount } from 'svelte';
3+
import { fetchPokemon, type Pokemon } from './pokeClient';
4+
export let id: string;
5+
let pokemonFetch: Promise<Pokemon>;
6+
onMount(() => {
7+
pokemonFetch = fetchPokemon(id);
8+
});
9+
</script>
10+
11+
<div class="flex flex-col items-center">
12+
{#if pokemonFetch !== undefined}
13+
{#await pokemonFetch}
14+
<h2>Loading...</h2>
15+
{:then pokemon}
16+
<img height={200} width={200} src={pokemon.sprites.front_default} alt={pokemon.name} />
17+
<div>
18+
Name: {pokemon.name.charAt(0).toUpperCase() + pokemon.name.slice(1)}
19+
</div>
20+
<div>Weight: {pokemon.weight}</div>
21+
{/await}
22+
{/if}
23+
</div>
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
let id = (Math.random() * 1000).toFixed();
3+
</script>
4+
5+
<a href={`/pokemon/${id}`}> Random Pokemon </a>

src/routes/+layout.svelte

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<script>
2+
import RandomPokemonLink from '../$lib/client/randomPokemonLink.svelte';
23
import '../app.css';
34
</script>
45

5-
<slot />
6+
<div class="flex min-h-screen w-full flex-col items-center p-16 text-white bg-black">
7+
<RandomPokemonLink />
8+
9+
<div class="p-8"><slot /></div>
10+
</div>

src/routes/+page.svelte

-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
<h1 class="text-3xl font-bold underline">Hello world!</h1>
2-
3-
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { createPost, readPosts } from '../../../$lib/server/sbClient';
2+
import type { Actions, PageServerLoad } from './$types';
3+
4+
export const load = (async ({ params }) => {
5+
return { id: params.id, posts: await readPosts(params.id).then((data) => data.data ?? []) };
6+
}) satisfies PageServerLoad;
7+
8+
export const actions = {
9+
default: async (event) => {
10+
const formData = await event.request.formData();
11+
createPost(event.params.id, formData.get('content')!.toString());
12+
}
13+
} satisfies Actions;

src/routes/pokemon/[id]/+page.svelte

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script lang="ts">
2+
import Pokemon from '../../../$lib/client/pokemon.svelte';
3+
import type { PageData } from './$types';
4+
import { invalidate } from '$app/navigation';
5+
import RandomPokemonLink from '../../../$lib/client/randomPokemonLink.svelte';
6+
7+
export let data: PageData;
8+
const invalidateRoute = () => invalidate(`/pokemon/${data.id}`);
9+
</script>
10+
11+
<Pokemon id={data.id} />
12+
<div class="p-8 flex flex-col items-center gap-1">
13+
<h2 class="text-2xl">Comments</h2>
14+
<div class="p-8">
15+
{#each data.posts as post}
16+
<article>
17+
<span>{post.content}</span>
18+
</article>
19+
{/each}
20+
<div />
21+
</div>
22+
</div>
23+
24+
<form class="grid justify-items-center grid-flow-row" method="post" on:submit={invalidateRoute}>
25+
<label for="content" class="text-xl"> Comment this Pokemon </label>
26+
<textarea id="content" name="content" class="ml-1 text-black" />
27+
<input type="submit" />
28+
</form>

0 commit comments

Comments
 (0)