forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cursor-based pagination for comments using "Load more" button (d…
…enoland#438) Simpler alternative to denoland#429 Towards denoland#414 CC @mbhrznr
- Loading branch information
Showing
6 changed files
with
82 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import { useSignal } from "@preact/signals"; | ||
import { useEffect } from "preact/hooks"; | ||
import { Comment } from "@/utils/db.ts"; | ||
import UserPostedAt from "@/components/UserPostedAt.tsx"; | ||
import { LINK_STYLES } from "@/utils/constants.ts"; | ||
|
||
async function fetchComments(itemId: string, cursor: string) { | ||
let url = `/api/items/${itemId}/comments`; | ||
if (cursor !== "" && cursor !== undefined) url += "?cursor=" + cursor; | ||
const resp = await fetch(url); | ||
if (!resp.ok) throw new Error(`Request failed: GET ${url}`); | ||
return await resp.json() as { comments: Comment[]; cursor: string }; | ||
} | ||
|
||
function CommentSummary(props: Comment) { | ||
return ( | ||
<div class="py-4"> | ||
<UserPostedAt {...props} /> | ||
<p>{props.text}</p> | ||
</div> | ||
); | ||
} | ||
|
||
export default function CommentsList(props: { itemId: string }) { | ||
const commentsSig = useSignal<Comment[]>([]); | ||
const cursorSig = useSignal(""); | ||
const isLoadingSig = useSignal(false); | ||
|
||
async function loadMoreComments() { | ||
isLoadingSig.value = true; | ||
try { | ||
const { comments, cursor } = await fetchComments( | ||
props.itemId, | ||
cursorSig.value, | ||
); | ||
commentsSig.value = [...commentsSig.value, ...comments]; | ||
cursorSig.value = cursor; | ||
} catch (error) { | ||
console.log(error.message); | ||
} finally { | ||
isLoadingSig.value = false; | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
loadMoreComments(); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
{commentsSig.value.map((comment) => ( | ||
<CommentSummary key={comment.id} {...comment} /> | ||
))} | ||
{cursorSig.value !== "" && !isLoadingSig.value && ( | ||
<button onClick={loadMoreComments} class={LINK_STYLES}> | ||
Load more | ||
</button> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters