Skip to content

Commit

Permalink
Load notes with document, catch some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeseast committed Apr 25, 2024
1 parent 6a44e7c commit 8071e5b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/lib/api/addons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function getAddons(
params: AddOnParams = {},
fetch = globalThis.fetch,
): Promise<Page<AddOnListItem>> {
const endpoint = new URL("addons", BASE_API_URL);
const endpoint = new URL("addons/", BASE_API_URL);
Object.entries(params).forEach(([key, value]) => {
endpoint.searchParams.set(key, String(value));
});
Expand Down
18 changes: 16 additions & 2 deletions src/lib/api/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,24 @@ export async function get(
fetch: typeof globalThis.fetch = globalThis.fetch,
): Promise<Document> {
const endpoint = new URL(`documents/${id}.json`, BASE_API_URL);
const expand = ["user", "organization", "projects", "revisions", "sections"];
const expand = [
"user",
"organization",
"projects",
"revisions",
"sections",
"notes",
];
endpoint.searchParams.set("expand", expand.join(","));

const resp = await fetch(endpoint, { credentials: "include" });
const resp = await fetch(endpoint, { credentials: "include" }).catch(
console.error,
);

// backend error, not much we can do
if (!resp) {
error(500);
}

if (isErrorCode(resp.status)) {
error(resp.status, resp.statusText);
Expand Down
13 changes: 9 additions & 4 deletions src/routes/documents/[id]-[slug]/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* We do this in a layout module because sub-routes can use the same
* document without loading it again.
*/

import { redirect } from "@sveltejs/kit";
import * as documents from "@/lib/api/documents";
import type { Document } from "@/lib/api/types";

import { redirect, error } from "@sveltejs/kit";
import * as documents from "$lib/api/documents";
import { getPinnedAddons } from "$lib/api/addons";
import { breadcrumbTrail } from "$lib/utils/navigation";

Expand All @@ -16,7 +16,12 @@ function documentPath(document: Document) {

/** @type {import('./$types').PageLoad} */
export async function load({ fetch, params, parent }) {
const document = await documents.get(+params.id, fetch);
const document = await documents.get(+params.id, fetch).catch(console.error);

console.log({ document });
if (!document) {
error(404, "Document not found");
}

if (document.slug !== params.slug) {
const canonical = new URL(document.canonical_url);
Expand Down
10 changes: 0 additions & 10 deletions src/routes/documents/[id]-[slug]/+page.ts

This file was deleted.

0 comments on commit 8071e5b

Please sign in to comment.