fix: add pageination to user profiles

This commit is contained in:
Xyphyn committed 2023-07-27 11:52:09 -07:00
1 parent ef0cb48710
commit 8e75e05585
3 files changed
+59 -44

No files matched your search

+3
View File
@@ -6,3 +6,6 @@ export const isMutable = (post: PostView, me: PersonView) =>
export const isCommentMutable = (comment: CommentView, me: PersonView) => export const isCommentMutable = (comment: CommentView, me: PersonView) =>
(me.person.admin && comment.comment.local) || (me.person.admin && comment.comment.local) ||
me.person.id == comment.creator.id me.person.id == comment.creator.id
export const isComment = (item: CommentView | PostView): item is CommentView =>
'comment' in item
+29 -38
View File
@@ -21,83 +21,74 @@
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import Button from '$lib/components/input/Button.svelte' import Button from '$lib/components/input/Button.svelte'
import CommentItem from '$lib/components/lemmy/comment/CommentItem.svelte' import CommentItem from '$lib/components/lemmy/comment/CommentItem.svelte'
import Pageination from '$lib/components/ui/Pageination.svelte'
import { page } from '$app/stores'
import { goto } from '$app/navigation'
import { isComment } from '$lib/components/lemmy/post/helpers.js'
export let data: { export let data
user: GetPersonDetailsResponse
}
const isComment = (item: CommentView | PostView): item is CommentView =>
'comment' in item
function getDateOfItem(item: CommentView | PostView): number {
if (isComment(item)) {
return Date.parse(item.comment.published)
} else {
return Date.parse(item.post.published)
}
}
const items = [...data.user.comments, ...data.user.posts].sort(
(a, b) => getDateOfItem(b) - getDateOfItem(a)
)
</script> </script>
<svelte:head> <svelte:head>
<title>{data.user.person_view.person.name}</title> <title>{data.person_view.person.name}</title>
</svelte:head> </svelte:head>
<div class="flex flex-col-reverse lg:flex-row gap-4 max-w-full w-full"> <div class="flex flex-col-reverse lg:flex-row gap-4 max-w-full w-full">
<div class="flex flex-col gap-4 max-w-full w-full"> <div class="flex flex-col gap-4 max-w-full w-full">
{#each items as item} {#each data.items as item}
{#if isComment(item)} {#if isComment(item) && (data.type == 'all' || data.type == 'comments')}
<CommentItem comment={item} /> <CommentItem comment={item} />
{:else} {:else if !isComment(item) && (data.type == 'all' || data.type == 'posts')}
<Post post={item} /> <Post post={item} />
{/if} {/if}
{/each} {/each}
<Pageination
page={data.page}
on:change={(p) => {
$page.url.searchParams.set('page', p.detail.toString())
goto($page.url.toString(), {
invalidateAll: true,
})
}}
/>
</div> </div>
<div class="max-w-sm w-full mx-auto"> <div class="max-w-sm w-full mx-auto">
<StickyCard class="w-full"> <StickyCard class="w-full">
<Avatar <Avatar
width={64} width={64}
url={data.user.person_view.person.avatar} url={data.person_view.person.avatar}
alt={data.user.person_view.person.name} alt={data.person_view.person.name}
/> />
<span class="flex flex-row items-center gap-1 text-sm"> <span class="flex flex-row items-center gap-1 text-sm">
<Icon src={Calendar} width={16} height={16} mini /> <Icon src={Calendar} width={16} height={16} mini />
<span class="capitalize"> <span class="capitalize">
<RelativeDate <RelativeDate date={new Date(data.person_view.person.published)} />
date={new Date(data.user.person_view.person.published)}
/>
</span> </span>
</span> </span>
<div class="text-sm flex flex-row flex-wrap gap-3"> <div class="text-sm flex flex-row flex-wrap gap-3">
<span class="flex flex-row items-center gap-1"> <span class="flex flex-row items-center gap-1">
<Icon src={PencilSquare} width={16} height={16} mini /> <Icon src={PencilSquare} width={16} height={16} mini />
<FormattedNumber number={data.user.person_view.counts.post_count} /> <FormattedNumber number={data.person_view.counts.post_count} />
</span> </span>
<span class="flex flex-row items-center gap-1"> <span class="flex flex-row items-center gap-1">
<Icon src={ChatBubbleOvalLeftEllipsis} width={16} height={16} mini /> <Icon src={ChatBubbleOvalLeftEllipsis} width={16} height={16} mini />
<FormattedNumber <FormattedNumber number={data.person_view.counts.comment_count} />
number={data.user.person_view.counts.comment_count}
/>
</span> </span>
</div> </div>
<div> <div>
<h1 class="font-bold text-lg"> <h1 class="font-bold text-lg">
{data.user.person_view.person.display_name ?? {data.person_view.person.display_name ?? data.person_view.person.name}
data.user.person_view.person.name}
</h1> </h1>
<span class="text-sm opacity-80 flex flex-row gap-0"> <span class="text-sm opacity-80 flex flex-row gap-0">
{#if data.user.person_view.person.display_name} {#if data.person_view.person.display_name}
{data.user.person_view.person.name}@ {data.person_view.person.name}@
{/if} {/if}
{new URL(data.user.person_view.person.actor_id).hostname} {new URL(data.person_view.person.actor_id).hostname}
</span> </span>
</div> </div>
{#if data.user.person_view.person.bio} {#if data.person_view.person.bio}
<Markdown source={data.user.person_view.person.bio} /> <Markdown source={data.person_view.person.bio} />
{/if} {/if}
</StickyCard> </StickyCard>
</div> </div>
+27 -6
View File
@@ -1,14 +1,35 @@
import { isComment } from '$lib/components/lemmy/post/helpers.js'
import { authData, getClient } from '$lib/lemmy.js' import { authData, getClient } from '$lib/lemmy.js'
import type { CommentView, PostView } from 'lemmy-js-client'
import { get } from 'svelte/store' import { get } from 'svelte/store'
export async function load({ params }) { function getDateOfItem(item: CommentView | PostView): number {
return { if (isComment(item)) {
user: await getClient().getPersonDetails({ return Date.parse(item.comment.published)
limit: 40, } else {
page: 1, return Date.parse(item.post.published)
}
}
export async function load({ params, url }) {
const page = Number(url.searchParams.get('page')) || 1
const type: 'comments' | 'posts' | 'all' =
(url.searchParams.get('type') as 'comments' | 'posts' | 'all') || 'all'
const user = await getClient().getPersonDetails({
limit: 20,
page: page,
username: params.name, username: params.name,
sort: 'New', sort: 'New',
auth: get(authData)?.token, auth: get(authData)?.token,
}), })
return {
type: type,
page: page,
person_view: user.person_view,
items: [...user.posts, ...user.comments].sort(
(a, b) => getDateOfItem(b) - getDateOfItem(a)
),
} }
} }