diff --git a/src/app/blog/[slug]/page.tsx b/src/app/blog/[slug]/page.tsx index 9832ef1..a9f862d 100644 --- a/src/app/blog/[slug]/page.tsx +++ b/src/app/blog/[slug]/page.tsx @@ -1,18 +1,5 @@ import Link from "next/link"; -// const getPost = async (slug: string): Promise => { -// const postRequest = await fetch(`https://cat-fact.herokuapp.com/facts/`); -// if (!postRequest.ok) return { id: "-1", title: "Error", content: "Failed to fetch post" }; -// const facts: Array = await postRequest.json(); -// const fact = facts.find((fact) => { -// return fact._id === slug; -// }); - -// if (!fact) return { id: "-1", title: "Error", content: "Failed to fetch post" }; -// const post = { id: fact._id, title: fact.user, content: fact.text }; -// return post; -// }; - const getPost = async (slug: string): Promise => { const postRequest = await fetch(`https://api.tombaker.me/v1/posts/${slug}`); if (!postRequest.ok) return null; diff --git a/src/app/blog/page.tsx b/src/app/blog/page.tsx index 9f6f619..0175b45 100644 --- a/src/app/blog/page.tsx +++ b/src/app/blog/page.tsx @@ -10,7 +10,7 @@ const PAGE_SIZE = 3; type Post = InferSelectModel; -const getLocalPosts = async ({ currentPage }: { currentPage: number }): Promise> => { +const getPosts = async ({ currentPage }: { currentPage: number }): Promise> => { const context = getRequestContext(); const db = drizzle(context.env.D1DATA); @@ -26,48 +26,23 @@ const getLocalPosts = async ({ currentPage }: { currentPage: number }): Promise< return results.map((result) => ({ ...result, total: total.count })); }; -const getPosts = async (): Promise> => { - const startTime = Date.now(); - const postRequest = await fetch("https://api.tombaker.me/v1/posts"); - if (!postRequest.ok) return []; - - const { data: posts, executionTime, source }: SonicResponse> = await postRequest.json(); - - const endTime = Date.now(); - posts[0].title += - " (fetched from SonicJS in " + (endTime - startTime) + `ms via ${source}, execution time: ${executionTime}ms)`; - return posts; -}; - const Page = async ({ searchParams }: { searchParams?: { page?: string } }) => { - // const posts = await getPosts(); - // console.log("Posts: ", posts); - const currentPage = Number(searchParams?.page) || 1; - console.log(searchParams, currentPage); - const localPosts = await getLocalPosts({ currentPage }); + const posts = await getPosts({ currentPage }); - const total = localPosts[0]?.total || 0; + const total = posts?.[0]?.total || 0; const totalPages = Math.ceil(total / PAGE_SIZE); return (

Tom's Blog

- {/* {posts.map((post: Post) => ( -
- -

{post.title}

- -
-
- ))} */} - {localPosts.map((post: Post) => ( + {posts.map((post: Post) => (

{post.title}

-
}} /> +

{post.summary}

))}
diff --git a/src/server/posts.ts b/src/server/posts.ts index 2ce37be..e59e976 100644 --- a/src/server/posts.ts +++ b/src/server/posts.ts @@ -13,6 +13,7 @@ export const tableName = "posts"; export const definition = { id: text("id").primaryKey(), title: text("title"), + summary: text("summary"), body: text("body"), userId: text("userId"), image: text("image"),