Skip to content

Commit

Permalink
Clean up code, add summary field
Browse files Browse the repository at this point in the history
  • Loading branch information
tombakerjr committed Jul 29, 2024
1 parent c409493 commit 8f155ad
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 43 deletions.
13 changes: 0 additions & 13 deletions src/app/blog/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
import Link from "next/link";

// const getPost = async (slug: string): Promise<Post> => {
// 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<Fact> = 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<Post | null> => {
const postRequest = await fetch(`https://api.tombaker.me/v1/posts/${slug}`);
if (!postRequest.ok) return null;
Expand Down
35 changes: 5 additions & 30 deletions src/app/blog/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const PAGE_SIZE = 3;

type Post = InferSelectModel<typeof postsTable>;

const getLocalPosts = async ({ currentPage }: { currentPage: number }): Promise<Array<Post & { total: number }>> => {
const getPosts = async ({ currentPage }: { currentPage: number }): Promise<Array<Post & { total: number }>> => {
const context = getRequestContext();
const db = drizzle(context.env.D1DATA);

Expand All @@ -26,48 +26,23 @@ const getLocalPosts = async ({ currentPage }: { currentPage: number }): Promise<
return results.map((result) => ({ ...result, total: total.count }));
};

const getPosts = async (): Promise<Array<Post>> => {
const startTime = Date.now();
const postRequest = await fetch("https://api.tombaker.me/v1/posts");
if (!postRequest.ok) return [];

const { data: posts, executionTime, source }: SonicResponse<Array<Post>> = 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 (
<div>
<h1 className="mb-4 border-b border-gray-100 dark:border-gray-600 pb-6">Tom&apos;s Blog</h1>
{/* {posts.map((post: Post) => (
<div key={post.id} className="mt-6">
<Link href={`/blog/${post.id}`}>
<h2 className="text-xl font-bold">{post.title}</h2>
</Link>
<div dangerouslySetInnerHTML={{ __html: post.body }} />
</div>
))} */}
{localPosts.map((post: Post) => (
{posts.map((post: Post) => (
<div key={post.id} className="mt-6">
<Link href={`/blog/${post.id}`}>
<h2 className="text-xl font-bold">{post.title}</h2>
</Link>
<div dangerouslySetInnerHTML={{ __html: post.body ?? <div /> }} />
<p>{post.summary}</p>
</div>
))}
<div className="mt-6">
Expand Down
1 change: 1 addition & 0 deletions src/server/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 8f155ad

Please sign in to comment.