-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pagination on the item page for comments + time-ago filter (#257)
* adding time-ago filter for items * simplify code for item page * creating CommentInput element * pagination for comments in item page * extract page selector * better * time ago selector to the top --------- Co-authored-by: Asher Gomez <[email protected]>
- Loading branch information
1 parent
5e19532
commit 0a80be0
Showing
6 changed files
with
158 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
export default function PageSelector( | ||
props: { currentPage: number; lastPage: number; timeSelector?: string }, | ||
) { | ||
return ( | ||
<div class="flex justify-center py-4 mx-auto"> | ||
<form class="inline-flex items-center gap-x-2"> | ||
{props.timeSelector && | ||
<input type="hidden" name="time-ago" value={props.timeSelector} />} | ||
<input | ||
id="current_page" | ||
class={`bg-transparent rounded rounded-lg outline-none w-full border-1 border-gray-500 hover:border-black transition duration-300 disabled:(opacity-50 cursor-not-allowed) rounded-md px-2 py-1 dark:(hover:border-white)`} | ||
type="number" | ||
name="page" | ||
min="1" | ||
max={props.lastPage} | ||
value={props.currentPage} | ||
// @ts-ignore: this is valid HTML | ||
onchange="this.form.submit()" | ||
/> | ||
<label for="current_page" class="whitespace-nowrap align-middle"> | ||
of {props.lastPage} | ||
</label> | ||
</form> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
// This should not exceed 10 since denoKV Kv.getMany can't handle as input an array with more than 10 elements | ||
export const PAGE_LENGTH = 10; | ||
|
||
export function calcPageNum(url: URL) { | ||
return parseInt(url.searchParams.get("page") || "1"); | ||
} | ||
|
||
export function calcLastPage(total = 0, pageLength = PAGE_LENGTH): number { | ||
return Math.ceil(total / pageLength); | ||
} |
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,23 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import { calcLastPage, calcPageNum } from "./pagination.ts"; | ||
import { assertEquals } from "std/testing/asserts.ts"; | ||
|
||
Deno.test("[pagination] calcPageNum()", () => { | ||
assertEquals(calcPageNum(new URL("https://saaskit.deno.dev/")), 1); | ||
assertEquals(calcPageNum(new URL("https://saaskit.deno.dev/?page=2")), 2); | ||
assertEquals( | ||
calcPageNum(new URL("https://saaskit.deno.dev/?time-ago=month")), | ||
1, | ||
); | ||
assertEquals( | ||
calcPageNum(new URL("https://saaskit.deno.dev/?time-ago=month&page=3")), | ||
3, | ||
); | ||
}); | ||
|
||
Deno.test("[pagination] calcLastPage()", () => { | ||
assertEquals(calcLastPage(1, 10), 1); | ||
assertEquals(calcLastPage(15, 10), 2); | ||
assertEquals(calcLastPage(11, 20), 1); | ||
assertEquals(calcLastPage(50, 20), 3); | ||
}); |