From dc4db405d2991d3ec6a114f3b08d3fccd057d3ee Mon Sep 17 00:00:00 2001
From: Wolfgang Müller
Date: Thu, 13 Feb 2025 17:52:16 +0100
Subject: frontend: Migrate to Svelte 5

---
 frontend/src/routes/characters/+page.svelte | 88 ++++++++++++++---------------
 1 file changed, 43 insertions(+), 45 deletions(-)

(limited to 'frontend/src/routes/characters/+page.svelte')

diff --git a/frontend/src/routes/characters/+page.svelte b/frontend/src/routes/characters/+page.svelte
index 0934bab..04c72cb 100644
--- a/frontend/src/routes/characters/+page.svelte
+++ b/frontend/src/routes/characters/+page.svelte
@@ -3,10 +3,7 @@
 	import { charactersQuery, fetchCharacter } from '$gql/Queries';
 	import type { Character } from '$gql/graphql';
 	import { CharacterSortLabel } from '$lib/Enums';
-	import { BasicFilterContext, initFilterContext } from '$lib/Filter';
-	import { initPaginationContext } from '$lib/Pagination';
-	import { initSelectionContext } from '$lib/Selection';
-	import { initSortContext } from '$lib/Sort';
+	import { BasicFilterContext } from '$lib/Filter.svelte';
 	import { toastFinally } from '$lib/Toasts';
 	import AddButton from '$lib/components/AddButton.svelte';
 	import Cardlet from '$lib/components/Cardlet.svelte';
@@ -19,6 +16,7 @@
 	import EditCharacter from '$lib/dialogs/EditCharacter.svelte';
 	import Pagination from '$lib/pagination/Pagination.svelte';
 	import Selectable from '$lib/selection/Selectable.svelte';
+	import { initSelectionContext } from '$lib/selection/Selection.svelte';
 	import SelectionOverlay from '$lib/selection/SelectionOverlay.svelte';
 	import DeleteSelection from '$lib/toolbar/DeleteSelection.svelte';
 	import Search from '$lib/toolbar/Search.svelte';
@@ -27,38 +25,32 @@
 	import SelectionControls from '$lib/toolbar/SelectionControls.svelte';
 	import Toolbar from '$lib/toolbar/Toolbar.svelte';
 	import { getContextClient } from '@urql/svelte';
-	import { openModal } from 'svelte-modals';
-	import type { PageData } from './$types';
+	import { modals } from 'svelte-modals';
+	import type { PageProps } from './$types';
+
+	let { data }: PageProps = $props();
+	let pagination = $derived(data.pagination);
+	let sort = $derived(data.sort);
 
 	const client = getContextClient();
-	export let data: PageData;
+	let result = $derived(charactersQuery(client, { ...data }));
+	let characters = $derived($result.data?.characters);
 
-	$: result = charactersQuery(client, {
-		pagination: data.pagination,
-		filter: data.filter,
-		sort: data.sort
+	let selection = initSelectionContext<Character>('Character', (a) => a.name);
+	$effect(() => {
+		if (characters) {
+			selection.view = characters.edges;
+		}
 	});
 
-	$: characters = $result.data?.characters;
-
-	const selection = initSelectionContext<Character>('Character', (c) => c.name);
-	$: if (characters) {
-		$selection.view = characters.edges;
-		$pagination.total = characters.count;
-	}
-
-	const filter = initFilterContext<BasicFilterContext>();
-	$: $filter = new BasicFilterContext(data.filter);
-
-	const sort = initSortContext(data.sort, CharacterSortLabel);
-	$: $sort.update = data.sort;
-
-	const pagination = initPaginationContext();
-	$: $pagination.update = data.pagination;
+	let filter = $state(new BasicFilterContext(data.filter));
+	$effect(() => {
+		filter = new BasicFilterContext(data.filter);
+	});
 
 	const edit = (id: number) => {
 		fetchCharacter(client, id)
-			.then((character) => openModal(EditCharacter, { character }))
+			.then((character) => modals.open(EditCharacter, { character }))
 			.catch(toastFinally);
 	};
 </script>
@@ -67,34 +59,40 @@
 
 <Column>
 	<Toolbar>
-		<SelectionControls slot="start">
-			<DeleteSelection mutation={deleteCharacters} />
-		</SelectionControls>
-		<svelte:fragment slot="center">
-			<Search name="Characters" bind:field={$filter.include.controls.name.contains} />
-			<SelectSort />
-			<SelectItems />
-		</svelte:fragment>
-		<svelte:fragment slot="end">
-			<AddButton title="Add Character" on:click={() => openModal(AddCharacter)} />
-		</svelte:fragment>
+		{#snippet start()}
+			<SelectionControls>
+				<DeleteSelection mutation={deleteCharacters} />
+			</SelectionControls>
+		{/snippet}
+		{#snippet center()}
+			<Search name="Characters" {filter} bind:field={filter.include.name.contains} />
+			<SelectSort {sort} labels={CharacterSortLabel} />
+			<SelectItems {pagination} />
+		{/snippet}
+		{#snippet end()}
+			<AddButton title="Add Character" onclick={() => modals.open(AddCharacter)} />
+		{/snippet}
 	</Toolbar>
 	{#if characters}
-		<Pagination />
+		<Pagination {pagination} total={characters.count} />
 		<main>
 			<Cardlets>
 				{#each characters.edges as { id, name }, index (id)}
-					<Selectable {index} {id} {edit} let:handle let:selected>
-						<Cardlet {name} on:click={handle} filter="characters" {id}>
-							<SelectionOverlay slot="overlay" position="right" centered {selected} />
-						</Cardlet>
+					<Selectable {index} {id} {edit}>
+						{#snippet children({ onclick, selected })}
+							<Cardlet {name} {onclick} filter="characters" {id}>
+								{#snippet overlay()}
+									<SelectionOverlay position="right" centered {selected} />
+								{/snippet}
+							</Cardlet>
+						{/snippet}
 					</Selectable>
 				{:else}
 					<Empty />
 				{/each}
 			</Cardlets>
 		</main>
-		<Pagination />
+		<Pagination {pagination} total={characters.count} />
 	{:else}
 		<Guard {result} />
 	{/if}
-- 
cgit v1.2.3-2-gb3c3

