Skip to content

Commit

Permalink
Remove trpc from article/[slug]
Browse files Browse the repository at this point in the history
  • Loading branch information
NiallJoeMaher committed Dec 28, 2023
1 parent c7b1b31 commit 08d5365
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 44 deletions.
12 changes: 4 additions & 8 deletions app/articles/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ import ArticleMenu from "@/components/ArticleMenu/ArticleMenu";
import { headers } from "next/headers";
import { notFound } from "next/navigation";
import { getServerAuthSession } from "@/server/auth";
import { api } from "@/server/trpc/server";
import ArticleAdminPanel from "@/components/ArticleAdminPanel/ArticleAdminPanel";
import { type Metadata } from "next";
import { getPost } from "@/server/lib/posts";

type Props = { params: { slug: string } };

export async function generateMetadata({ params }: Props): Promise<Metadata> {
const slug = params.slug;

const post = await api.post.bySlug.query({
slug,
});
const post = await getPost({ slug });

// Might revisit to give more defaults
const tags = post?.tags.map((tag) => tag.tag.title);
Expand Down Expand Up @@ -57,9 +55,7 @@ const ArticlePage = async ({ params }: Props) => {

const host = headers().get("host") || "";

const post = await api.post.bySlug.query({
slug,
});
const post = await getPost({ slug });

if (!post) {
notFound();
Expand Down Expand Up @@ -101,7 +97,7 @@ const ArticlePage = async ({ params }: Props) => {
<div className="mx-auto max-w-3xl px-2 pb-4 sm:px-4">
<BioBar author={post.user} />
{post.showComments ? (
<CommentsArea postId={post.id} postOwnerId={post.userId} />
<CommentsArea postId={post.id} postOwnerId={post.user.id} />
) : (
<h3 className="py-10 text-lg italic">
Comments are disabled for this post
Expand Down
33 changes: 0 additions & 33 deletions server/api/router/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,39 +397,6 @@ export const postRouter = createTRPCRouter({

return currentPost;
}),
bySlug: publicProcedure
.input(GetSinglePostSchema)
.query(async ({ input, ctx }) => {
const post = await ctx.db.post.findUnique({
where: {
slug: input.slug,
},
include: {
user: {
select: {
username: true,
name: true,
image: true,
bio: true,
},
},
tags: {
select: {
id: true,
tag: {
select: {
title: true,
},
},
},
},
},
});
if (!post) {
return null;
}
return post;
}),
myBookmarks: protectedProcedure.query(async ({ ctx }) => {
const response = await ctx.db.bookmark.findMany({
where: {
Expand Down
69 changes: 66 additions & 3 deletions server/lib/posts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,76 @@
import db from "@/server/db/client";
import * as Sentry from "@sentry/nextjs";
import "server-only";
import { z } from "zod";

type GetTrending = {
currentUserId?: string;
};
export const GetPostSchema = z.object({
slug: z.string(),
});

export const GetTrendingSchema = z.object({
currentUserId: z.string(),
});

type GetPost = z.infer<typeof GetPostSchema>;
type GetTrending = z.infer<typeof GetTrendingSchema>;

export async function getPost({ slug }: GetPost) {
try {
GetPostSchema.parse({ slug });

const response = await db.post.findUnique({
where: { slug },
select: {
id: true,
title: true,
body: true,
published: true,
updatedAt: true,
readTimeMins: true,
slug: true,
excerpt: true,
canonicalUrl: true,
showComments: true,
user: {
select: {
name: true,
image: true,
username: true,
bio: true,
id: true,
},
},
bookmarks: {
select: { userId: true },
},
tags: {
select: {
id: true,
tag: {
select: {
title: true,
},
},
},
},
},
});

if (!response || response.published === null) {
return null;
}
const currentUserLikesPost = !!response.bookmarks.length;
response.bookmarks = [];
return { ...response, currentUserLikesPost };
} catch (error) {
Sentry.captureException(error);
throw new Error("Error fetching post");
}
}

export async function getTrending({ currentUserId }: GetTrending) {
try {
GetTrendingSchema.parse({ currentUserId });
const TRENDING_COUNT = 5;

const response = await db.post.findMany({
Expand Down

0 comments on commit 08d5365

Please sign in to comment.