feat: page redirect

This commit is contained in:
Arman committed 2023-03-06 16:41:16 +01:00
1 parent 6bd79b8d65
commit 00923d8bb5
8 files changed
+102 -54

No files matched your search

+1 -6
View File
@@ -1,5 +1,5 @@
<script lang="ts">
import { goto, invalidate } from '$app/navigation';
import { invalidate } from '$app/navigation';
import type { Dependencies } from '$lib/constants';
import { InputSelect } from '$lib/elements/forms';
import { customPageLimit } from '$lib/stores/layout';
@@ -21,8 +21,6 @@
];
async function limitChange() {
const currentPage = Math.floor(offset / $customPageLimit + 1);
const newPage = Math.floor((offset + 1) / limit);
$customPageLimit = limit;
await Promise.allSettled([
@@ -30,9 +28,6 @@
invalidate(dependency);
})
]);
if (currentPage !== newPage) {
await goto(path + (newPage > 1 ? `/${newPage}` : ``), { replaceState: true });
}
}
</script>
+28 -25
View File
@@ -14,6 +14,7 @@
};
export let columns: Writable<Column[]>;
export let showToggle = true;
const pathname = $page.url.pathname;
let showSelectColumns = false;
@@ -46,7 +47,7 @@
{#if $prefs?.preferredView === 'list'}
<DropList bind:show={showSelectColumns} scrollable={true}>
<Button secondary on:click={() => (showSelectColumns = true)}>
<span class="icon-view-board" />
<span class="icon-view-board" aria-hidden="true" aria-label="columns" />
<span class="text">Columns</span>
<span class="inline-tag">{selectedColumnsNumber}</span>
</Button>
@@ -62,27 +63,29 @@
</DropList>
{/if}
<div class="icon-toggle">
<ul class="icon-toggle-list">
<li class="icon-toggle-item">
<button
class="icon-toggle-button"
aria-label="List View"
type="button"
class:is-selected={$prefs.preferredView === 'list'}
on:click={() => prefs.updatePrefs({ ...$prefs, preferredView: 'list' })}>
<span class="icon-view-list" aria-hidden="true" />
</button>
</li>
<li class="icon-toggle-item">
<button
class="icon-toggle-button"
aria-label="Grid View"
type="button"
class:is-selected={$prefs.preferredView === 'grid'}
on:click={() => prefs.updatePrefs({ ...$prefs, preferredView: 'grid' })}>
<span class="icon-view-grid" aria-hidden="true" />
</button>
</li>
</ul>
</div>
{#if showToggle}
<div class="icon-toggle">
<ul class="icon-toggle-list">
<li class="icon-toggle-item">
<button
class="icon-toggle-button"
aria-label="List View"
type="button"
class:is-selected={$prefs.preferredView === 'list'}
on:click={() => prefs.updatePrefs({ ...$prefs, preferredView: 'list' })}>
<span class="icon-view-list" aria-hidden="true" />
</button>
</li>
<li class="icon-toggle-item">
<button
class="icon-toggle-button"
aria-label="Grid View"
type="button"
class:is-selected={$prefs.preferredView === 'grid'}
on:click={() => prefs.updatePrefs({ ...$prefs, preferredView: 'grid' })}>
<span class="icon-view-grid" aria-hidden="true" />
</button>
</li>
</ul>
</div>
{/if}
+19
View File
@@ -1,7 +1,26 @@
import { goto } from '$app/navigation';
export function pageToOffset(page: number, limit: number): number {
return page ? page * limit - limit : 0;
}
export async function redirectOnOffsetOverflow(
offset: number,
page: number,
total: number,
project: string,
path: string,
search: string
) {
if (offset > total) {
if (page <= 2) {
await goto(`/console/project-${project}/${path}?${search}`);
} else {
await goto(`/console/project-${project}/${path}/${page - 1}?${search}`);
}
}
}
type TabElement = { href: string; title: string; hasChildren?: boolean };
export function isTabSelected(
@@ -1,13 +1,13 @@
import { Query } from '@aw-labs/appwrite-console';
import { sdkForProject } from '$lib/stores/sdk';
import { pageToOffset } from '$lib/helpers/load';
import { pageToOffset, redirectOnOffsetOverflow } from '$lib/helpers/load';
import { CARD_LIMIT, Dependencies } from '$lib/constants';
import type { PageLoad } from './$types';
import { customPageLimit } from '$lib/stores/layout';
import { get } from 'svelte/store';
import { prefs } from '$lib/stores/user';
export const load: PageLoad = async ({ params, parent, depends }) => {
export const load: PageLoad = async ({ params, parent, depends, url }) => {
await parent();
depends(Dependencies.DATABASES);
@@ -16,12 +16,23 @@ export const load: PageLoad = async ({ params, parent, depends }) => {
const offset = pageToOffset(page, limit);
const databases = await sdkForProject.databases.list([
Query.limit(limit),
Query.offset(offset),
Query.orderDesc('$createdAt')
]);
await redirectOnOffsetOverflow(
offset,
page,
databases.total,
params.project,
'databases',
url.search
);
return {
offset,
databases: await sdkForProject.databases.list([
Query.limit(limit),
Query.offset(offset),
Query.orderDesc('$createdAt')
])
databases
};
};
@@ -11,7 +11,7 @@
import GridView from './gridView.svelte';
import TableView from './tableView.svelte';
import { prefs } from '$lib/stores/user';
import { columns } from '../../[[page]]/store';
import { columns } from './store';
export let data: PageData;
let showCreate = false;
@@ -1,6 +1,6 @@
import { Query } from '@aw-labs/appwrite-console';
import { sdkForProject } from '$lib/stores/sdk';
import { pageToOffset } from '$lib/helpers/load';
import { pageToOffset, redirectOnOffsetOverflow } from '$lib/helpers/load';
import { CARD_LIMIT, PAGE_LIMIT } from '$lib/constants';
import type { PageLoad } from './$types';
import { get } from 'svelte/store';
@@ -8,17 +8,28 @@ import { prefs } from '$lib/stores/user';
const limit = get(prefs)?.prefferedView === 'list' ? PAGE_LIMIT : CARD_LIMIT;
export const load: PageLoad = async ({ params, parent }) => {
export const load: PageLoad = async ({ params, parent, url }) => {
await parent();
const page = Number(params.page);
const offset = pageToOffset(page, limit);
const collections = await sdkForProject.databases.listCollections(params.database, [
Query.limit(limit),
Query.offset(offset),
Query.orderDesc('$createdAt')
]);
await redirectOnOffsetOverflow(
offset,
page,
collections.total,
params.project,
`databases/database-${params.database}`,
url.search
);
return {
offset,
collections: await sdkForProject.databases.listCollections(params.database, [
Query.limit(limit),
Query.offset(offset),
Query.orderDesc('$createdAt')
])
collections
};
};
@@ -0,0 +1,15 @@
import { writable } from 'svelte/store';
export type Column = {
id: string;
name: string;
show: boolean;
width?: number;
};
export const columns = writable<Column[]>([
{ id: '$id', name: 'Database ID', show: true, width: 50 },
{ id: 'name', name: 'Name', show: true, width: 120 },
{ id: '$createdAt', name: 'Created', show: true, width: 120 },
{ id: '$updatedAt', name: 'Updated', show: true, width: 120 }
]);
@@ -16,16 +16,10 @@
import { toLocaleDateTime } from '$lib/helpers/date';
import type { Writable } from 'svelte/store';
import type { PageData } from './$types';
import type { Column } from './store';
export let data: PageData;
export let columns: Writable<
{
id: string;
name: string;
width: number;
show: boolean;
}[]
>;
export let columns: Writable<Column[]>;
const project = $page.params.project;
const databaseId = $page.params.database;
</script>