Skip to content

Commit

Permalink
Debugging project pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
allanlasser committed Dec 3, 2024
1 parent 77cf79d commit 5a21dfc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
21 changes: 11 additions & 10 deletions src/routes/(app)/projects/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@
$: query = data.query;
$: projects = data.projects.results;
$: next = data.projects.next;
$: previous = data.projects.previous;
$: next = data.projects.next; // this will be an API url with a cursor
$: previous = data.projects.previous; // this will be an API url with a cursor
function paginate(u: Nullable<URL | string>) {
if (!u) return;
u = new URL(u);
const target = new URL($page.url);
const cursor = u.searchParams.get("cursor");
if (cursor) target.searchParams.set("cursor", cursor);
goto(target);
const pageUrl = new URL(u);
const gotoUrl = new URL($page.url);
// get the cursor out of the pageUrl, pass it to the gotoUrl
const cursor = pageUrl.searchParams.get("cursor");
if (cursor) gotoUrl.searchParams.set("cursor", cursor);
console.log(gotoUrl.toString());
goto(gotoUrl);
}
</script>

Expand Down Expand Up @@ -108,8 +109,8 @@
slot="center"
has_next={Boolean(next)}
has_previous={Boolean(previous)}
on:next={(e) => paginate(next)}
on:previous={(e) => paginate(previous)}
on:next={() => paginate(next)}
on:previous={() => paginate(previous)}
/>
</PageToolbar>
</ContentLayout>
Expand Down
21 changes: 8 additions & 13 deletions src/routes/(app)/projects/+page.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ProjectResults } from "$lib/api/types";
import type { Maybe, ProjectResults } from "$lib/api/types";

import { DEFAULT_PER_PAGE } from "@/config/config.js";
import { getOwned, getShared, list } from "$lib/api/projects";
import { list } from "$lib/api/projects";

type ProjectFilter = "owned" | "shared" | "public";

Expand All @@ -24,36 +24,31 @@ export async function load({ url, parent, fetch }) {
filter = "public";
}

let projects: ProjectResults = {
const defaultProjects: ProjectResults = {
results: [],
next: null,
previous: null,
};

let data: Maybe<ProjectResults>;
let error: unknown;

if (me && filter === "owned") {
({ data: projects = projects, error } = await list(
{ ...params, owned_by_user: true },
fetch,
));
({ data, error } = await list({ ...params, owned_by_user: true }, fetch));
}

if (me && filter === "shared") {
({ data: projects = projects, error } = await list(
{ ...params, is_shared: true },
fetch,
));
({ data, error } = await list({ ...params, is_shared: true }, fetch));
}

if (filter === "public") {
({ data: projects = projects, error } = await list(params, fetch));
({ data, error } = await list(params, fetch));
}

return {
error,
list: filter,
projects,
projects: data ?? defaultProjects,
query,
cursor,
per_page,
Expand Down

0 comments on commit 5a21dfc

Please sign in to comment.