From d8c1f15c91e5a53dbfac7e8337f20df7c6e663cb Mon Sep 17 00:00:00 2001 From: Max Schmidt Date: Mon, 15 May 2023 17:38:17 +0200 Subject: [PATCH] HS Signed-off-by: Max Schmidt --- astro/src/components/RichText.astro | 2 +- astro/src/pages/index.astro | 7 ++---- astro/src/pages/posts/[id].astro | 35 ++++++++++++++++------------- astro/src/utils/helpers.ts | 6 +---- astro/src/utils/payload.ts | 7 ++++++ payload/src/collections/Posts.ts | 2 ++ 6 files changed, 33 insertions(+), 26 deletions(-) create mode 100644 astro/src/utils/payload.ts diff --git a/astro/src/components/RichText.astro b/astro/src/components/RichText.astro index 4d5f9ec..d54e1a5 100644 --- a/astro/src/components/RichText.astro +++ b/astro/src/components/RichText.astro @@ -9,7 +9,7 @@ const contentArray = getContentArray(content); { contentArray.map((value) => { if (typeof value === "string") { - return
; + return
; } else { return ( console.log(post.title)); +const posts = await getPosts(); --- diff --git a/astro/src/pages/posts/[id].astro b/astro/src/pages/posts/[id].astro index 23e342c..d834169 100644 --- a/astro/src/pages/posts/[id].astro +++ b/astro/src/pages/posts/[id].astro @@ -1,26 +1,31 @@ --- import Layout from "../../layouts/Layout.astro"; -import type { Post } from "../../types"; - import RichText from "../../components/RichText.astro"; +import type { Post } from "../../types"; +import { getPost, getPosts } from "../../utils/payload"; + export async function getStaticPaths() { - const paths = ( - await (await fetch("http://payload:3001/api/posts")).json() - ).docs.map((post: Post) => ({ + const posts = await getPosts(); + const paths = posts.map((post: Post) => ({ params: { id: post.id }, })); return paths; } + const { id } = Astro.params; -const post = (await ( - await fetch(`http://payload:3001/api/posts/${id}`) -).json()) as Post; +const post = id && (await getPost(id)); --- - -
- BACK -

{post.title}

- {post.content && } -
-
+{ + post ? ( + +
+ BACK +

{post.title}

+ {post.content && } +
+
+ ) : ( +
404
+ ) +} diff --git a/astro/src/utils/helpers.ts b/astro/src/utils/helpers.ts index 650638c..391d90f 100644 --- a/astro/src/utils/helpers.ts +++ b/astro/src/utils/helpers.ts @@ -1,8 +1,4 @@ -import { - payloadSlateToDomConfig, - SlateToDomConfig, - slateToHtml, -} from "slate-serializers"; +import { payloadSlateToDomConfig, slateToHtml } from "slate-serializers"; import { Element } from "domhandler"; export const getContentArray = (content: any) => { diff --git a/astro/src/utils/payload.ts b/astro/src/utils/payload.ts new file mode 100644 index 0000000..57efda9 --- /dev/null +++ b/astro/src/utils/payload.ts @@ -0,0 +1,7 @@ +import type { Post } from "../types"; + +export const getPosts = async () => + (await (await fetch("http://payload:3001/api/posts")).json()).docs as Post[]; + +export const getPost = async (id: string) => + (await (await fetch(`http://payload:3001/api/posts/${id}`)).json()) as Post; diff --git a/payload/src/collections/Posts.ts b/payload/src/collections/Posts.ts index 8efa59f..835c5c9 100644 --- a/payload/src/collections/Posts.ts +++ b/payload/src/collections/Posts.ts @@ -23,6 +23,8 @@ const Posts: CollectionConfig = { name: "content", type: "richText", admin: { + elements: ["h2", "h3", "h4", "link", "ol", "ul", "upload"], + leaves: ["bold", "italic", "underline"], upload: { collections: { media: {