Skip to content
Snippets Groups Projects
Commit 26c4e7e5 authored by Yusuf Akgül's avatar Yusuf Akgül :hatching_chick:
Browse files

small fix and working follow button

parent de10c572
No related branches found
No related tags found
1 merge request!38Profile
Pipeline #39655 passed
...@@ -6,8 +6,8 @@ import { Card } from "@/components/ui/card" ...@@ -6,8 +6,8 @@ import { Card } from "@/components/ui/card"
import { UserNotFound } from "@/components/user-not-found" import { UserNotFound } from "@/components/user-not-found"
import getURL from "@/lib/utils" import getURL from "@/lib/utils"
export const dynamic = 'force-dynamic' // export const dynamic = 'force-dynamic'
export const fetchCache = 'force-no-store' // export const fetchCache = 'force-no-store'
export default async function ProfileLayout({ export default async function ProfileLayout({
params, params,
......
...@@ -87,9 +87,7 @@ export async function PUT(request: Request) { ...@@ -87,9 +87,7 @@ export async function PUT(request: Request) {
// unfollow a user // unfollow a user
export async function DELETE(request: Request) { export async function DELETE(request: Request) {
const session = await getCurrentUser() const session = await getCurrentUser()
const { searchParams } = new URL(request.url) const { userId } = await request.json()
const userId = searchParams.get("userId") || undefined
const followerIdSchema = z const followerIdSchema = z
.object({ .object({
......
"use client" "use client"
import { useRouter } from "next/navigation"
import { useEffect, useState } from "react"
import { Icons } from "./icons"
import { useFollow } from "./profile/hooks/use-follow" import { useFollow } from "./profile/hooks/use-follow"
import { Button } from "./ui/button" import { Button } from "./ui/button"
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "./ui/dialog" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "./ui/dialog"
...@@ -16,17 +19,41 @@ export const FollowButton = ({ ...@@ -16,17 +19,41 @@ export const FollowButton = ({
const followMutation = useFollow("follow") const followMutation = useFollow("follow")
const unfollowMutation = useFollow("unfollow") const unfollowMutation = useFollow("unfollow")
const [text, setText] = useState<"Following" | "Unfollow">("Following")
const [isHovered, setIsHovered] = useState(false)
const [open, setOpen] = useState(false)
const router = useRouter()
useEffect(() => {
if (followMutation.isSuccess) {
router.refresh()
}
if (unfollowMutation.isSuccess) {
setOpen(false)
router.refresh()
}
}, [router, followMutation.isSuccess, unfollowMutation.isSuccess])
return ( return (
<Dialog> <Dialog open={open} onOpenChange={setOpen}>
{isFollowing ? ( {isFollowing ? (
<Button variant="outline" size="lg" > <DialogTrigger asChild>
<span className="hover:hidden">Following</span> <Button variant={`${isHovered ? "destructive" : "outline"}`} size="lg"
<DialogTrigger> className="w-24"
<span className="hidden hover:block text-destructive border-destructive">Unfollow</span> onMouseEnter={() => {
</DialogTrigger> setText("Unfollow")
</Button> setIsHovered(true)
}}
onMouseOut={() => {
setText("Following")
setIsHovered(false)
}}>
{text}
</Button>
</DialogTrigger>
) : ( ) : (
<Button variant="outline" size="lg" onClick={() => followMutation.mutate({ userId })}> <Button size="lg" onClick={() => followMutation.mutate({ userId })}>
Follow Follow
</Button> </Button>
)} )}
...@@ -34,12 +61,14 @@ export const FollowButton = ({ ...@@ -34,12 +61,14 @@ export const FollowButton = ({
<DialogHeader> <DialogHeader>
<DialogTitle>Unfollow @{username}?</DialogTitle> <DialogTitle>Unfollow @{username}?</DialogTitle>
<DialogDescription> <DialogDescription>
Their Gweets will no longer show up in your home timeline. You can still view their profile, unless their Gweets are protected.
You can still view their profile, unless their Tweets are protected.
</DialogDescription> </DialogDescription>
</DialogHeader> </DialogHeader>
<DialogFooter> <DialogFooter>
<Button type="submit" onClick={() => unfollowMutation.mutate({ userId })}> <Button type="submit" onClick={() => unfollowMutation.mutate({ userId })} disabled={unfollowMutation.isLoading}>
{unfollowMutation.isLoading && (
<Icons.spinner className="mr-2 h-4 w-4 animate-spin" />
)}
Unfollow Unfollow
</Button> </Button>
</DialogFooter> </DialogFooter>
......
export const followUser = async (userId: string) => { export const followUser = async (userId: string) => {
console.log({ userId })
try { try {
const data = await fetch("/api/users/follow", { const data = await fetch("/api/users/follow", {
method: "POST", method: "PUT",
body: JSON.stringify({ body: JSON.stringify({ userId }),
userId,
}),
}).then((result) => result.json()) }).then((result) => result.json())
return data return data
......
export const unfollowUser = async ( export const unfollowUser = async (userId: string) => {
user_id: string,
) => {
try { try {
const { data } = await fetch(`/api/users/follow?user_id=${user_id}`, { const data = await fetch(`/api/users/follow`, {
method: "DELETE" method: "DELETE",
body: JSON.stringify({ userId }),
}).then((result) => result.json()) }).then((result) => result.json())
return data return data
......
...@@ -10,25 +10,25 @@ export const ProfileNavbar = ({ children, param }: { children: React.ReactNode, ...@@ -10,25 +10,25 @@ export const ProfileNavbar = ({ children, param }: { children: React.ReactNode,
return ( return (
<Tabs defaultValue={"gweets"} value={pathValue} className="w-full h-full p-6 md:p-12 "> <Tabs defaultValue={"gweets"} value={pathValue} className="w-full h-full p-6 md:p-12 ">
<TabsList className="grid w-full grid-cols-4"> <TabsList className="grid w-full grid-cols-4">
<Link href={`/${param}`} className="w-full inline-flex items-center justify-center"> <Link href={`/${param}`} scroll={false} className="w-full inline-flex items-center justify-center">
<TabsTrigger value="gweets" className="w-full"> <TabsTrigger value="gweets" className="w-full">
Gweets Gweets
</TabsTrigger> </TabsTrigger>
</Link> </Link>
<Link href={`/${param}/games`} className="w-full inline-flex items-center justify-center"> <Link href={`/${param}/games`} scroll={false} className="w-full inline-flex items-center justify-center">
<TabsTrigger value="games" className="w-full"> <TabsTrigger value="games" className="w-full">
Games Games
</TabsTrigger> </TabsTrigger>
</Link> </Link>
<Link href={`/${param}/media`} className="w-full inline-flex items-center justify-center"> <Link href={`/${param}/media`} scroll={false} className="w-full inline-flex items-center justify-center">
<TabsTrigger value="media" className="w-full"> <TabsTrigger value="media" className="w-full">
Media Media
</TabsTrigger> </TabsTrigger>
</Link> </Link>
<Link href={`/${param}/likes`} className="w-full inline-flex items-center justify-center"> <Link href={`/${param}/likes`} scroll={false} className="w-full inline-flex items-center justify-center">
<TabsTrigger value="likes" className="w-full"> <TabsTrigger value="likes" className="w-full">
Likes Likes
</TabsTrigger> </TabsTrigger>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment