Add blog post view

main
trivernis 5 months ago
parent b178c786f8
commit 1e11512b21
Signed by: Trivernis
GPG Key ID: 7E6D18B61C8D2F4B

@ -17,6 +17,10 @@
} }
} }
.page {
position: relative;
}
.flex-row { .flex-row {
display: flex; display: flex;
flex-direction: row; flex-direction: row;

@ -23,6 +23,12 @@ export type BlogPostTeaser = {
}; };
}; };
export type BlogPost = BlogPostTeaser & {
attributes: {
content: any;
};
};
export async function getPosts(locale = "all"): Promise<BlogPostTeaser[]> { export async function getPosts(locale = "all"): Promise<BlogPostTeaser[]> {
return await fetchApi<BlogPostTeaser[]>({ return await fetchApi<BlogPostTeaser[]>({
endpoint: "blog-posts", endpoint: "blog-posts",
@ -46,3 +52,33 @@ export async function getPosts(locale = "all"): Promise<BlogPostTeaser[]> {
}, },
}); });
} }
export async function getPost(slug: string): Promise<BlogPost> {
return await fetchApi<BlogPost>({
endpoint: "blog-posts",
wrappedByKey: "data",
wrappedByList: true,
query: {
populate: {
author: {
populate: ["slug", "name"],
},
collection: {
populate: ["slug", "name"],
},
tags: {
populate: ["slug", "name"],
},
teaserImage: {
populate: "*",
},
},
filters: {
slug: {
$eq: slug,
},
},
locale: "all",
},
});
}

@ -4,6 +4,7 @@
import Footer from "../components/organisms/Footer.svelte"; import Footer from "../components/organisms/Footer.svelte";
</script> </script>
<div class="page">
<Header /> <Header />
<div class="content"> <div class="content">
<slot /> <slot />
@ -11,6 +12,7 @@
<div class="footer-position"> <div class="footer-position">
<Footer /> <Footer />
</div> </div>
</div>
<style lang="scss"> <style lang="scss">
@import "$lib/vars.scss"; @import "$lib/vars.scss";
@ -18,6 +20,16 @@
@layer default, layout; @layer default, layout;
@layer layout { @layer layout {
.page {
position: absolute;
top: 0;
left: 0;
width: 100%;
margin: 0;
min-height: 100%;
display: flex;
flex-direction: column;
}
.content { .content {
margin: 0 20%; margin: 0 20%;
transition-duration: 1s; transition-duration: 1s;
@ -35,8 +47,7 @@
.footer-position { .footer-position {
margin: 0; margin: 0;
width: 100%; width: 100%;
position: absolute; margin-top: auto;
bottom: 0;
} }
} }
</style> </style>

@ -8,6 +8,7 @@ export const load: PageLoad = async ({ params }: { params: unknown }) => {
return { return {
posts, posts,
error: undefined,
}; };
} catch (err: any) { } catch (err: any) {
console.error(err); console.error(err);

@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import type { PageData } from "../$types"; import type { PageData } from "./$types";
import Error from "../../components/molecules/Error.svelte"; import Error from "../../components/molecules/Error.svelte";
import BlogPostTeaser from "../../components/organisms/BlogPostTeaser.svelte"; import BlogPostTeaser from "../../components/organisms/BlogPostTeaser.svelte";

@ -0,0 +1,26 @@
import { getPost, getPosts } from "$lib/cms/blog";
import { error } from "@sveltejs/kit";
import type { PageLoad } from "./$types";
export const load: PageLoad = async ({
params,
}: { params: { slug: string } }) => {
try {
const slug = params.slug;
const post = await getPost(slug);
return {
post,
error: undefined,
};
} catch (err: any) {
console.error(err);
return {
posts: [],
error: {
message: "Could not load blog post :(",
code: 500,
},
};
}
};

@ -0,0 +1,12 @@
<script lang="ts">
import Error from "../../../components/molecules/Error.svelte";
import type { PageData } from "./$types";
export let data: PageData;
</script>
<code>{JSON.stringify(data.post, null, 2)}</code>
{#if data.error}
<Error error={data.error} />
{/if}
Loading…
Cancel
Save