-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kawtar Choubari
committed
Nov 1, 2023
1 parent
3a682ba
commit 897876d
Showing
12 changed files
with
358 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { NextResponse } from "next/server"; | ||
import { SupabaseAdmin } from "@/lib/supabase"; | ||
|
||
export async function POST( | ||
req: Request, | ||
{ params }: { params: { slug: string } } | ||
) { | ||
try { | ||
await SupabaseAdmin.rpc("increment_post_view", { post_slug: params.slug }); | ||
|
||
return NextResponse.json({ | ||
message: `Successfully incremented post: ${params.slug}`, | ||
}); | ||
} catch (error) { | ||
return NextResponse.json({ error }); | ||
} | ||
} | ||
|
||
export async function GET( | ||
req: Request, | ||
{ params }: { params: { slug: string } } | ||
) { | ||
const { data } = await SupabaseAdmin.from("posts") | ||
.select("view_count") | ||
.filter("slug", "eq", params.slug); | ||
|
||
if (data) | ||
return NextResponse.json({ | ||
total: data[0]?.view_count || null, | ||
}); | ||
return NextResponse.json({ | ||
message: "Unsupported Request", | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { countFormatter } from "@/lib/utils"; | ||
import { FaEye } from "react-icons/fa"; | ||
|
||
interface PostViewsProps { | ||
slug: string; | ||
} | ||
|
||
export default async function PostViews({ slug }: PostViewsProps) { | ||
const response = await fetch( | ||
`${process.env.NEXT_PUBLIC_APP_URL}/api/views/${slug}` | ||
); | ||
const data = await response.json(); | ||
|
||
return ( | ||
<div className="text-sm mb-2 opacity-60 flex items-center"> | ||
<FaEye className="inline-block mr-2 text-base" /> | ||
<p className="italic"> | ||
{data?.total && `${countFormatter(data.total)} views`} | ||
</p> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Link from "next/link"; | ||
import Image from "next/image"; | ||
import { format, parseISO } from "date-fns"; | ||
import { Post } from "contentlayer/generated"; | ||
import { FaCalendar } from "react-icons/fa"; | ||
import PostViews from "./mdx/post-views"; | ||
|
||
export default function PostCard(post: Post) { | ||
return ( | ||
<article | ||
key={post._id} | ||
className="mb-8 shadow-lg rounded-lg dark:bg-dark bg-lighter transform transition-all duration-300 ease-in-out hover:shadow-md hover:shadow-accent" | ||
> | ||
<Link href={post.slug}> | ||
{post.image && ( | ||
<Image | ||
src={post.image} | ||
placeholder="blur" | ||
blurDataURL="/blog/placeholder.jpg" | ||
alt={post.title} | ||
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 75vw, 50vw" | ||
width={804} | ||
height={452} | ||
className="rounded-md border bg-muted transition-colors" | ||
/> | ||
)} | ||
<div className="px-5 pt-2 pb-5"> | ||
<h2 className="text-2xl font-serif font-bold text-accent py-2"> | ||
{post.title} | ||
</h2> | ||
<div className="flex gap-5"> | ||
{post.date && ( | ||
<p className="text-sm mb-2 opacity-60 flex items-center"> | ||
<FaCalendar className="inline-block mr-2" /> | ||
<time dateTime={post.date} className="italic"> | ||
{format(parseISO(post.date), "LLLL d, yyyy")} | ||
</time> | ||
</p> | ||
)} | ||
{/* @ts-expect-error Async Server Component */} | ||
<PostViews slug={post.slugAsParams} /> | ||
</div> | ||
|
||
{post.description && ( | ||
<p className="text-muted-foreground">{post.description}</p> | ||
)} | ||
</div> | ||
</Link> | ||
</article> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createClient } from "@supabase/supabase-js"; | ||
|
||
const supabaseUrl: string = process.env.SUPABASE_URL || ""; | ||
const supabaseServerKey: string = process.env.SUPABASE_SERVICE_KEY || ""; | ||
|
||
const SupabaseAdmin = createClient(supabaseUrl, supabaseServerKey); | ||
|
||
export { SupabaseAdmin }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.