-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented follow and unfollow functionality
- Loading branch information
1 parent
8fc8bb1
commit 42b84af
Showing
13 changed files
with
294 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import CommentItem from "./CommentItem"; | ||
|
||
interface CommentFeedProps { | ||
comments?: Record<string, any>[]; | ||
} | ||
|
||
const CommentFeed: React.FC<CommentFeedProps> = ({ comments = [] }) => { | ||
return ( | ||
<> | ||
{comments.map((comment: Record<string, any>) => ( | ||
<CommentItem key={comment.id} data={comment} /> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
export default CommentFeed; |
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,78 @@ | ||
import { useRouter } from "next/router"; | ||
import { useCallback, useMemo } from "react"; | ||
import { formatDistanceToNowStrict } from "date-fns"; | ||
|
||
import Avatar from "../Avatar"; | ||
|
||
interface CommentItemProps { | ||
data: Record<string, any>; | ||
} | ||
|
||
const CommentItem: React.FC<CommentItemProps> = ({ data = {} }) => { | ||
const router = useRouter(); | ||
|
||
const goToUser = useCallback( | ||
(ev: any) => { | ||
ev.stopPropagation(); | ||
|
||
router.push(`/users/${data.user.id}`); | ||
}, | ||
[router, data.user.id] | ||
); | ||
|
||
const createdAt = useMemo(() => { | ||
if (!data?.createdAt) { | ||
return null; | ||
} | ||
|
||
return formatDistanceToNowStrict(new Date(data.createdAt)); | ||
}, [data.createdAt]); | ||
|
||
return ( | ||
<div | ||
className=' | ||
border-b-[1px] | ||
border-neutral-800 | ||
p-5 | ||
cursor-pointer | ||
hover:bg-neutral-900 | ||
transition | ||
' | ||
> | ||
<div className='flex flex-row items-start gap-3'> | ||
<Avatar userId={data.user.id} /> | ||
<div> | ||
<div className='flex flex-row items-center gap-2'> | ||
<p | ||
onClick={goToUser} | ||
className=' | ||
text-white | ||
font-semibold | ||
cursor-pointer | ||
hover:underline | ||
' | ||
> | ||
{data.user.name} | ||
</p> | ||
<span | ||
onClick={goToUser} | ||
className=' | ||
text-neutral-500 | ||
cursor-pointer | ||
hover:underline | ||
hidden | ||
md:block | ||
' | ||
> | ||
@{data.user.username} | ||
</span> | ||
<span className='text-neutral-500 text-sm'>{createdAt}</span> | ||
</div> | ||
<div className='text-white mt-1'>{data.body}</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CommentItem; |
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,60 @@ | ||
import axios from "axios"; | ||
import { useCallback, useMemo } from "react"; | ||
import { toast } from "react-hot-toast"; | ||
|
||
import useCurrentUser from "./useCurrentUser"; | ||
import useLoginModal from "./useLoginModal"; | ||
import usePost from "./usePost"; | ||
import usePosts from "./usePosts"; | ||
|
||
const useLike = ({ postId, userId }: { postId: string; userId?: string }) => { | ||
const { data: currentUser } = useCurrentUser(); | ||
const { data: fetchedPost, mutate: mutateFetchedPost } = usePost(postId); | ||
const { mutate: mutateFetchedPosts } = usePosts(userId); | ||
|
||
const loginModal = useLoginModal(); | ||
|
||
const hasLiked = useMemo(() => { | ||
const list = fetchedPost?.likedIds || []; | ||
|
||
return list.includes(currentUser?.id); | ||
}, [fetchedPost, currentUser]); | ||
|
||
const toggleLike = useCallback(async () => { | ||
if (!currentUser) { | ||
return loginModal.onOpen(); | ||
} | ||
|
||
try { | ||
let request; | ||
|
||
if (hasLiked) { | ||
request = () => axios.delete("/api/like", { data: { postId } }); | ||
} else { | ||
request = () => axios.post("/api/like", { postId }); | ||
} | ||
|
||
await request(); | ||
mutateFetchedPost(); | ||
mutateFetchedPosts(); | ||
|
||
toast.success("Success"); | ||
} catch (error) { | ||
toast.error("Something went wrong"); | ||
} | ||
}, [ | ||
currentUser, | ||
hasLiked, | ||
postId, | ||
mutateFetchedPosts, | ||
mutateFetchedPost, | ||
loginModal, | ||
]); | ||
|
||
return { | ||
hasLiked, | ||
toggleLike, | ||
}; | ||
}; | ||
|
||
export default useLike; |
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,17 @@ | ||
import useSWR from "swr"; | ||
|
||
import fetcher from "@/libs/fetcher"; | ||
|
||
const useNotifications = (userId?: string) => { | ||
const url = userId ? `/api/notifications/${userId}` : null; | ||
const { data, error, isLoading, mutate } = useSWR(url, fetcher); | ||
|
||
return { | ||
data, | ||
error, | ||
isLoading, | ||
mutate, | ||
}; | ||
}; | ||
|
||
export default useNotifications; |
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,19 @@ | ||
import useSWR from "swr"; | ||
|
||
import fetcher from "@/libs/fetcher"; | ||
|
||
const usePost = (postId: string) => { | ||
const { data, error, isLoading, mutate } = useSWR( | ||
postId ? `/api/posts/${postId}` : null, | ||
fetcher | ||
); | ||
|
||
return { | ||
data, | ||
error, | ||
isLoading, | ||
mutate, | ||
}; | ||
}; | ||
|
||
export default usePost; |
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,17 @@ | ||
import useSWR from "swr"; | ||
|
||
import fetcher from "@/libs/fetcher"; | ||
|
||
const usePosts = (userId?: string) => { | ||
const url = userId ? `/api/posts?userId=${userId}` : "/api/posts"; | ||
const { data, error, isLoading, mutate } = useSWR(url, fetcher); | ||
|
||
return { | ||
data, | ||
error, | ||
isLoading, | ||
mutate, | ||
}; | ||
}; | ||
|
||
export default usePosts; |
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,19 @@ | ||
import useSWR from "swr"; | ||
|
||
import fetcher from "@/libs/fetcher"; | ||
|
||
const useCurrentUser = (userId: string) => { | ||
const { data, error, isLoading, mutate } = useSWR( | ||
userId ? `/api/users/${userId}` : null, | ||
fetcher | ||
); | ||
|
||
return { | ||
data, | ||
error, | ||
isLoading, | ||
mutate, | ||
}; | ||
}; | ||
|
||
export default useCurrentUser; |
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
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,57 @@ | ||
import { prisma } from "@/libs/prismadb"; | ||
import serverAuth from "@/libs/serverAuth"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
if (req.method !== "POST" && req.method !== "GET") { | ||
return res.status(405).end(); | ||
} | ||
|
||
try { | ||
if (req.method === "POST") { | ||
const { currentUser } = await serverAuth(req, res); | ||
const { body } = req.body; | ||
const post = await prisma.post.create({ | ||
data: { body, userId: currentUser.id }, | ||
}); | ||
|
||
return res.status(200).json(post); | ||
} | ||
|
||
if (req.method === "GET") { | ||
const { userId } = req.query; | ||
let posts; | ||
if (userId && typeof userId === "string") { | ||
posts = await prisma.post.findMany({ | ||
where: { | ||
userId, | ||
}, | ||
include: { | ||
user: true, | ||
comments: true, | ||
}, | ||
orderBy: { | ||
createdAt: "desc", | ||
}, | ||
}); | ||
} else { | ||
posts = await prisma.post.findMany({ | ||
include: { | ||
user: true, | ||
comments: true, | ||
}, | ||
orderBy: { | ||
createdAt: "desc", | ||
}, | ||
}); | ||
} | ||
return res.status(200).json(posts); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
return res.status(400).end(); | ||
} | ||
} |
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