summaryrefslogtreecommitdiffstatshomepage
path: root/frontend/src/routes/worlds/+page.svelte
blob: f223a613c8d657bdcd6d3a0dc6842c2525953caf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<script lang="ts">
	import { deleteWorlds } from '$gql/Mutations';
	import { fetchWorld, worldsQuery } from '$gql/Queries';
	import type { World } from '$gql/graphql';
	import { WorldSortLabel } from '$lib/Enums';
	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';
	import Empty from '$lib/components/Empty.svelte';
	import Guard from '$lib/components/Guard.svelte';
	import Head from '$lib/components/Head.svelte';
	import Cardlets from '$lib/containers/Cardlets.svelte';
	import Column from '$lib/containers/Column.svelte';
	import AddWorld from '$lib/dialogs/AddWorld.svelte';
	import EditWorld from '$lib/dialogs/EditWorld.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';
	import SelectItems from '$lib/toolbar/SelectItems.svelte';
	import SelectSort from '$lib/toolbar/SelectSort.svelte';
	import SelectionControls from '$lib/toolbar/SelectionControls.svelte';
	import Toolbar from '$lib/toolbar/Toolbar.svelte';
	import { getContextClient } from '@urql/svelte';
	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();
	let result = $derived(worldsQuery(client, { ...data }));
	let worlds = $derived($result.data?.worlds);

	let selection = initSelectionContext<World>('World', (a) => a.name);
	$effect(() => {
		if (worlds) {
			selection.view = worlds.edges;
		}
	});

	let filter = $state(new BasicFilterContext(data.filter));
	$effect(() => {
		filter = new BasicFilterContext(data.filter);
	});

	const edit = (id: number) => {
		fetchWorld(client, id)
			.then((world) => modals.open(EditWorld, { world }))
			.catch(toastFinally);
	};
</script>

<Head section="worlds" />

<Column>
	<Toolbar>
		{#snippet start()}
			<SelectionControls>
				<DeleteSelection mutation={deleteWorlds} />
			</SelectionControls>
		{/snippet}
		{#snippet center()}
			<Search name="Worlds" {filter} bind:field={filter.include.name.contains} />
			<SelectSort {sort} labels={WorldSortLabel} />
			<SelectItems {pagination} />
		{/snippet}
		{#snippet end()}
			<AddButton title="Add World" onclick={() => modals.open(AddWorld)} />
		{/snippet}
	</Toolbar>
	{#if worlds}
		<Pagination {pagination} total={worlds.count} />
		<main>
			<Cardlets>
				{#each worlds.edges as { id, name }, index (id)}
					<Selectable {index} {id} {edit}>
						{#snippet children({ onclick, selected })}
							<Cardlet {name} {onclick} filter="worlds" {id}>
								{#snippet overlay()}
									<SelectionOverlay position="right" centered {selected} />
								{/snippet}
							</Cardlet>
						{/snippet}
					</Selectable>
				{:else}
					<Empty />
				{/each}
			</Cardlets>
		</main>
		<Pagination {pagination} total={worlds.count} />
	{:else}
		<Guard {result} />
	{/if}
</Column>