summaryrefslogblamecommitdiffstatshomepage
path: root/frontend/src/routes/circles/+page.svelte
blob: 14b08660d06652b5c34a1013f5929dafd8366005 (plain) (tree)




































































































                                                                                                                                
<script lang="ts">
	import { deleteCircles } from '$gql/Mutations';
	import { circlesQuery, fetchCircle } from '$gql/Queries';
	import type { Circle } from '$gql/graphql';
	import { CircleSortLabel } 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 { 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 AddCircle from '$lib/dialogs/AddCircle.svelte';
	import EditCircle from '$lib/dialogs/EditCircle.svelte';
	import Pagination from '$lib/pagination/Pagination.svelte';
	import Selectable from '$lib/selection/Selectable.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 { openModal } from 'svelte-modals';
	import type { PageData } from './$types';

	const client = getContextClient();
	export let data: PageData;

	$: result = circlesQuery(client, {
		pagination: data.pagination,
		filter: data.filter,
		sort: data.sort
	});

	$: circles = $result.data?.circles;

	const selection = initSelectionContext<Circle>('Circle', (c) => c.name);
	$: if (circles) {
		$selection.view = circles.edges;
		$pagination.total = circles.count;
	}

	const filter = initFilterContext<BasicFilterContext>();
	$: $filter = new BasicFilterContext(data.filter);

	const sort = initSortContext(data.sort, CircleSortLabel);
	$: $sort.update = data.sort;

	const pagination = initPaginationContext();
	$: $pagination.update = data.pagination;

	const edit = (id: number) => {
		fetchCircle(client, id)
			.then((circle) => openModal(EditCircle, { circle }))
			.catch(toastFinally);
	};
</script>

<Head section="circles" />

<Column>
	<Toolbar>
		<SelectionControls slot="start">
			<DeleteSelection mutation={deleteCircles} />
		</SelectionControls>
		<svelte:fragment slot="center">
			<Search name="Circles" bind:field={$filter.include.controls.name.contains} />
			<SelectSort />
			<SelectItems />
		</svelte:fragment>
		<svelte:fragment slot="end">
			<AddButton title="Add Circle" on:click={() => openModal(AddCircle)} />
		</svelte:fragment>
	</Toolbar>
	{#if circles}
		<Pagination />
		<main>
			<Cardlets>
				{#each circles.edges as { id, name }, index (id)}
					<Selectable {index} {id} {edit} let:handle let:selected>
						<Cardlet {name} on:click={handle} filter="circles" {id}>
							<SelectionOverlay slot="overlay" position="right" centered {selected} />
						</Cardlet>
					</Selectable>
				{:else}
					<Empty />
				{/each}
			</Cardlets>
		</main>
		<Pagination />
	{:else}
		<Guard {result} />
	{/if}
</Column>