mirror of
https://github.com/r-freeman/portfolio.git
synced 2025-04-18 20:44:46 +00:00
Render comments on server
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m18s
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m18s
This commit is contained in:
parent
fd33bcced9
commit
f8d668e4f3
@ -1,32 +0,0 @@
|
|||||||
import {createClient} from '@/lib/supabase/server'
|
|
||||||
import {NextResponse} from 'next/server'
|
|
||||||
|
|
||||||
export async function GET(request: Request, {params}: { params: Promise<{ slug: string }> }) {
|
|
||||||
const {slug} = await params
|
|
||||||
if (typeof slug !== 'undefined') {
|
|
||||||
try {
|
|
||||||
const supabase = await createClient()
|
|
||||||
const {data: comments, error} = await supabase
|
|
||||||
.from('comments')
|
|
||||||
.select(`
|
|
||||||
id,
|
|
||||||
content,
|
|
||||||
published,
|
|
||||||
created_at,
|
|
||||||
user:users!inner(id, name, image),
|
|
||||||
article:articles!inner(id, title, slug)
|
|
||||||
`)
|
|
||||||
.eq('article.slug', slug)
|
|
||||||
.eq('published', true)
|
|
||||||
.order('created_at', {ascending: false})
|
|
||||||
|
|
||||||
if (comments !== null && comments?.length > 0) {
|
|
||||||
return NextResponse.json({comments: comments})
|
|
||||||
}
|
|
||||||
return NextResponse.json([])
|
|
||||||
} catch (e) {
|
|
||||||
return new Response(JSON.stringify({status: 'Internal Server Error'}), {status: 500})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new Response(JSON.stringify({status: 'Not Found'}), {status: 404})
|
|
||||||
}
|
|
@ -8,6 +8,7 @@ import {formatDate} from '@/lib/formatDate'
|
|||||||
import ArticleNav from '@/components/ui/ArticleNav'
|
import ArticleNav from '@/components/ui/ArticleNav'
|
||||||
import Comments from '@/components/ui/Comments'
|
import Comments from '@/components/ui/Comments'
|
||||||
import {getAllArticles} from '@/lib/getAllArticles'
|
import {getAllArticles} from '@/lib/getAllArticles'
|
||||||
|
import {getComments} from '@/lib/getComments'
|
||||||
|
|
||||||
type ArticleLayout = {
|
type ArticleLayout = {
|
||||||
title: string
|
title: string
|
||||||
@ -50,6 +51,7 @@ export async function ArticleLayout({
|
|||||||
slug,
|
slug,
|
||||||
children
|
children
|
||||||
}: ArticleLayout) {
|
}: ArticleLayout) {
|
||||||
|
const comments = await getComments(slug)
|
||||||
const articles = await getAllArticles(false)
|
const articles = await getAllArticles(false)
|
||||||
const {prev, next} = findAdjacentArticles(articles, slug)
|
const {prev, next} = findAdjacentArticles(articles, slug)
|
||||||
|
|
||||||
@ -81,7 +83,7 @@ export async function ArticleLayout({
|
|||||||
</header>
|
</header>
|
||||||
<Prose className="mt-8" data-mdx-content>{children}</Prose>
|
<Prose className="mt-8" data-mdx-content>{children}</Prose>
|
||||||
</article>
|
</article>
|
||||||
<Comments slug={slug}/>
|
<Comments slug={slug} comments={comments}/>
|
||||||
<ArticleNav prev={prev} next={next}/>
|
<ArticleNav prev={prev} next={next}/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -3,9 +3,7 @@
|
|||||||
import React, {ReactNode, useActionState} from 'react'
|
import React, {ReactNode, useActionState} from 'react'
|
||||||
import {useSession} from 'next-auth/react'
|
import {useSession} from 'next-auth/react'
|
||||||
import Image from 'next/image'
|
import Image from 'next/image'
|
||||||
import useSWR from 'swr'
|
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import fetcher from '@/lib/fetcher'
|
|
||||||
import {formatDate} from '@/lib/formatDate'
|
import {formatDate} from '@/lib/formatDate'
|
||||||
import {addComment, loginWithGitHub} from '@/app/actions/comments'
|
import {addComment, loginWithGitHub} from '@/app/actions/comments'
|
||||||
import {Button} from '@/components/ui/Button'
|
import {Button} from '@/components/ui/Button'
|
||||||
@ -65,7 +63,7 @@ Comments.Status = function Status({children}: CommentsStatusProps) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<p aria-live="polite" role="status"
|
<p aria-live="polite" role="status"
|
||||||
className={clsx('text-base font-semibold',
|
className={clsx('text-sm font-semibold',
|
||||||
!isError ? 'text-green-800 dark:text-green-600' : 'text-red-800 dark:text-red-600')}>
|
!isError ? 'text-green-800 dark:text-green-600' : 'text-red-800 dark:text-red-600')}>
|
||||||
{children}
|
{children}
|
||||||
</p>
|
</p>
|
||||||
@ -134,21 +132,14 @@ Comments.Form = function Form({slug}: CommentsProps) {
|
|||||||
|
|
||||||
type CommentsProps = {
|
type CommentsProps = {
|
||||||
slug: string
|
slug: string
|
||||||
|
comments?: any
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Comments({slug}: CommentsProps) {
|
export default function Comments({slug, comments}: CommentsProps) {
|
||||||
const {data, isLoading, error} = useSWR(`/api/comments/${slug}`, fetcher) as {
|
|
||||||
data: { comments: Comment[] },
|
|
||||||
isLoading: boolean,
|
|
||||||
error: string
|
|
||||||
}
|
|
||||||
|
|
||||||
if (error) return null
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="mt-24">
|
<div className="mt-24">
|
||||||
{!isLoading &&
|
{comments &&
|
||||||
<Comments.List comments={data?.comments}/>
|
<Comments.List comments={comments}/>
|
||||||
}
|
}
|
||||||
<Comments.Form slug={slug}/>
|
<Comments.Form slug={slug}/>
|
||||||
</div>
|
</div>
|
||||||
|
24
lib/getComments.ts
Normal file
24
lib/getComments.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import {createClient} from '@/lib/supabase/client'
|
||||||
|
|
||||||
|
export async function getComments(slug: string) {
|
||||||
|
try {
|
||||||
|
const supabase = await createClient()
|
||||||
|
const {data: comments, error} = await supabase
|
||||||
|
.from('comments')
|
||||||
|
.select(`
|
||||||
|
id,
|
||||||
|
content,
|
||||||
|
published,
|
||||||
|
created_at,
|
||||||
|
user:users!inner(id, name, image),
|
||||||
|
article:articles!inner(id, title, slug)
|
||||||
|
`)
|
||||||
|
.eq('article.slug', slug)
|
||||||
|
.eq('published', true)
|
||||||
|
.order('created_at', {ascending: false})
|
||||||
|
|
||||||
|
return comments
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user