generated from warmachine028/github-super-starter-kit
-
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.
- Loading branch information
1 parent
4a527d9
commit a29db7b
Showing
4 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
Binary file not shown.
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,74 @@ | ||
'use client' | ||
|
||
import { useToast } from '@/hooks' | ||
import { QueryKey, useMutation, useQuery, useQueryClient } from '@tanstack/react-query' | ||
import { Button } from '../ui/button' | ||
import { LikeInfo } from '@/types' | ||
import { kyInstance } from '@/lib/ky' | ||
import { Heart } from 'lucide-react' | ||
import { cn } from '@/lib/utils' | ||
|
||
interface LikeButtonProps { | ||
postId: string | ||
initialState: LikeInfo | ||
} | ||
|
||
const LikeButton = ({ postId, initialState }: LikeButtonProps) => { | ||
const { toast } = useToast() | ||
const queryClient = useQueryClient() | ||
const queryKey: QueryKey = ['like-info', postId] | ||
|
||
const { data } = useQuery({ | ||
queryKey, | ||
queryFn: () => kyInstance.get(`/api/posts/${postId}/likes`).json<LikeInfo>(), | ||
initialData: initialState, | ||
staleTime: Infinity | ||
}) | ||
|
||
const { mutate } = useMutation({ | ||
mutationFn: () => | ||
data.isLikedByUser ? | ||
kyInstance.delete(`/api/posts/${postId}/likes`) | ||
: kyInstance.post(`/api/posts/${postId}/likes`), | ||
onMutate: async () => { | ||
await queryClient.cancelQueries({ queryKey }) | ||
|
||
const previousState = queryClient.getQueryData<LikeInfo>(queryKey) | ||
queryClient.setQueryData<LikeInfo>(queryKey, () => ({ | ||
likes: | ||
(previousState?.likes || 0) + // | ||
(previousState?.isLikedByUser ? -1 : +1), | ||
isLikedByUser: !previousState?.isLikedByUser | ||
})) | ||
return { previousState } | ||
}, | ||
onError: (error, _, context) => { | ||
queryClient.setQueryData(queryKey, context?.previousState) | ||
console.error(error) | ||
toast({ | ||
variant: 'destructive', | ||
description: 'Something went wrong. Please try again.' | ||
}) | ||
} | ||
}) | ||
|
||
return ( | ||
<Button | ||
variant="ghost" | ||
aria-label={data.isLikedByUser ? 'Unlike' : 'Like'} | ||
aria-pressed={data.isLikedByUser} | ||
aria-atomic | ||
onClick={() => mutate()} | ||
className="flex items-center gap-2 text-sm font-medium tabular-nums" | ||
> | ||
<Heart className={cn('size-4', data.isLikedByUser && 'fill-destructive text-destructive')} /> | ||
<span> | ||
{data.likes} <span className="hidden sm:inline">likes</span> | ||
</span> | ||
</Button> | ||
) | ||
} | ||
|
||
LikeButton.displayName = 'LikeButton' | ||
|
||
export default LikeButton |
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
a29db7b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-book – ./
nextife.vercel.app
next-book-pritam.vercel.app
next-book-git-main-pritam-kundu.vercel.app
next-boook.vercel.app
nextifly.vercel.app
next-book-pritam-kundu.vercel.app
next-book-15.vercel.app