fix: template pagination

This commit is contained in:
Arman committed 2025-04-01 10:59:41 +02:00
1 parent 654f544806
commit 3b686664f2
4 files changed
+93 -120

No files matched your search

+31 -60
View File
@@ -1,32 +1,36 @@
<script lang="ts">
import { IconChevronLeft, IconChevronRight } from '@appwrite.io/pink-icons-svelte';
import { Button, Icon, Layout } from '@appwrite.io/pink-svelte';
import { Button, Icon, Layout, Pagination } from '@appwrite.io/pink-svelte';
import { createEventDispatcher } from 'svelte';
export let sum: number;
export let total: number;
export let limit: number;
export let offset: number;
export let hidePages = false;
const dispatch = createEventDispatcher();
$: totalPages = Math.ceil(sum / limit);
$: totalPages = Math.ceil(total / limit);
$: currentPage = Math.floor(offset / limit + 1);
$: pages = pagination(currentPage, totalPages);
function handleOptionClick(page: number) {
if (currentPage !== page) {
offset = limit * (page - 1);
function handleOptionClick(e: CustomEvent) {
if (currentPage !== e.detail) {
offset = limit * (e.detail - 1);
dispatch('change');
}
}
function handleButtonPage(direction: string) {
if (direction === 'next' && currentPage < totalPages) {
offset = limit * (currentPage - 1);
function next() {
if (currentPage < totalPages) {
offset = limit * currentPage;
dispatch('change');
} else if (direction === 'prev' && currentPage > 1) {
offset = limit * (currentPage - 1);
}
}
function prev() {
if (currentPage > 1) {
offset = limit * (currentPage - 2);
dispatch('change');
}
}
@@ -49,66 +53,33 @@
}
</script>
{#if totalPages > 1}
<Layout.Stack direction="row">
{#if !hidePages}
<Pagination
{limit}
page={currentPage}
{total}
type="button"
on:next={next}
on:page={handleOptionClick}
on:prev={prev} />
{:else}
<Layout.Stack direction="row" inline>
<Button.Button
size="s"
variant="compact"
on:click={() => handleButtonPage('prev')}
disabled={currentPage <= 1}>
on:click={prev}
disabled={currentPage <= 1 || totalPages <= 1}>
<Icon icon={IconChevronLeft} slot="start" />
Prev
</Button.Button>
{#if !hidePages}
<ol class="pagination-list is-only-desktop">
{#each pages as page}
{#if typeof page === 'number'}
<li class="pagination-item">
<button
type="button"
class="button"
on:click={() => handleOptionClick(+page)}
class:is-disabled={currentPage === page}
class:is-text={currentPage !== page}
aria-label="page">
<span class="text">{page}</span>
</button>
</li>
{:else}
<li class="li is-text">
<span class="icon">...</span>
</li>
{/if}
{/each}
</ol>
{/if}
<Button.Button
size="s"
variant="compact"
on:click={() => handleButtonPage('next')}
disabled={currentPage === totalPages}>
on:click={next}
disabled={currentPage === totalPages || totalPages <= 1}>
<Icon icon={IconChevronRight} slot="end" />
Next
</Button.Button>
</Layout.Stack>
{:else}
<nav class="pagination">
<button type="button" class="button is-text is-disabled" aria-label="prev page">
<span class="icon-cheveron-left" aria-hidden="true"></span>
<span class="text">Prev</span>
</button>
{#if !hidePages}
<ol class="pagination-list is-only-desktop">
<li class="pagination-item">
<button type="button" class="button is-disabled" aria-label="page">
<span class="text">1</span>
</button>
</li>
</ol>
{/if}
<button type="button" class="button is-text is-disabled" aria-label="next page">
<span class="text">Next</span>
<span class="icon-cheveron-right" aria-hidden="true"></span>
</button>
</nav>
{/if}
+22 -12
View File
@@ -1,15 +1,22 @@
<script lang="ts">
import { Layout, Typography } from '@appwrite.io/pink-svelte';
import PaginationInline from './paginationInline.svelte';
import Limit from './limit.svelte';
export let items = [];
export let limit = 5;
export let hideFooter = false;
let {
items = [],
limit = $bindable(5),
hideFooter = false,
hidePages = true,
hasLimit = false,
name = 'items'
} = $props();
let offset = 0;
let total = $derived(items.length);
$: total = items?.length;
$: paginatedItems = items.slice(offset, offset + limit);
let offset = $state(0);
let paginatedItems = $derived(items.slice(offset, offset + limit));
</script>
<Layout.Stack gap="s">
@@ -17,12 +24,15 @@
{#if !hideFooter}
<Layout.Stack direction="row" justifyContent="space-between" alignItems="center">
<Typography.Text variant="m-400" color="--fgcolor-neutral-secondary">
Total results: {total}
</Typography.Text>
<div>
<PaginationInline {limit} bind:offset sum={total} hidePages />
</div>
{#if hasLimit}
<Limit bind:limit sum={total} {name} />
{:else}
<Typography.Text variant="m-400" color="--fgcolor-neutral-secondary">
Total results: {total}
</Typography.Text>
{/if}
<PaginationInline {limit} bind:offset {total} {hidePages} />
</Layout.Stack>
{/if}
</Layout.Stack>
@@ -1,6 +1,6 @@
<script lang="ts">
import { base } from '$app/paths';
import { EmptySearch, PaginationWithLimit, SvgIcon } from '$lib/components';
import { EmptySearch, PaginationWithLimit, Paginator, SvgIcon } from '$lib/components';
import { Button, InputSearch } from '$lib/elements/forms';
import { page } from '$app/stores';
import Wizard from '$lib/layout/wizard.svelte';
@@ -132,37 +132,44 @@
</svelte:fragment>
<Layout.Stack gap="l">
{#if data.templates?.length > 0}
<Layout.Grid columns={3} columnsXS={2} columnsXXS={1}>
{#each data.templates as template}
{@const templateFrameworks = template.frameworks.map((t) => t.name)}
<Paginator
items={data.templates}
let:paginatedItems
limit={12}
hidePages={false}
hasLimit>
<Layout.Grid columns={3} columnsXS={2} columnsXXS={1}>
{#each paginatedItems as template}
{@const templateFrameworks = template.frameworks.map((t) => t.name)}
<Card.Link
variant="secondary"
href={`${base}/project-${$page.params.project}/sites/create-site/templates/template-${template.key}`}
padding="xxs">
<Card.Media
title={template.name}
src={$app.themeInUse === 'dark'
? template?.screenshotDark ||
`${base}/images/sites/screenshot-placeholder-dark.svg`
: template?.screenshotLight ||
`${base}/images/sites/screenshot-placeholder-light.svg`}
alt={template.name}
avatar>
<svelte:fragment slot="avatar">
<Tooltip>
<SvgIcon
name={getFrameworkIcon(templateFrameworks[0])}
iconSize="small" />
<svelte:fragment slot="tooltip">
{capitalize(templateFrameworks[0])}
</svelte:fragment>
</Tooltip>
</svelte:fragment>
</Card.Media>
</Card.Link>
{/each}
</Layout.Grid>
<Card.Link
variant="secondary"
href={`${base}/project-${$page.params.project}/sites/create-site/templates/template-${template.key}`}
padding="xxs">
<Card.Media
title={template.name}
src={$app.themeInUse === 'dark'
? template?.screenshotDark ||
`${base}/images/sites/screenshot-placeholder-dark.svg`
: template?.screenshotLight ||
`${base}/images/sites/screenshot-placeholder-light.svg`}
alt={template.name}
avatar>
<svelte:fragment slot="avatar">
<Tooltip>
<SvgIcon
name={getFrameworkIcon(templateFrameworks[0])}
iconSize="small" />
<svelte:fragment slot="tooltip">
{capitalize(templateFrameworks[0])}
</svelte:fragment>
</Tooltip>
</svelte:fragment>
</Card.Media>
</Card.Link>
{/each}
</Layout.Grid>
</Paginator>
{:else}
<EmptySearch
hidePagination
@@ -174,11 +181,5 @@
>Clear search</Button>
</EmptySearch>
{/if}
<PaginationWithLimit
name="Templates"
limit={data.limit}
offset={data.offset}
total={data.sum} />
</Layout.Stack>
</Wizard>
@@ -1,13 +1,8 @@
import { sdk } from '$lib/stores/sdk';
import { getPage, getSearch, getView, pageToOffset, View } from '$lib/helpers/load';
import { getLimit } from '$lib/helpers/load';
import { getSearch, getView, View } from '$lib/helpers/load';
export const load = async ({ url, route }) => {
const limit = getLimit(url, route, 12);
const page = getPage(url);
const search = getSearch(url);
const view = getView(url, route, View.Grid);
const offset = pageToOffset(page, limit);
const filter = {
useCases: url.searchParams.getAll('useCase'),
frameworks: url.searchParams.getAll('framework')
@@ -51,13 +46,9 @@ export const load = async ({ url, route }) => {
});
return {
offset,
limit,
view,
filter,
frameworks,
useCases,
sum: templates.length,
templates: templates.splice(((page === 0 ? 1 : page) - 1) * limit, limit)
templates
};
};