From 3db7338b98ab9d4ddb9d5c33088b74164ba21a65 Mon Sep 17 00:00:00 2001 From: Bakhtiyor Ganijon Date: Tue, 31 Oct 2023 06:53:00 +0900 Subject: [PATCH] update routes --- app/(Main)/[username]/[url]/page.tsx | 59 +++++------ app/(Main)/[username]/page.tsx | 35 +++---- app/(Main)/feed/page.tsx | 7 +- app/(Main)/page.tsx | 4 +- app/api/feed/route.ts | 125 ++++++++++++++---------- app/api/posts/[username]/views/route.ts | 66 +++---------- components/blog/actions.ts | 29 ++++++ components/blog/comment.tsx | 2 +- components/blog/more-from-author.tsx | 19 +--- components/blog/post.tsx | 9 +- components/feed/get-feed.ts | 11 ++- components/providers/top-loader.tsx | 2 +- components/user/posts.tsx | 26 ++--- 13 files changed, 190 insertions(+), 204 deletions(-) create mode 100644 components/blog/actions.ts diff --git a/app/(Main)/[username]/[url]/page.tsx b/app/(Main)/[username]/[url]/page.tsx index fa39eed2..25784480 100644 --- a/app/(Main)/[username]/[url]/page.tsx +++ b/app/(Main)/[username]/[url]/page.tsx @@ -1,11 +1,12 @@ - -import React, { use, useEffect, useState } from "react" +'use server' import { getSessionUser } from "@/components/get-session-user" import { redirect, useRouter } from "next/navigation" import postgres from "@/lib/postgres" import Post from "@/components/blog/post" import PostComment from "@/components/blog/comment" import MoreFromAuthor from "@/components/blog/more-from-author" +import { cookies } from 'next/headers' +import { incrementPostViews } from "@/components/blog/actions" export default async function PostView({ params }: { params: { username: string, url: string } }) { @@ -22,8 +23,18 @@ export default async function PostView({ params }: { params: { username: string, } }, include: { - _count: { select: { comments: true } } - } + _count: { select: { comments: true, savedUsers: true, likes: true } }, + author: { + include: { + Followers: true, + Followings: true + } + }, + }, + orderBy: { + createdAt: "desc" + }, + take: 4 }, _count: { select: { posts: true, Followers: true, Followings: true } }, Followers: true, @@ -54,7 +65,13 @@ export default async function PostView({ params }: { params: { username: string, tag: true } }, - _count: { select: { savedUsers: true, likes: true } } + author: { + include: { + Followers: true, + Followings: true + } + }, + _count: { select: { savedUsers: true, likes: true, comments: true } } } }); if (!post) redirect("/404"); @@ -66,34 +83,12 @@ export default async function PostView({ params }: { params: { username: string, if (post?.visibility !== "public") redirect("/404"); } - // async function handleFollow(followeeId: string) { - // if (status === "authenticated") { - // setIsFollowingLoading(true); - // try { - // const followerId = (await getSessionUser()).userid; - // await fetch(`/api/follow?followeeId=${followeeId}&followerId=${followerId}`, { - // method: "GET", - // }); - // } catch (error) { - // console.error(error); - // } - // } else { - // return null; - // } - - // } + - // if (!isLoaded || !post) { - // return ( - //
- // - //
- // ) - // } + await fetch(`${process.env.DOMAIN}/api/posts/${author?.username}/views/`, { + method: "POST", + body: JSON.stringify({ post: post, author: author }), + }); return ( <> diff --git a/app/(Main)/[username]/page.tsx b/app/(Main)/[username]/page.tsx index 8adca7f4..60911ef2 100644 --- a/app/(Main)/[username]/page.tsx +++ b/app/(Main)/[username]/page.tsx @@ -20,6 +20,20 @@ export default async function Page({ params }: { orderBy: { createdAt: "desc" }, + include: { + _count: { + select: { + likes: true, + savedUsers: true, + }, + }, + tags: { + take: 1, + include: { + tag: true, + }, + }, + }, }, Followers: { include: { @@ -43,28 +57,15 @@ export default async function Page({ params }: { if (!user) redirect("/404"); const posts = user.posts; - const postComments = await postgres.comment.findMany({ - where: { - postId: { - in: posts.map((post: any) => post.id) - } - } - }); posts?.forEach((post: any) => { - postComments?.forEach((comment: any) => { - if (comment.postid === post.postid) { - post.comments = post.comments + 1; - } - } - ) - } - ) + post.author = user; + }) + const followers = user.Followers; const following = user.Followings; - const sessionUserName = await getSession(); - console.log(sessionUserName); + const sessionUserName = await getSessionUser(); return (
diff --git a/app/(Main)/feed/page.tsx b/app/(Main)/feed/page.tsx index c8b77710..2df5695c 100644 --- a/app/(Main)/feed/page.tsx +++ b/app/(Main)/feed/page.tsx @@ -9,6 +9,7 @@ import { fetchTags } from '@/components/feed/get-tags'; import { getSessionUser } from '@/components/get-session-user'; import FeedTabs from '@/components/feed/navbar/navbar'; import { revalidatePath, revalidateTag } from 'next/cache'; +import { redirect } from 'next/navigation'; export default async function Feed({ searchParams @@ -28,9 +29,9 @@ export default async function Feed({ console.log("Feed before revalidate", feed); tag ? revalidatePath('/feed?tag='+tag) : revalidatePath('/feed'); console.log("Feed after revalidate", feed); - // if(!session) { - // return redirect('/') - // } + if(!session) { + return redirect('/') + } const userFollowings = session?.tagfollower; diff --git a/app/(Main)/page.tsx b/app/(Main)/page.tsx index 5a5855a5..96988e17 100644 --- a/app/(Main)/page.tsx +++ b/app/(Main)/page.tsx @@ -4,13 +4,13 @@ import { redirect } from 'next/navigation'; import dynamic from 'next/dynamic' export default function Home() { - const { status, update } = useSession(); + const { status } = useSession(); const LoadedLanding = dynamic(() => import('@/components/landing/landing'), { ssr: false, }); - if (status === 'authenticated') { + if (status !== 'unauthenticated') { redirect('/feed'); } return ; diff --git a/app/api/feed/route.ts b/app/api/feed/route.ts index 1b9cc8d3..b8163f7a 100644 --- a/app/api/feed/route.ts +++ b/app/api/feed/route.ts @@ -1,59 +1,84 @@ import postgres from '@/lib/postgres' import { NextRequest, NextResponse } from 'next/server' -export async function GET(req: NextRequest, res: NextResponse) { - try { - const user_id = Number(req.nextUrl.searchParams.get('user')) - let page = parseInt(req.nextUrl.searchParams.get('page')!) - let limit = 5 - let offset = 0 - - const userFollowings = await postgres.follow.findMany({ - select: { - followingId: true, - }, - where: { - followerId: user_id, - }, - }) - const feed = await postgres.post.findMany({ - where: { - authorId: { - in: userFollowings.map((user) => user.followingId), +export async function POST(req: NextRequest) { + const { page = 0, tag, id } = await req.json() + if (!id) { + return NextResponse.json({ error: 'No user found' }, { status: 500 }) + } + if (tag) { + const postTags = await postgres.postTag.findMany({ + select: { postId: true }, + where: { tag: { name: { equals: tag } } }, + }); + const postIds = postTags.map((postTag) => postTag.postId); + return fetchFeed({ + where: { id: { in: postIds } }, + orderBy: { createdAt: "desc" }, + take: 5, + skip: page * 5, + include: { + author: { + include: { + Followers: true, + Followings: true, + }, }, - }, - orderBy: { - createdAt: 'desc', - }, - take: limit, - skip: page * limit, - include: { - author: { - include: { - Followers: true, - Followings: true, - } - }, - _count: { - select: { - likes: true, - comments: true, - savedUsers: true, + _count: { + select: { + likes: true, + savedUsers: true, + }, + }, + tags: { + take: 1, + include: { + tag: true, + }, }, }, - tags: { - take: 1, - include: { - tag: true, + }); + } else { + const following = await postgres.follow.findMany({ + select: { followingId: true }, + where: { followerId: id }, + }); + const followingIds = following.map((user) => user.followingId); + return fetchFeed({ + where: { authorId: { in: followingIds } }, + orderBy: { createdAt: "desc" }, + take: 5, + skip: page * 5, + include: { + author: { + include: { + Followers: true, + Followings: true, + }, }, - }, - } - }) + _count: { + select: { + likes: true, + savedUsers: true, + }, + }, + tags: { + take: 1, + include: { + tag: true, + }, + }, + }, + }); + } +} - return NextResponse.json({ feed }, { status: 200 }) +const fetchFeed = async (query: any) => { + try { + const feed = await postgres.post.findMany(query); + await new Promise((resolve) => setTimeout(resolve, 750)); + return NextResponse.json({ feed: feed}, { status: 200 }); + } catch (error) { + return NextResponse.json({ error: error }, { status: 500 }); } - catch (error) { - console.error(error) - return NextResponse.json({ error: 'Something went wrong' }, { status: 500 }) - } -} \ No newline at end of file +}; diff --git a/app/api/posts/[username]/views/route.ts b/app/api/posts/[username]/views/route.ts index 7b056f8c..c04ce782 100644 --- a/app/api/posts/[username]/views/route.ts +++ b/app/api/posts/[username]/views/route.ts @@ -1,59 +1,15 @@ -import postgres from "@/lib/postgres"; -import { NextRequest, NextResponse } from "next/server"; +// pages/api/some-route.ts +import { NextRequest, NextResponse } from 'next/server'; +import { incrementPostViews } from '@/components/blog/actions'; -export async function POST( - req: NextRequest, - { params }: { params: { username: string } } -) { - try { - // Get the 'slug' route parameter from the request object - const username = params.username; - const postUrl = req.nextUrl.searchParams.get("url"); +export default async function POST(req: NextRequest, res: NextResponse) { + const { post } = await req.json(); + const { author } = await req.json(); - if (username === undefined || username === null) { - return NextResponse.json({ error: "User not found" }, { status: 404 }); - } - if (postUrl === undefined || postUrl === null) { - return NextResponse.json({ error: "Post not found" }, { status: 404 }); - } + const cookie = await incrementPostViews({ post, author }); - // Check if the view has already been counted by looking for a cookie - const cookieName = `post_views_${username}_${postUrl}`; - const hasViewed = req.cookies.get(cookieName); + // Set the cookie + res.cookies.set(cookie) - if (!hasViewed) { - // Execute a query to fetch the specific userid by name - const author = await postgres.user.findUnique({ - where: { - username: username, - }, - select: { - id: true, - }, - }); - const authorID = author?.id; - - await postgres.post.update({ - where: { - url: postUrl, - authorId: authorID, - }, - data: { - views: { - increment: 1, - }, - }, - }); - // Set a cookie to indicate that the post has been viewed - req.cookies.set(cookieName, "true",) - } - - return NextResponse.json({ message: "View added" }, { status: 200 }); - } catch (error) { - console.log(error); - return NextResponse.json( - { error: "Something went wrong" }, - { status: 500 } - ); - } -} + return NextResponse.json({ message: 'Post view incremented' }, { status: 200 }); +} \ No newline at end of file diff --git a/components/blog/actions.ts b/components/blog/actions.ts new file mode 100644 index 00000000..ccff1c95 --- /dev/null +++ b/components/blog/actions.ts @@ -0,0 +1,29 @@ +import postgres from "@/lib/postgres"; + +// actions.ts +export async function incrementPostViews({ post, author} : { post: any, author: any }) { + const cookieName = `post_views_${author?.username}_${post?.url}`; + + // Make an API request to increment the view count + await postgres.post.update({ + where: { + url: post?.url, + authorId: author?.id, + }, + data: { + views: { + increment: 1, + }, + }, + }); + + // Prepare the cookie information to be set + const expirationDate = new Date(Date.now() + 24 * 60 * 60 * 1000); // 1 day from now + return { + name: cookieName, + value: "true", + path: `/${author?.username}/${post?.url}`, + expires: expirationDate, + secure: true, + }; +} \ No newline at end of file diff --git a/components/blog/comment.tsx b/components/blog/comment.tsx index c40149df..de7a7583 100644 --- a/components/blog/comment.tsx +++ b/components/blog/comment.tsx @@ -57,7 +57,7 @@ export default function PostComment({ comments, post, postAuthor }: { comments: }, [submitted]) return ( - <>
+ <>

Comments

{/* commentform prop that inticades comment posted or not */} diff --git a/components/blog/more-from-author.tsx b/components/blog/more-from-author.tsx index 13c9a84d..c3a8b860 100644 --- a/components/blog/more-from-author.tsx +++ b/components/blog/more-from-author.tsx @@ -8,6 +8,7 @@ import { useSession } from "next-auth/react"; import LoginDialog from "../login-dialog"; import { useState } from "react"; import { getSessionUser } from "../get-session-user"; +import TagPostCard from "../tags/post-card"; export default function MoreFromAuthor({ author, post, sessionUser }: { author: any, post: any, sessionUser: any }) { const { status } = useSession(); @@ -47,7 +48,7 @@ export default function MoreFromAuthor({ author, post, sessionUser }: { author: post.length !== 0 && ( <> -
+
@@ -75,21 +76,7 @@ export default function MoreFromAuthor({ author, post, sessionUser }: { author:
{ post?.map((p: any) => ( - + )) }
diff --git a/components/blog/post.tsx b/components/blog/post.tsx index b5bde128..29a89dfb 100644 --- a/components/blog/post.tsx +++ b/components/blog/post.tsx @@ -55,11 +55,6 @@ export default function Post({ post, author, sessionUser, tags }: { post: any, a } } - useEffect(() => { - if(post){ - incrementPostViews(); - } - }, [post]); async function handleFollow(followeeId: string) { if (status === "authenticated") { @@ -85,12 +80,12 @@ export default function Post({ post, author, sessionUser, tags }: { post: any, a } return ( <> -
+
{ post?.cover && ( - {post?.title} + {post?.title} ) }

{post?.title}

diff --git a/components/feed/get-feed.ts b/components/feed/get-feed.ts index cd7cfdcf..41fd4e22 100644 --- a/components/feed/get-feed.ts +++ b/components/feed/get-feed.ts @@ -1,8 +1,17 @@ 'use server' import { getFeed } from "@/lib/prisma/feed"; +import { getSessionUser } from "../get-session-user"; export const fetchFeed = async ({ page = 0, tag }: { page?: number, tag?: string | undefined }) => { - const result = await getFeed({ page, tag }); + const user = await getSessionUser(); + + const result = await fetch(process.env.DOMAIN + "/api/feed", { + method: "POST", + body: JSON.stringify({ page, tag, id: user?.id }), + headers: { + "Content-Type": "application/json", + }, + }).then((res) => res.json()); return result?.feed; } \ No newline at end of file diff --git a/components/providers/top-loader.tsx b/components/providers/top-loader.tsx index e0f5d9ba..9102cf06 100644 --- a/components/providers/top-loader.tsx +++ b/components/providers/top-loader.tsx @@ -8,7 +8,7 @@ function TopLoader() { color="#1e9cf1" initialPosition={0.1} crawlSpeed={200} - height={8} + height={3} crawl={true} showSpinner={false} easing="ease" diff --git a/components/user/posts.tsx b/components/user/posts.tsx index 6a0b48b7..b691259d 100644 --- a/components/user/posts.tsx +++ b/components/user/posts.tsx @@ -6,6 +6,8 @@ import Link from "next/link"; import { formatNumberWithSuffix } from "../format-numbers"; import { useSession } from "next-auth/react"; import { useState } from "react"; +import { get } from "http"; +import FeedPostCard from "../blog/feed-post-card"; export default function UserPosts({ posts, className, user, sessionUser }: { posts: any, className?: string, user?: any, sessionUser?: any }) { const [deleted, setDeleted] = useState(false); @@ -16,8 +18,8 @@ export default function UserPosts({ posts, className, user, sessionUser }: { pos setDeleted(true); } - const { data: session, status } = useSession(); - if (status !== "authenticated" || !session) return null; + const { status } = useSession(); + if (status !== "authenticated") return null; return (
@@ -29,30 +31,16 @@ export default function UserPosts({ posts, className, user, sessionUser }: { pos
- + - {session?.user?.name === user?.name || session?.user?.name === user?.username ? ( + {sessionUser?.id === user?.id ? ( Edit ) : ( null )} - {session?.user?.name === user?.name || session?.user?.name === user?.username ? ( + {sessionUser?.id === user?.id ? ( handleDelete(article.postid)}> Delete ) : ( null )}