Add page number in query string

This commit is contained in:
LittleSheep 2024-02-15 18:37:27 +08:00
parent 520ab738f9
commit 34cb534342
2 changed files with 45 additions and 21 deletions

View File

@ -1,11 +1,12 @@
import { createSignal, Show } from "solid-js";
import { createEffect, createSignal, Show } from "solid-js";
import { useParams } from "@solidjs/router";
import { useSearchParams } from "@solidjs/router";
import { createStore } from "solid-js/store";
import { closeModel, openModel } from "../scripts/modals.ts";
import PostList from "../components/posts/PostList.tsx";
import NameCard from "../components/NameCard.tsx";
import PostPublish from "../components/posts/PostPublish.tsx";
import { createStore } from "solid-js/store";
import { closeModel, openModel } from "../scripts/modals.ts";
export default function AccountPage() {
const [error, setError] = createSignal<string | null>(null);
@ -13,15 +14,23 @@ export default function AccountPage() {
const [page, setPage] = createSignal(0);
const [info, setInfo] = createSignal<any>(null);
const [searchParams, setSearchParams] = useSearchParams();
const params = useParams();
createEffect(() => {
setPage(parseInt(searchParams["page"] ?? "1"));
}, [searchParams]);
async function readPosts(pn?: number) {
if (pn) setPage(pn);
const res = await fetch("/api/posts?" + new URLSearchParams({
take: (10).toString(),
offset: ((page() - 1) * 10).toString(),
authorId: params["accountId"]
}));
if (pn) setSearchParams({ page: pn });
const res = await fetch(
"/api/posts?" +
new URLSearchParams({
take: searchParams["take"] ? searchParams["take"] : (10).toString(),
offset: searchParams["offset"] ? searchParams["offset"] : ((page() - 1) * 10).toString(),
authorId: params["accountId"],
}),
);
if (res.status !== 200) {
setError(await res.text());
} else {
@ -34,7 +43,7 @@ export default function AccountPage() {
const meta: { [id: string]: any } = {
reposting: null,
replying: null,
editing: null
editing: null,
};
meta[field] = data;
setPublishMeta(meta);
@ -46,7 +55,7 @@ export default function AccountPage() {
const [publishMeta, setPublishMeta] = createStore<any>({
replying: null,
reposting: null,
editing: null
editing: null,
});
return (
@ -54,10 +63,18 @@ export default function AccountPage() {
<div id="alerts">
<Show when={error()}>
<div role="alert" class="alert alert-error">
<svg xmlns="http://www.w3.org/2000/svg" class="stroke-current shrink-0 h-6 w-6" fill="none"
viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" />
<svg
xmlns="http://www.w3.org/2000/svg"
class="stroke-current shrink-0 h-6 w-6"
fill="none"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
<span class="capitalize">{error()}</span>
</div>
@ -89,4 +106,4 @@ export default function AccountPage() {
/>
</>
);
}
}

View File

@ -1,8 +1,9 @@
import { createSignal, Show } from "solid-js";
import { createEffect, createSignal, Show } from "solid-js";
import { createStore } from "solid-js/store";
import { useSearchParams } from "@solidjs/router";
import PostList from "../components/posts/PostList.tsx";
import PostPublish from "../components/posts/PostPublish.tsx";
import { createStore } from "solid-js/store";
export default function DashboardPage() {
const [error, setError] = createSignal<string | null>(null);
@ -10,13 +11,19 @@ export default function DashboardPage() {
const [page, setPage] = createSignal(0);
const [info, setInfo] = createSignal<any>(null);
const [searchParams, setSearchParams] = useSearchParams();
createEffect(() => {
setPage(parseInt(searchParams["page"] ?? "1"));
}, [searchParams]);
async function readPosts(pn?: number) {
if (pn) setPage(pn);
if (pn) setSearchParams({ page: pn });
const res = await fetch(
"/api/posts?" +
new URLSearchParams({
take: (10).toString(),
offset: ((page() - 1) * 10).toString(),
take: searchParams["take"] ? searchParams["take"] : (10).toString(),
offset: searchParams["offset"] ? searchParams["offset"] : ((page() - 1) * 10).toString(),
reply: false.toString(),
}),
);