add layout

This commit is contained in:
Hamcha 2023-07-13 15:02:58 +02:00
parent ce5c3e33ab
commit 7b9b766dd8
Signed by: hamcha
GPG Key ID: 1669C533B8CF6D89
6 changed files with 39 additions and 22 deletions

View File

@ -4,6 +4,7 @@ export interface Site {
owner_display_name: string;
title: string;
description: string | null;
collections: { slug: string; name: string }[];
created_at: string;
modified_at: string | null;
deleted_at: string | null;

View File

@ -0,0 +1,8 @@
<script lang="ts">
import type { LayoutData } from './$types';
export let data: LayoutData;
</script>
<header><h2>{data.site.title}</h2></header>
<slot />

16
src/routes/+layout.ts Normal file
View File

@ -0,0 +1,16 @@
import { PUBLIC_API_BASE } from '$env/static/public';
import type { Site } from '$lib/mabel-types';
import { getSiteName } from '$lib/url';
import { error } from '@sveltejs/kit';
import type { LayoutLoad } from './$types';
export const load = (async ({ url }) => {
const site = getSiteName(url.hostname);
const siteData = await fetch(`${PUBLIC_API_BASE}/sites/${site}`);
if (siteData.status === 404) throw error(404, 'Not Found');
return {
site: (await siteData.json()) as Site
};
}) satisfies LayoutLoad;

View File

@ -1,20 +0,0 @@
import { error } from '@sveltejs/kit';
import type { PageLoad } from './$types';
import { PUBLIC_API_BASE } from '$env/static/public';
import { getSiteName } from '$lib/url';
import type { Post, Site } from '$lib/mabel-types';
export const load = (async ({ url, params }) => {
const site = getSiteName(url.hostname);
const siteData = await fetch(`${PUBLIC_API_BASE}/sites/${site}`);
if (siteData.status === 404) throw error(404, 'Not Found');
const pageData = await fetch(`${PUBLIC_API_BASE}/posts/${site}/${params.slug}`);
if (pageData.status === 404) throw error(404, 'Not Found');
return {
site: (await siteData.json()) as Site,
page: (await pageData.json()) as Post
};
}) satisfies PageLoad;

View File

@ -8,7 +8,5 @@
<title>{data.site.title} - {data.page.title}</title>
</svelte:head>
<header><h2>{data.site.title}</h2></header>
<h1>{data.page.title}</h1>
by {data.page.author_display_name}

View File

@ -0,0 +1,14 @@
import { error } from '@sveltejs/kit';
import type { PageLoad } from './$types';
import { PUBLIC_API_BASE } from '$env/static/public';
import type { Post } from '$lib/mabel-types';
export const load = (async ({ params, parent }) => {
const { site } = await parent();
const pageData = await fetch(`${PUBLIC_API_BASE}/posts/${site.name}/${params.slug}`);
if (pageData.status === 404) throw error(404, 'Not Found');
return {
page: (await pageData.json()) as Post
};
}) satisfies PageLoad;