Add: Post Pagination
This commit is contained in:
parent
22c841396a
commit
6320815f2f
@ -1,9 +1,26 @@
|
|||||||
<script>
|
<script>
|
||||||
import { goto } from '$app/navigation';
|
import { goto, replaceState } from '$app/navigation';
|
||||||
|
import { page } from '$app/stores';
|
||||||
import { getNamedId } from '$lib/util';
|
import { getNamedId } from '$lib/util';
|
||||||
|
import { onMount } from 'svelte';
|
||||||
import Post from '../post/post.svelte';
|
import Post from '../post/post.svelte';
|
||||||
import PostCard from './postcard.svelte';
|
import PostCard from './postcard.svelte';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
export let itemsCount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
export let pageIndex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
export let totalResultCount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {import("$types/base").Post[]}
|
* @type {import("$types/base").Post[]}
|
||||||
*/
|
*/
|
||||||
@ -14,6 +31,19 @@
|
|||||||
*/
|
*/
|
||||||
export let glancePost = null;
|
export let glancePost = null;
|
||||||
|
|
||||||
|
function getDefaultedUrl(/** @type {URL} */ url) {
|
||||||
|
const u = new URL(url);
|
||||||
|
u.searchParams.set('items', String(itemsCount));
|
||||||
|
u.searchParams.set('page', String(pageIndex));
|
||||||
|
return u;
|
||||||
|
}
|
||||||
|
|
||||||
|
const newUrl = getDefaultedUrl($page.url)
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
replaceState(newUrl, {});
|
||||||
|
});
|
||||||
|
|
||||||
/** @type {boolean[]} */
|
/** @type {boolean[]} */
|
||||||
$: hidden = Array(posts.length).fill(false);
|
$: hidden = Array(posts.length).fill(false);
|
||||||
|
|
||||||
@ -25,7 +55,8 @@
|
|||||||
hidden[index] = p == post;
|
hidden[index] = p == post;
|
||||||
});
|
});
|
||||||
|
|
||||||
goto(`/posts?glance=${post.id}`, {
|
newUrl.searchParams.set('glance', String(post.id));
|
||||||
|
goto(newUrl, {
|
||||||
replaceState: true,
|
replaceState: true,
|
||||||
noScroll: true
|
noScroll: true
|
||||||
});
|
});
|
||||||
@ -36,7 +67,8 @@
|
|||||||
|
|
||||||
hidden = Array(posts.length).fill(false);
|
hidden = Array(posts.length).fill(false);
|
||||||
|
|
||||||
goto(`/posts`, {
|
newUrl.searchParams.delete('glance');
|
||||||
|
goto(newUrl, {
|
||||||
replaceState: true,
|
replaceState: true,
|
||||||
noScroll: true
|
noScroll: true
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,17 +1,30 @@
|
|||||||
import { getPost, getPosts } from '$lib/server/db/post';
|
import { getPost, getPostCount, getPosts } from '$lib/server/db/post';
|
||||||
|
import { parseIntNull } from '$lib/util';
|
||||||
|
|
||||||
|
|
||||||
/** @type {import('./$types').PageServerLoad} */
|
/** @type {import('./$types').PageServerLoad} */
|
||||||
export async function load({ url }) {
|
export async function load({ url }) {
|
||||||
const result = await getPosts();
|
const count = await getPostCount();
|
||||||
|
|
||||||
const glance = url.searchParams.get('glance');
|
const page = parseIntNull(url.searchParams.get('page')) ?? 0;
|
||||||
const glanceID = glance ? parseInt(glance) : null
|
const items = parseIntNull(url.searchParams.get('items')) ?? 10;
|
||||||
|
|
||||||
const glancePost = glanceID ? await getPost(glanceID, { withMetrics: true }) : null;
|
const pageSize = Math.min(Math.max(items, 1), 30);
|
||||||
|
|
||||||
|
const result = await getPosts({
|
||||||
|
limit: pageSize,
|
||||||
|
offset: pageSize * page,
|
||||||
|
});
|
||||||
|
|
||||||
|
const glance = parseIntNull(url.searchParams.get('glance'));
|
||||||
|
|
||||||
|
const glancePost = glance ? await getPost(glance, { withMetrics: true }) : null;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
posts: result,
|
posts: result,
|
||||||
glancePost: glancePost
|
glancePost: glancePost,
|
||||||
|
itemsCount: pageSize,
|
||||||
|
pageIndex: page,
|
||||||
|
totalResultCount: count,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,15 @@
|
|||||||
<script>
|
<script>
|
||||||
|
import { page } from "$app/stores";
|
||||||
import Card from "$comp/card.svelte";
|
import Card from "$comp/card.svelte";
|
||||||
import Postlist from "$comp/page/posts/postlist.svelte";
|
import Postlist from "$comp/page/posts/postlist.svelte";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {{
|
* @type {{
|
||||||
* posts: import("$types/base").Post[],
|
* posts: import("$types/base").Post[],
|
||||||
* glancePost: import("$types/base").Post | null
|
* glancePost: import("$types/base").Post | null,
|
||||||
|
* itemsCount: number,
|
||||||
|
* pageIndex: number,
|
||||||
|
* totalResultCount: number,
|
||||||
* }}
|
* }}
|
||||||
*/
|
*/
|
||||||
export let data;
|
export let data;
|
||||||
@ -29,5 +33,5 @@
|
|||||||
<img class="icon" src="waving-hand-sign.png" alt="wave">
|
<img class="icon" src="waving-hand-sign.png" alt="wave">
|
||||||
<span>Welcome to ECHO</span>
|
<span>Welcome to ECHO</span>
|
||||||
</Card>
|
</Card>
|
||||||
<Postlist posts={data.posts} glancePost={data.glancePost}></Postlist>
|
<Postlist posts={data.posts} glancePost={data.glancePost} itemsCount={data.itemsCount} pageIndex={data.pageIndex} totalResultCount={data.totalResultCount}></Postlist>
|
||||||
</main>
|
</main>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user